Mysql built Table norms and precautions

A table design specifications

  1. Database names, table names, field names must be lowercase letters, "_" split.
  2. Database names, table names, field names must be no more than 12 characters.
  3. Database names, table names, field names intended to see to know the name, it is recommended to use a noun rather than a verb.
  4. We recommend using InnoDB storage engine.
  5. Precision floating-point store must use the alternative DECIMAL FLOAT and DOUBLE.
  6. UNSIGNED is recommended to store non-negative values.
  7. INT UNSIGNED recommended storage IPV4.
  8. Shaping the length of the definition is not added, such as the use of INT, instead of INT (4).
  9. Using short data types, such as in the range 0-80 when used TINYINT UNSIGNED.
  10. Not recommended to use ENUM type, use TINYINT instead.
  11. Do not use TEXT, BLOB types as possible.
  12. VARCHAR (N), N represents the number of characters than the number of bytes, such as VARCHAR (255), can store maximum 255 characters, it is necessary to choose the actual width N.
  13. VARCHAR (N), N as small as possible, because a table of all the MySQL VARCHAR maximum field length is 65535 bytes, sort and create a temporary table for a class of memory operation, the length of N will be used for memory.
  14. Select the table character set UTF8.
  15. Use VARBINARY storing variable-length strings.
  16. YEAR type of storage use.
  17. Storage date using the DATE type.
  18. Storage time (accurate to the second) type TIMESTAMP recommended because TIMESTAMP uses 4 bytes, 8 bytes DATETIME.
  19. Suggestion field is defined as NOT NULL.
  20. The split oversized fields to other tables.
  21. Prohibit the use VARBINARY, BLOB store pictures, documents, etc. in the database.
  22. Table structure changes need to inform the DBA review.

Second, the index specification

  1. Non-unique index must "idx_ field name field name _ [_ field name]" named in accordance with.
  2. The only index must "uniq_ field name field name _ [_ field name]" named in accordance with.
  3. Index names must be lowercase.
  4. The number of fields in the index is not recommended over five.
  5. The number of index controlled within a single table 5.
  6. Unique key fields of three or less, and when the fields are shaping, using a unique primary keys.
  7. When no unique key or a unique key ineligible 5 using increment (number or by sending an acquisition) id as the primary key.
  8. Unique key and primary key are not repeated.
  9. Need to consider the order of the index field number field values ​​after de-emphasis, and more in front of the number.
  10. ORDER BY, GROUP BY, DISTINCT fields need to be added after the index.
  11. SQL statement using EXPLAIN to determine whether the rational use of the index, to avoid extra columns appear: Using File Sort, UsingTemporary.
  12. UPDATE, DELETE statements need to add an index based on the WHERE condition.
  13. Not recommended% prefix fuzzy queries, such as LIKE "% weibo".
  14. Indexing a VARCHAR field length is too long, or the MD5 Hash crc32 added field, Hash indexed fields.
  15. Reasonable created joint index (avoid redundancy), (a, b, c) is equivalent to (a), (a, b), (a, b, c).
  16. Rational use of covering indexes.
  17. SQL changes need to confirm whether the index needs to be changed and notify the DBA.

Three, SQL statements, specification

  1. Use prepared statement, you can provide performance and avoid SQL injection.
  2. IN contains the value of the SQL statement should not be too much.
  3. UPDATE, DELETE statement does not use LIMIT.
  4. WHERE condition must use the appropriate type, to avoid MySQL implicit type conversion.
  5. SELECT statement to obtain only required field.
  6. SELECT, INSERT statement must be explicitly specified field names, without using SELECT *, not using INSERTINTO table ().
  7. 使 用SELECT column_name1, column_name2 FROM table WHERE[condition]而不是SELECT column_name1 FROM table WHERE[condition]和SELECT column_name2 FROM table WHERE [condition]。
  8. Nonequijoins the WHERE conditions (IN, BETWEEN, <, <=,>,> =) will cause the latter condition can not use the index.
  9. To avoid calculation mathematical operation or function in a SQL statement, DB and service logic easily coupled together.
  10. INSERT statement using batch submission (INSERT INTO tableVALUES (), (), () ......), the number of values ​​should not be too much.
  11. Avoid using stored procedures, triggers, functions, etc., DB and service logic easily coupled together, and MySQL stored procedures, triggers, functions, present certain bug.
  12. Avoid using JOIN.
  13. Rational use SQL statements to reduce the number of database interactions.
  14. Without using ORDER BY RAND (), alternatively use other methods.
  15. Pagination is recommended to use reasonable methods to increase the efficiency of paging.
  16. Using the number of COUNT (*) recorded in tables, instead of COUNT (primary_key) and COUNT (1).
  17. QUERY is prohibited to perform back office functions and the type of statistics from the library.

