search
HomeBackend DevelopmentPHP Tutorial'Internet MySQL Development Specifications' compiled based on years of experience

Written on the front: There is no circle without rules. For those who have just joined the Internet, they will definitely come into contact with MySQL, MySQL, as the most popular relational database product on the Internet, it has its strengths and weaknesses. , based on its characteristics, combined with the characteristics of most applications on the Internet, based on my many years of experience as a MySQL DBA in Internet companies, the author has now summarized some development specifications for the Internet MySQL, for reference only.
1. Basic specifications
(1) Use INNODB storage engine
(2) Character Set using UTF8
(3) All tables need to add comments
(4) It is recommended that the data volume of a single table be controlled at 5000W Within
(5) Do not store graphs in the database, files and other big data
(6) It is prohibited to do database stress testing online
(7) ForbiddenDirect connection to the database from the test and development environment
2. Naming convention
(1) Library name, table name and field name must have a fixed naming length, 12 characters or less
(2) Library name, table name, field name are forbidden limited to more than 32 characters. The meaning of the name must be seen
(3) Library name, table name, field name forbiddenDisabled Use MySQL Reserved words
(4) Temporary library and table names must be prefixed with tmp and Date is suffix
(5) Backup libraries and tables must be prefixed with bak and suffixed with date
3. Library, table, field development and design specifications
(1) forbiddenUse partition table
(2) Split large fields and have low access frequency fields, separating hot and cold Data
(3) Use HASH to enter , the table name suffix is ​​ ⽤⼗base number , the subscript starts from 0
(4) The table by date and time must comply with YYYY[MM][DD][HH] format
(5) Adopt appropriate database and table sharding strategy. For example, Qianku Ten Tables, Ten Databases Hundred Tables, etc.
(6) Try not to use TEXT, BLOB types
(7) Use DECIMAL instead of FLOAT and DOUBLE to store precise floating point numbers
(8) The simpler, the better: convert characters For numbers, use TINYINT instead of ENUMtype
(9) All fields are defined as NOT NULL
(10) Use UNSIGNED to store non-negative integers
(11) INT type fixed occupation 4byte storage
(12) Use timestamp to store time
(13) Use INT UNSIGNED to store IPV4
(14) USE VARBINARY Storage case-sensitive variable-length strings
(15) It is forbidden to store plain text passwords in the database, and the passwords should be encrypted and stored
(16) Use numeric type fields
Tinyint (1Byte)
smallint (2Byte)
mediumint (3Byte)
int (4Byte)
bigint (8Byte)
0
If the numerical field is not that big, don’t use bigint
(17) Storageip is best to use intstorage Instead of char(15)
(18) is not allowed to use ENUM
(19) Avoid using NULL Field
The NULL field is difficult to query optimization, the index of the NULL field requires additional space, the composite index of the NULL field is invalid
(20) Use less The performance of text/blob, varchar will be much higher than text, it is really unavoidable blob, please disassemble the table
(21) Large files or photos are not allowed to be stored in the database. You can put large objects on the disk and store their paths in the database
4. Index specifications
1, the number of indexes should be controlled:
(1) The number of indexes in a single table should not exceed 5
(2) The number of fields in a single index does not exceed 5
(3) Use prefix index for strings, and the prefix index length does not exceed 8 characters(4) It is recommended to give priority to prefix index. If necessary, you can add pseudo columns and create indexes
2, primary key guidelines
(1) The table must have a primary key
(2) Do not use frequently updated columns as primary keys
(3) Try not to choose string columns as primary keys
(4) Do not use
UUID MD5 HASH
These are used as primary keys(The values ​​are too discrete)
(5) By default, non-empty unique key is used as the primary key
(6) It is recommended to choose auto-increment or issuer
3, important SQL must be indexed, such as:
(1) UPDATE, DELETE statement WHEREConditions Column
(2) ORDER BY , GROUP BY, DISTINCT fields
4 , multiple tablesJOIN The fields of should be noted as follows:
(1) The field with the greatest distinction is placed in front
(2) core SQL Prioritize covering indexes
(3) Avoid redundant and duplicate indexes
(4) Indexes should comprehensively evaluate data density and distribution and consider query and update ratio
5, index taboos
(1) Do not create indexes on low cardinality columns, such as "gender"
(2) Do not perform mathematical operations and functions on index columns Operation
6, try not to use foreign keys
(1) Foreign keys are used to protect referential integrity and can be implemented on the business side
(2) Operations on the parent table and child table will affect each other, reducing availability
7, index naming: non-unique indexes must start with idx_field1_field 2 is named, unique so it must be named after uniq_field1_field2, the index name must be all lowercase
8 . The newly created unique index must not duplicate the primary key
9, the default value of the index field cannot be NULL, it must be changed to other default or empty. NULLvery affects the query efficiency of the index
10, repeatedly check the SQLSQL
related to the table, and build the index in line with the characteristics of the leftmost prefix. For statements with multiple repeated fields, you need to modify the order of the statement condition fields and create a joint index for it to reduce the number of indexes
11
. If you can use a unique index, you must use a unique index to improve query efficiency
12, R&D should use explain frequently. If they find that the index selectivity is poor, they must learn to use
hint
五、 SQL Find a way to break it into smaller pieces sql
statement
( fully utilizes
QUERY CACHE and fully utilizes multi-core CPU)(2) The transaction should be simple and the length of the entire transaction Not too long (3) Avoid using triggers, functions, stored procedures(4)
Reduce business coupling and
sacle out , sharding
Leave room for(5)
Avoid doing Mathematical operations (MySQL is not good at math operations And logical judgment )
(4) Don’t use select *, just select these fields (5) The use of
OR in sql is rewritten as IN() (or is not as efficient as in
)
(6) It is recommended that the number of numbers in should be controlled within 1000
(7) limitPay attention to efficiency when paginating. LimitThe larger, the lower the efficiency. You can rewrite limit, for example, rewrite the example:
select id fromtlimit 10000, 10; => select id from t where id > 10000 limit10;
( 9) Use union all instead of union
(10) Avoid using for big watches JOIN
(11) Use group by Group and automatically sort
(12) Update the data in batches and do not update too much data at one time
(13) ) Reduce the number of interactions with the database
(13) Pay attention to the use of performance analysis tools
Sql explain / showprofile / mysqlsla
(14) SQL statement requirements All R&D, SQLkeywords are all capitalized, and each word is only allowed to have one space
(15) SQL statements cannot have implicit conversions, such as select id from table where id='1'
(16) INThe number of data in the condition should be small, I remember it should be within 500, you must learn to use it exist replaces in, exist in some scenarios the query will be faster than in
(17) can be used NOT INNo need to NOTIN, there are too many pitfalls. . Will detect nulls and NULL
(18) In SQL statements, the prohibited prefix is ​​% ’s like
(19) Do not use negative queries, such as not in/like
(19) About paging queries: The program recommends the rational use of paging to improve efficiencylimit , offset is larger and should be used with subqueries
(20) It is forbidden to run large queries in the database
(21) make Precompiled statements, only passing parameters, are more efficient than passing SQL statements; parse once, use multiple times; reduce SQL injection probability
( 22) forbidden to useorder by rand()
(23) forbidden SingleSQLstatement at the same time Update multiple tables
6. Process specification
(1) All table creation operations need to inform the query involved in the table in advancesql
(2) All table creation needs to determine which indexes should be established before the table can be built online;
(3) All operations of changing the table structure and adding indexes must be done It is necessary to send out the query sql involving the changed table to inform DBA and other relevant personnel;
(4) Before creating a new table and adding fields, require R&D The email must be sent out at least 3days in advance to give dba time for evaluation, optimization and review
(5)Batch import and export data must be notified in advance DBAAssist in observation
(6) forbiddenExecute background management and statistical queries online from the library
(7) BanApp accounts with super permissions exist
(8) Promotion activities or new features must be notified in advanceDBAEnter to conduct traffic assessment
(9) not in business Batch update and database query during peak period
Receive Brothers IT Education’s original Linux operation and maintenance engineer video/detailed Linux tutorial for free. For details, please consult the official website customer service: http://www.lampbrother.net/linux/
Learn PHP, Linux, HTML5, UI, Android and other video tutorials (courseware + notes + video)! Contact Q2430675018
Welcome to joinlinux communication group 478068715

