MySQL common norms

A. To build the table specification

1. Use the InnoDB engine

No special circumstances must use InnoDB engine (version 5.5 default engine)

InnoDB supports transactions, better row-level locking, MVCC, concurrent performance, CPU and memory cache page optimization allows better resource utilization.

2. specification table name, the field

Table names, field names, lowercase underlined style, as far as possible see to know the name of Italy.

3. Naming Specification Index

Primary key index table _ called pk_ field names, the only index table _ called uk_ field name, the general index for the table name _ idx_ field name.

4. NOT NULL field advantage

NULL columns in the index, index statistics, worthy of comparison is more complicated;

NULL type internal MySQL require special handling, increase the complexity of the recording process, NULL columns require additional space identifier;

Processing can be employed is the NULL IS NULL or IS NOT NULL:! WHERE name = 'John Doe'; query result will not contain the name of the recording NULL.

5.char 与 varchar

If the fixed length of a string, you should use fixed-length string type char, high access efficiency;

If the string length is not fixed, you should use varchar variable-length string type, it allocates space based on the actual length of the string, to conserve resources. If the string length is more than 5000, it should be defined as a text field and other large text type, and a separate table, associated with the primary key index to avoid affecting the efficiency of other fields.

II. Index specification

1. good use of the unique index

Field has a unique characteristic of the business, even more fields, must be built in a unique index, a unique index can significantly improve query performance (general index, also known as left-prefix index, if the field is too long (767bytes) will only index fields before part of the index is the only index the entire field).

2. The most left-prefix principles

B Tree index file has the most left-prefix matching characteristics +, if the leftmost value undetermined, it can not use this index. The establishment of joint index, the highest distinction of the field on the left.

3. Control number of indexes

Single table index recommended control in less than 5, no more than five joint index field.

4. carefully indexed

Prohibited frequently updated, the index is not high distinction of the column;

Updates will change B + Tree, greatly reducing performance;

"Gender" This is not discrimination properties, indexing is meaningless, performance similar to a full table scan.

The association should pay attention to the type of query related field

When related inquiries, guaranteed to be associated field must have an index, and consistent data type, to prevent different field types cause implicit conversion, resulting in failure of the index.

Three. SQL statements

1. caution SELECT *

Do not recommend using SELECT *, obtain only the necessary fields, read unnecessary columns will increase CPU, IO, NET consumption.

Meanwhile, SELECT * easy to add or remove fields after the bug.

2. prohibit the use of implicit conversion

WHERE id = 1201800992222222;  

If the id is the string will produce an implicit type conversion, resulting in a full table scan.

3. prohibited functions on condition attributes WHERE

WHERE CAST(create_time AS SIGNED) < 20190708180606;

By CAST function, to convert the date into an integer type, resulting in a full table scan.

Trap 4. Statistical functions

When a full column is NULL, COUNT (col) returns 0, using the SUM (col) returns NULL, NULL values ​​statistical function does the calculation.

5.IN与EXISTS

Known A, B two tables:

A:100000 B:100

SELECT * FROM A WHERE A.id IN (SELECT id FROM B);

IN large table small table, high efficiency : small tables can be loaded into memory, you can hit the big table index (contents IN to try not to exceed 200).

A:100 B:100000

SELECT * FROM A WHERE EXISTS (SELECT * FROM B WHERE B.id = A.id);

Small table EXISTS large table, high efficiency : a small table full table scan, you can hit the big table index.

6. It should be noted association table

Table association should try to follow the principle of small table-driven large table;

A LEFT JOIN B should pay attention to: ON condition is associated with pre-screening, WHERE condition associated after screening;

SELECT * FROM A LEFT JOIN B ON A.id = B.id AND B.name = 'z' WHERE a.age> 20; - if A 10 records match, B only a matching record, the final return 10 recording;

SELECT * FROM A LEFT JOIN B on A.id = B.id WHERE a.age> 20 AND B.name = 'z'; - if A 10 records match, B only a matching record, return a final recording;

7. good use EXPLAIN

SQL execution efficiency analysis, the first re-live the King, the results of EXPLAIN to prevail, at least to reach the level range.

(1) consts single table can only have a matching row (primary key or unique index).

(2) ref refers to the use of an ordinary index (normal index).

(3) range of the index range search.

(4) index index full scan (index full scan physical documents, very slowly, the index level is also relatively low range, and a full table scan is trivial).

(5) all full table scan

Published 149 original articles · won praise 100 · views 180 000 +

Guess you like

Origin blog.csdn.net/u011212394/article/details/96430000