First, the design table specification:
1. MySQL builds a table, the field needs to be set to non-null, and the default value of the field needs to be set.
2. When creating a table in mysql, when the field needs to be NULL, the default value of the field needs to be set, and the default value is not NULL.
3. MySQL creates a table. If the field is equivalent to a foreign key, an index should be added to the field.
4. When creating a MySQL table, the fields, column types, type lengths, non-null and default values of the same attribute values between different tables must be consistent, otherwise the index cannot be correctly used for association comparison.
5. When using MySQL, one SQL statement can only use one index of one table. All field types can be indexed, and a multi-column index can have up to 15 attributes.
6. If it is possible to select among multiple indexes, MySQL usually uses the index that finds the fewest rows and indexes the index with the highest unique value.
7. Establishing an index index (part1, part2, part3) is equivalent to establishing three indexes of index (part1), index (part1, part2) and index (part1, part2, part3).
8. MySQL uses the following format for the like syntax:
SELECT * FROM t1 WHERE key_col LIKE 'ab%' ;
9. SELECT COUNT(*) syntax is not as efficient as SELECT COUNT(col_name) in a statement without where condition. But it is faster to execute in a statement with where condition.
10. In the multiple and conditions in the where condition, all must be the key_part attribute of a multi-column index and must contain key_part1. If each has a single index, only the index that traverses the fewest rows is used.
11. Among the multiple or conditions in the where condition, each condition must be a valid index.
12. The conditions behind ORDER BY must be attributes of the same index, and the sorting order must be consistent (for example, both ascending or descending).
13. All GROUP BY columns refer to attributes of the same index, and the index must hold its keys in order.
14. JOIN index, all fields matching ON and where should establish appropriate indexes.
15. Use FORCE INDEX to inform MySQL that it is more efficient to use indexes for intelligent scan full tables.
16. Periodically ANALYZE TABLE tbl_name updates the key distribution for the scanned table.
17. Periodically use slow log check statements, execute explain, and analyze indexes for possible improvements.
18. If conditions permit, set larger values of key_buffer_size and query_cache_size (global parameters), and the value of sort_buffer_size (session variable, it is recommended not to exceed 4M).
Second, naming conventions:
1. The naming of the primary key adopts the following rules:
the name of the primary key starts with pk_, followed by the name of the table where the primary key is located. The length of the primary key name cannot exceed 30 characters. If it is too long, the table name can be abbreviated. Abbreviation rules are the same as the abbreviation rules for table names. Primary key names are represented by lowercase English words.
2. The naming of foreign keys adopts the following rules:
the foreign key name starts with fk_, followed by the table name where the foreign key is located and the corresponding main table name (excluding t_). Child table names and parent table names are themselves separated by underscores (_). Foreign key names cannot exceed 30 characters in length. If it is too long, the table name can be abbreviated. Abbreviation rules are the same as the abbreviation rules for table names. Foreign key names are represented by lowercase English words.
3. The naming of indexes adopts the following rules:
1) The index name is represented by lowercase English letters and numbers. The length of the index name cannot exceed 30 characters.
2) The index corresponding to the primary key has the same name as the primary key.
3) Unique indexes start with uni_ followed by the table name. Generic indexes start with ind_ followed by the table name.
4) If the index length is too long, the table name can be abbreviated. Abbreviation rules are the same as the abbreviation rules for table names
5) Basic syntax of index
CREATE INDEX index name ON table name (field);
show index from table name;
drop index index name on table name;
6) For fields with more unique values, use the index of The better the effect. When setting up a joint index, the more unique values, the more they should be placed on the "left side".
Three, SQL statement optimization specifications:
1. Use mysql explain to test the efficiency of SQL execution, explain shows how mysql uses indexes to process select statements and join tables. Can help choose better indexes and write more optimized query statements.
1) How to use: add explain before the select statement. 2) The form of explain analysis
result is as follows: table
| type | possible_keys | key | key_len | ref | rows | A row of data is about which table type: This is the important column, showing what type is used for the connection. The join types from best to worst are const, eq_reg, ref, range, indexhe, and ALL
possible_keys : Displays the indexes that may be applied to this table. If empty, there are no possible indices. An appropriate statement
key can be selected from the WHERE statement for the relevant field: the index actually used. If NULL, no index is used. In rare cases, MYSQL will choose an index that is under-optimized. In this case, you can use USE INDEX(indexname) in the SELECT statement to force the use of an index or IGNORE INDEX(indexname) to force MYSQL to ignore the index
key_len: the length of the index used. Shorter lengths are better without loss of precision
ref: shows which column of the index is used, a constant if possible
rows: the number of rows that MYSQL thinks must be checked to return the requested data
Extra: return The meaning of the description 2. Try to use "inner join" query to replace "in" in the sub- query
condition, to prevent the database from hanging due to the large amount of data
.
When "!=" or "<>" is used later, the index does not take effect, just like ordinary fields.
2) If string functions or other functions are used after the where condition of the query statement, the index does not take effect, just like ordinary fields.
3) Use the connection ( When using join) query, the index will only take effect when the data types of the primary key and foreign key are the same.
4) Use the Like keyword after the where condition of the query statement. It should be noted that the like '%jx%' and like '%jx' indexes are both Does not take effect, like 'jx%' index takes effect
5) InnoDB data table does not support full-text index
MySQL table index specification
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=326170588&siteId=291194637
Recommended
Ranking