Fourth, casual table specifications

  1. Each table is recommended to control the amount of data in less 5000w.
  2. You may be used hash, range, lookup table hash table for binding.
  3. If you use a table scattered md5 (or similar hash algorithm) hash table, the table name suffix hexadecimal, such as user_ff.
  4. CRC32 recommended remainder (or similar arithmetic algorithm) hash table, the table name suffix numbers, numbers must start from 0 and equal width, such as 100 tables scattered suffix from 00-99.
  5. Time casual table, the table name suffix must use a specific format, such as daily casual table user_20110209, monthly bulk table user_201102.

V. Other

  1. Bulk import, export data DBA needs to review and observe the service in the implementation process.
  2. Batch update data such as update, delete operation, the DBA needs to be reviewed, and observe the service in the implementation process.
  3. The product has non-database platform operation and maintenance cause problems and failures, such as front-end station was arrested, please notify the DBA, easy to maintain service stability.
  4. Problems affecting the business sector program bug database services, please notify the DBA, easy to maintain service stability.
  5. Business promotion activities, please notify in advance DBA services and access assessments.
  6. If the business sector appears human errors lead to loss of data, need to recover data, please notify the DBA for the first time, and provide accurate time, statements and other important clues misuse.

 

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

FAQ

1-1. Library name, table names, field names must be lowercase letters, "_" split.

a) MySQL have configuration parameters lower_case_table_names, can not be changed dynamically, Linux system defaults to 0, that is, the name of the database table to store the actual situation, case sensitive. If it is 1, lowercase store, not case sensitive. If it is 2 to store the actual situation, but more in lowercase.

b) If a mixed case, there may coexist abc, Abc, ABC plurality of tables, etc., lead to confusion.

c) display field names are case-sensitive, but does not distinguish between actual use, that is not the same as establishing two names but the case is not the same field.

d) In order to unify specifications, database name, table name, field name in lowercase letters.

 

1-2. Library name, table names, field names must be no more than 12 characters.

Database names, table names, column names up to 64 characters, but for a standardized, easy to identify and reduce the volume must not exceed 12 characters.

 

1-3. Library name, table names, field names intended to see to know the name, it is recommended to use a noun rather than a verb.

a) User comments available usercomment table name or comment.

b) library table is an objective reality of things, kind of object, it is proposed to use the term.

 

1-4. I recommend using InnoDB storage engine.

a) default lead after breaking 5.5, supports transactions, row-level locking, better recovery, better performance under high concurrency, multi-core, large memory, ssd better hardware support.

b) Specific official white paper comparing visible attachment.

 

1-5. Precision floating-point store must use the alternative DECIMAL FLOAT and DOUBLE.

a) the value of the type mysql (not including integer):
    the IEEE754 floating point: float (single-precision), or Double real (double precision)
    fixed points: decimal numeric or
   single precision floating point binary significant digits of 24 bits, according to decimal, the 8-bit; the double precision float 53 significant digits are binary, decimal, is 16-bit
   significant figures a real number of more than 8, if a single-precision floating point number representation, will produce an error ! Similarly, if a real number of significant figures of more than 16, represented by double precision floating point, will produce errors
b) IEEE754 floating-point standard computer, is within the binary representation, but a decimal number to binary when the float, can also cause the error, because not all numbers can be converted into a binary number of finite length.
   That can be accurately converted to a binary decimal, the decimal a decimal binary may not exactly represented.

Example:
drop IF EXISTS Table T;

Create Table T (a float value (10,2));

INSERT INTO T values (131,072.67), (131,072.68);

SELECT value from T;

+-----------+

|value    |

+-----------+

| 131072.67 |

| 131072.69 |

+-----------+

 