Type
bytes
Minimum value
Maximum
(signed/unsigned)
(signed/unsigned) Symbolic)
TINYINT
1
-128
127
Unsigned
0
255
SMALLINT
2
-32768
32767
Unsigned
0
65535
MEDIUMINT
3
-8388608
8388607
None Symbol
0
16777215
INT
4
-2147483648
2147483647
Unsigned
BIGINT
8
-9223372036854775808
9223372036854775807
unsigned
18446744073709551615


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
11 Best PHP URL Shortener Scripts (Free and Premium)11 Best PHP URL Shortener Scripts (Free and Premium)Mar 03, 2025 am 10:49 AM

Long URLs, often cluttered with keywords and tracking parameters, can deter visitors. A URL shortening script offers a solution, creating concise links ideal for social media and other platforms. These scripts are valuable for individual websites a

Working with Flash Session Data in LaravelWorking with Flash Session Data in LaravelMar 12, 2025 pm 05:08 PM

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

Build a React App With a Laravel Back End: Part 2, ReactBuild a React App With a Laravel Back End: Part 2, ReactMar 04, 2025 am 09:33 AM

This is the second and final part of the series on building a React application with a Laravel back-end. In the first part of the series, we created a RESTful API using Laravel for a basic product-listing application. In this tutorial, we will be dev

Simplified HTTP Response Mocking in Laravel TestsSimplified HTTP Response Mocking in Laravel TestsMar 12, 2025 pm 05:09 PM

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

cURL in PHP: How to Use the PHP cURL Extension in REST APIscURL in PHP: How to Use the PHP cURL Extension in REST APIsMar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

12 Best PHP Chat Scripts on CodeCanyon12 Best PHP Chat Scripts on CodeCanyonMar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Announcement of 2025 PHP Situation SurveyAnnouncement of 2025 PHP Situation SurveyMar 03, 2025 pm 04:20 PM

The 2025 PHP Landscape Survey investigates current PHP development trends. It explores framework usage, deployment methods, and challenges, aiming to provide insights for developers and businesses. The survey anticipates growth in modern PHP versio

Notifications in LaravelNotifications in LaravelMar 04, 2025 am 09:22 AM

In this article, we're going to explore the notification system in the Laravel web framework. The notification system in Laravel allows you to send notifications to users over different channels. Today, we'll discuss how you can send notifications ov

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)