1-6. UNSIGNED recommended storing non-negative values.

The same number of bytes stored in a larger range of values. As is -128-127 tinyint signed, unsigned 0-255

 

1- 7. How to use INT UNSIGNED storage ip?

INTUNSIGNED used instead of char (15) to store the ipv4 address conversion is performed by the function inet_ntoa and MySQL inet_aton. Ipv6 address conversion function is currently no need to use two or DECIMAL bigINT stored. E.g:

SELECT inet_aton ( '209.207.224.40');

3520061480

Select INET_NTOA (3520061480);

209.207.224.40

 

1-8. INT [M], M represents what meaning?

Note that the latter type of numerical number in parentheses indicates the width and not only with the relationship stored range, such as INT (3) the default display 3, padded with spaces, beyond the normal display, the Python , the Java client, which do not have this feature.

 

1-10. Does not recommend the use of ENUM, SET type, use TINYINT instead.

a) ENUM, there are three questions: add new value to do DDL, the default value problem (an illegal value into ENUM (that is, outside the allowable string value column), the empty string is inserted as a special error value), the index value problem (actually inserted into the digital value corresponding to the index)

Example:

drop table if exists t;

create table t(sex enum('0','1'));

insert into t values(1);

insert into t values('3');

select * from t;

+------+

| sex  |

+------+

| 0    |

|     |

+------+

2 rows in set (0.00 sec)

 

1-11. Do not use TEXT, BLOB types as possible.

a) index scheduling problem, only the length of the length or specified manually max_sort_length ORDER BY SUBSTRING (column, length) to sort

b) Memory lead break does not support text, blog type, will generate temporary tables on disk

c) may waste more space

d) may not be able to use adaptive hash index

e) led to the use of the statement where no index slow

 

1-13. VARCHAR will produce additional storage you?

VARCHAR (M), <uses a 256 byte length to store, if M> If M = 256 is used to store the length of two bytes.

 

1-14. Table Character Set to select UTF8.

a) the utf8 character set, if Chinese characters, 3 bytes, but ASCII characters or 1 byte.
b) unity, there will be no risk of conversion garbled
c) Users in other regions (USA, India, Taiwan) without having to install Simplified Chinese support, will be able to see your text normal, and will not be garbled
d) ISO-8859-1 encoding (latin1) using all the space within a single byte, transmission and storage of any other encoded byte stream will not be discarded in ISO-8859-1 support system. That is any other byte stream encoded as ISO-8859-1 encoding think there is no problem, save the verbatim byte stream.

 

1-15. VARBINARY memory using variable-length string.

Binary byte stream, there is no coding problems

 

1-18. Why TIMESTAMP is recommended to store the time instead of DATETIME?

TIMESTAMP DATETIME and are accurate to the second, preferred TIMESTAMP, TIMESTAMP because only four bytes, and DATETIME8 bytes. At the same time TIMESTAMP with automatic assignment and automatic update features.

How to use the TIMESTAMP automatically assign attributes?

a) the current time as a default value of ts: ts TIMESTAMP DEFAULTCURRENT_TIMESTAMP.

b) When the row updates, the update value of ts: ts TIMESTAMP DEFAULT 0 ONUPDATE CURRENT_TIMESTAMP.

c) 1 and 2 may be combined: ts TIMESTAMP DEFAULTCURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP.

 

1-19 recommended field is defined as NOT NULL.

a) If the index field is null, the need for additional 1 byte

b) the index, index statistics, comparison becomes more complex value

c) 0 is available, '' instead of

d) If the index field must be defined as not null

 

1-21. Prohibition of the Use VARBINARY, BLOB store pictures, documents in a database and so on.

More efficient use of distributed file system

 

2. Why MySQL performance depends on the index?

MySQL query speed dependent on good index design, so vital to high performance index. Reasonable index will speed up queries (UPDATE, and DELETE including speed, MySQL page will contain the line is loaded into memory, and then UPDATE or DELETE operation), the index will reduce unreasonable speed.

MySQL index to find similar Xinhua Dictionary of phonetic and radical look, when the Pinyin and radical index does not exist, only to find the page by page by page. When MySQL queries can not use the index, MySQL will be a full table scan, it will consume a large amount of IO.

 

2-5. Why not be too much of a table index exists?

InnoDB's secondaryindex using b + tree to store, it is necessary to adjust the b + tree in UPDATE, DELETE, INSERT, when too many indexes will slow down the speed of updates.

 

2-11. EXPLAIN statement

Information on how to EXPLAIN SELECT statement statement (executed in the MySQL client) can get MySQL to perform. Information by performing a SELECT statement EXPLAIN, you can know whether to use the index MySQL when the implementation of the SELECT statement, full table scan, temporary tables, sorting and so on. Try to avoid the MySQL full table scan, using temporary tables, sorting and so on. See the official documentation.

 

2-13 is not recommended to use the prefix% fuzzy queries, such as LIKE "% weibo".

It will lead to full table scan

2-14. How to index VARCHAR field length greater than 50?

The following Table increase a url_crc32, then indexed url_crc32, reduce the length of the index field, and improve efficiency.

  • CREATE TABLE url(

       ……

       url VARCHAR(255) NOT NULL DEFAULT 0,
       url_crc32INT UNSIGNED NOT NULL DEFAULT 0,

       ……

       index idx_url(url_crc32)

    );

 

2-16. What is covered by the index?

InnoDB storage engine, secondaryindex (non-primary key index) is not directly stored in the row address, stores the master key. If the user needs to query data in column secondaryindex not included, you need to look through secondaryindex to the primary key, and then query the data to other primary key column, and therefore needs to query twice.

The concept of covering indexes that the query can be completed in an index, covering index would be more efficient, natural primary key query is covered by the index.

Reasonable to create an index and a reasonable use of the query, when the index can use to cover when performance gains.

For example SELECT email, uid FROM user_email WHEREuid = xx, uid if not the primary key, may be added when appropriate index index (uid, email), to obtain performance improvements.

 

3-3.UPDATE, DELETE statement does not use LIMIT.

a) may result in inconsistent data from the master

b) recorded in the error log, resulting log occupies a lot of space

3-4. Why do you need to avoid the MySQL implicit type conversion?

Because then the implicit type conversion to MySQL, might be converted into an index field type number type = value of the right side, resulting in not using the index, and the reasons for avoiding the use of functions in the index field are similar.

 

3-6. Why is not recommended to use SELECT *?

Increase the number of unnecessary consumption (cpu, io, memory, network bandwidth); increases the possibility of using a covering index; and when the table structure is changed, the preceding also needs to be updated.

 

3-13. How to reduce the number of interactions with the database?

Use the following statement to reduce the number of interactions and the db:

INSERT ... ON DUPLICATE KEY UPDATE

REPLACE

INSERT IGNORE

INSERT INTO values ​​(), () how to use multiple latitudes scattered tables scattered library uses a combination?

Such as micro-blog message, according to the first crc32 (message_id) message will be scattered to the 16 library, then for each table in the library one day generate a new table.

 

3-14. Why not use the ORDER BY rand ()?

Since ORDER BYrand () will read data from the disk, sort, IO and consumes large amounts of CPU, you can obtain a rand value in the program, and by obtaining the corresponding value from the database.

 

3-15. How MySQL, paging?

If there is a similar statement following page:

SELECT * FROM table ORDER BY TIME DESC LIMIT 10000,10;

This paging approach will lead to a lot of io, because MySQL is used to read the policy in advance.

Recommended pagination:

SELECT * FROM table WHERE TIME<last_TIME ORDER BY TIME DESC LIMIT 10.

SELECT * FROM table inner JOIN(SELECT id FROM table ORDER BY TIME LIMIT 10000,10) as t USING(id)

 

 3-17. Why avoid the use of complex SQL?

Refused to use complex SQL, large SQL split into a plurality of fractional simple SQL execution. Reasons: easy to use simple SQL to MySQL QueryCache; reducing lock time in particular MyISAM table; may be used a multi-core CPU.

 

Why 2. InnoDB storage engine to avoid using COUNT (*)?

InnoDB tables to avoid using COUNT (*) operation, counting statistics memcache real-time requirements can be used or the Redis strong, non-real-time statistics may be used alone statistics, updated regularly.

Published 393 original articles · won praise 41 · views 260 000 +

Guess you like

Origin blog.csdn.net/qq_32440951/article/details/88106331