MySQL relevant statute (v1.0)

0) Introduction

a. Basic Statute

[Mandatory] table storage engine must use InnoDB (for the main library is generally mandatory)

[Mandatory] table default character set utf8, when necessary, use utf8mb4 (personal stepped pit: emoji expression storage issues)

  • Description:
    1. GM, without the risk of distortion, 3-byte characters, English 1 byte
    2. utf8mb4 utf8 is a superset of, for example, a memory 4 bytes emoticons, use it

 [Mandatory] prohibit the use of stored procedures, views, triggers, Event

  • Explanation
    1. Greater impact on database performance, internet business, make the site and service layers do and do not find the database layer
    2. Debugging, troubleshooting, migration, are more difficult, poor scalability

[Mandatory] prohibit large files are stored in a database, such as photos, you can be in the object storage system, the database storage path storage of large files

[Mandatory] ban on online database environment to do stress tests

[Mandatory] testing, development, online database environment must be isolated

b.  naming convention

[Mandatory] library name, table names, column names must be in lower case, separated by underscores adopted

  • Description: abc, Abc, ABC is to their gravesites

[Recommended] database names, table names, column names must see to know the name of justice, not longer than 32 characters

  • Description: tmp, wushan TM know who is doing these libraries

[Recommended] backup must bak as a prefix, suffix date

[Recommended] from the library must be suffix -s

[Recommended] prepared by the library suffix must -ss

1) to build the table Statute

[Force] Single Instance number of tables must be controlled within 2000

[] Single table points to force the number of tables must be controlled within 1024

[Mandatory] table must have a primary key, it is recommended to use UNSIGNED integer primary key

  • Description: Potential pit: delete the table without a primary key, if it is the primary mode of row from architecture, from the library will be caught

[Mandatory] prohibit the use of foreign keys, if you want to ensure the integrity, the application shall achieve

  • Description: foreign key so that mutual coupling between the tables, the influence update / delete SQL like properties, may cause a deadlock, it tends to be high concurrency bottleneck database

[Recommended] recommend breaking large field, a field of low access frequency are stored in a separate table, the separation of hot and cold data

[Recommended] use tinyint / int / bigint based on service differentiation, respectively, will take 1/4/8 bytes

[Recommended] using char / varchar based on service differentiation

  • Description:
    1. Fixed field length, or the length of approximately business scenario, suitable char, debris can be reduced, a high performance query
    2. Field length difference between the larger, less or update business scenario, suitable varchar, the space can be reduced

 [Recommended] Use datetime / timestamp based on service differentiation

  • Description: The former occupies 5 bytes, which is 4 bytes, in memory using YEAR, stores the date using DATE, storage time using datetime

 [Mandatory] field must be defined as NOT NULL and set default values

  • Description:
    1. NULL columns using the index, index statistics, value is more complex, MySQL harder to optimize
    2. NULL need more storage space
    3. IS NULL NULL only use or IS NOT NULL, while pit when = /! = / In / not in

 [Mandatory] use INT UNSIGNED storage IPv4, do not use char (15)

 [Mandatory] use varchar (20) to store the phone number, do not use integer

  • Description:
    1. Involves the country code, may appear + / - / () and other characters, for example +86
    2. Phone number will not be used to do the math
    3. varchar Fuzzy queries may be, for example, like '138%'

 [Mandatory] TINYINT instead of using ENUM

  • Description: ENUM add new value to DDL operations

Forced expression of [] is the concept of whether or not the field name must is_xxx manner, the data type is unsigned tinyint (1 is represented, 0 for No).

  • Note: If any of the fields non-negative, must be unsigned.
  • Positive Example: Expression field name is_deleted tombstone, 1 to delete, 0 indicates not deleted.

[Mandatory] table names, field names must be lowercase letters or numbers, start with a number banned, prohibited only the middle two numbers appear underlined. Modify the cost of a large database field names, because they can not carry out a pre-release, so the field names need to be carefully considered.

  • Description: MySQL is not case sensitive in Windows, but by default in Linux is case sensitive. Therefore, the database names, table names, field names, do not allow any uppercase letter, to avoid any further complications.
  • 正例:aliyun_admin,rdc_config,level3_name
  • 反例:AliyunAdmin,rdcConfig,level_3_name

[Mandatory] table name does not use a plural noun.

  • Description: Table name should represent only entity table of contents inside, should not represent the number of entities, DO corresponding to the class name is singular, idiomatic.

[Forced] to disable a reserved word, such as desc, range, match, delayed, please refer to the official MySQL reserved words.

[Mandatory] primary key index named pk_ field names; unique index called uk_ field names; ordinary index name was idx_ field name.

  • Description: pk_ i.e. primary key; uk_ i.e. unique key; idx_ i.e. short index.

[Recommended] decimal type is decimal, prohibit the use of float and double.

  • Description: float and double when stored, there is a loss of accuracy problems are likely when comparing values, get incorrect results. If the stored data exceeds the range decimal range, it is recommended to split into separate data store and decimal integer.

[Force] If the length of a string is almost equal, the use of fixed-length string type char.

[Varchar] mandatory variable length strings, not pre-allocated storage space, a length not more than 5000, if the memory length is greater than this value, defined as the field type text, an independent list, with the corresponding primary key, to avoid affecting other fields efficiency index.

[Mandatory] table must have three fields: id, gmt_create, gmt_modified.

  • Description: id shall wherein the primary key of type unsigned bigint, increment single table, in steps of 1. gmt_create, gmt_modified types are date_time type, the former said in the initiative to create now, the latter past participle passive updates.

[Recommended] named the best table is to add "The role of business name _ the table."

  • 正例:alipay_task / force_project / trade_config

[Recommended] library name and application name as consistent as possible.

[Recommended] If you modify the meaning or status field to field indicates the additional need to update the comment field.

[Recommended] field allows appropriate redundancy to improve query performance, but must be considered consistent data. Redundant field should be followed: 1) is not a field frequently modified. 2) not long varchar fields, but it can not be a text field.

  • Positive Example: Product Category Name high frequency of use, field length is short, the basic static name, category name can be stored in the redundancy table associated, avoiding relational query.

[Recommended] single table rows over 5 million lines or single-table capacity of more than 2GB, it is recommended sub-library sub-table.

  • Note: If the amount of data expected after three years failed to reach this level, you do not sub-library sub-table when creating tables.

[Reference characters] suitable storage length, not only save space database tables, saving index storage, more importantly, to enhance the retrieval speed.

  • Positive Example: in the following table, in which the unsigned value to avoid misdirected negative, and indicates a range expanded.

Objects

Age range

Types of

byte

It represents the range

people

150 years old

unsigned tinyint

1

Unsigned value: 0 to 255

turtle

Hundreds of years old

unsigned smallint

2

Unsigned values: 0-65535

dinosaur fossil

Thousands of years

unsigned int

4

Unsigned values: 0 to about 4.29 billion

sun

About 50 billion years

unsigned bigint

8

Unsigned value: 0 th to 19 about 10

2) Index Statute

[Recommended] the only index to use uniq_ [field name] named

[Recommended] non-unique index to use idx_ [field name] named

[Recommended] recommended that the number of single-table index control in less than five

  • Description:
    1. Internet high concurrency business, too much can affect the write performance index
    2. When the execution plan, if the index is too much, reduce performance, and may result in less than optimal index MySQL Select
    3. Very complex query requirements, you can choose a more suitable ES and other stored

 [Recommended] composite index number field does not recommend more than five

  • Note: If the five fields not greatly narrow the scope row, eight is the design problem

[Recommended] does not recommend the establishment of an index on the field frequently updated

[Recommended] Do not make unnecessary JOIN query, if you want to JOIN query, the field must be the same type of JOIN and index

  • Description: JOIN stepped on the field because of inconsistent type, which led to a full table scan of the pit it?

[Recommended] understanding of the most left-prefix composite index principle, avoid duplication index, if the establishment of (a, b, c), equivalent to the establishment of (a), (a, b), (a, b, c)

[Force] fields having unique operational characteristics, even when a combination of a plurality of fields, a unique index must be completed.

  • Note: Do not think that unique index affects the insert speed, the speed loss is negligible, but to improve the search speed is obvious; In addition, even at the application layer to do a very comprehensive check control, as long as there is no unique index, according to Murphy's Law, there must be dirty data generated.

[Mandatory] prohibits more than three tables join. Need to join the field, data types must be absolutely consistent; multi-table associated with the query, ensure that fields are associated with the need to have an index.

  • Note: Also note that even if the two-table join table index, SQL performance.

[Mandatory] indexing on varchar fields, you must specify the length of the index, no need for indexing the whole field, you can determine the length of an index based on the actual text of discrimination.

  • Description: the length of the discrimination index is a contradiction, the general type of string data length of the index 20, the discrimination will be more than 90%, can count (distinct left (column name, index length)) / count (*) to determine the degree of differentiation.

[Mandatory] search page is strictly prohibited left vague or fuzzy whole, if necessary, please take the search engine to solve.

  • Explanation: The index file with the most left-prefix match the characteristics of the B-Tree, and if the value is left undetermined, it can not use this index.

[Recommended] If there is order by the scene, note the use of the orderliness of the index. The final order by the field is part of the composite index, and the index on a combination of final order, to avoid the appearance file_sort affect query performance.

  • Positive Example: where a = and b = order by c; index:?? A_b_c
  • Anti Example: index has a range of search, then the ordering index can not be used, such as: WHERE a> 10 ORDER BY b; a_b not sorted index.

[Recommended] use an index to cover the query, to avoid back to the table.

  • Note: If you need a book to know what Chapter 11 is titled, will open that page in Chapter 11 corresponding to the directory you look like, this directory is to play the role of covering indexes?. Positive examples: the ability to establish the type of indexes: a primary key index, the only index, the general index, while covering index is an effect of one kind of query, the results explain the use, extra columns will appear: using index.

[Recommended] or using the delay associated with the sub-query optimization over more scene paging.

  • Description: MySQL is not the skip offset lines, but taking offset + N line, and then return to give up before the offset rows, return N rows, that when offset is particularly large, the efficiency is very low, or control the total number of pages returned or on the number of pages exceeds a certain threshold will be rewritten SQL.
  • Positive Example: id to quickly locate the segment needs to be acquired, then the association:. SELECT a * FROM Table 1 a, (select id from Table 1 where 100000,20 conditions the LIMIT) B WHERE  a.id = b.id

[Recommended] SQL performance optimization goal: to reach at least the level range, the requirement is ref level, if you can be consts best.

  • Description:
    1. consts single table can only have a matching row (primary key or unique index), data can be read in the optimization stage.
    2. ref refers to the use of ordinary index (normal index).
    3. range of the index range search.
  • Counter-example: explain the results table, type = index, the index of physical files full scan, speed is very slow, the index level is also relatively low range, and a full table scan is trivial.

[Recommended] build composite index of time, the highest distinction of the far left.

  • Positive example: if where a = and b =, almost close to a column of unique values, then you only need idx_a single index can build??.
  • Description: When the equal sign, and the presence of non-judgment equate mixing conditions, when indexing, please Equation Condition the column predecoder. Such as: where a> and b = even if a higher degree of differentiation, it must b at the forefront of the index??.

[Recommended] prevent implicit conversion caused because of the different field types, resulting in failure of the index.

[Reference] when creating an index to avoid extreme misunderstanding following:

  1. Ninglanwuque. I think a query needs to build an index.
  2. Ningquewulan. Think the index will consume space, a serious slow down the speed of updates and additions.
  3. The only index boycott. The only think that all of the business need to be addressed in the application layer through the "check the plug after the first" approach.

3) the statute inquiry

[Mandatory] prohibit the use of select *, obtain only the necessary fields

  • Description:
    1. select * will increase the cpu / io / memory / bandwidth consumption
    2. Efficient use of the specified field index covering
    3. Queries the specified field, when the table structure changes, to ensure no impact on the application

[Recommended] insert must specify the field, prohibit the use of insert into T values ​​()

  • Explanation: The field is inserted when the table structure changes, to ensure no impact on the application

[Mandatory] implicit type conversion index will fail, resulting in a full table scan

[Mandatory] prohibit the use of the column function or expression where condition

  • Description: The cause can not hit the index, a full table scan

[Mandatory] prohibits the beginning of the negative fuzzy query to the query and%

  • Description: The cause can not hit the index, a full table scan

[Mandatory] JOIN prohibit large tables and subqueries (non Offline large database)

[Recommended] with OR on a field must be asked to rewrite IN, IN value must be less than 50

[Recommended] The application must capture SQL Exception

  • Description: easy to locate online issue

[Mandatory] Do not use the count (column name) or count (constant) instead of count (*), count (*) syntax is the number of rows SQL92 standard statistical definition, has nothing to do with the database, nothing to do with NULL and non-NULL.

  • Description: count (*) counts the value NULL row, while count (column name) does not count rows in this column is NULL values.

[Force] count (distinct col) calculates the number of columns is not repeated rows other than NULL, the note count (distinct col1, col2) If a whole is NULL, then even if another row have different values, but also returns to zero.

[Mandatory] When the value of a column of all time NULL, count (col) returns a value of 0, but the sum (col) returns a value of NULL, so using sum () takes note NPE problem.

  • Positive Example: in the following manner may be used to avoid problems NPE sum of: SELECT IF (ISNULL (SUM (g)), 0, SUM (g)) FROM table;

[Force] using the ISNULL () to determine whether a NULL value.

  • Description: NULL direct comparison with any value will be NULL.
  • NULL <> NULL result returned is NULL, and not false.
  • NULL = NULL returns the result is NULL, rather than true.
  • NULL <> 1 results returned is NULL, and not true.

When [forced] to write paging query logic in code, if the count is zero should be returned directly to avoid the implementation of pagination statement back.

[] Shall not be used to force a foreign key with a cascade, all foreign key concepts that must be addressed in the application layer.

  • Description: The relationship between student achievement and example, students student_id table is the primary key, then the results table student_id was foreign keys. If you update the student table student_id, while triggering the results table student_id update, that is, cascading updates. Foreign keys with cascading updates for stand-alone low concurrency, not suitable for distributed, highly concurrent cluster; cascade update is strong blocker, there is a risk database update storm; insert foreign keys affect the speed of the database.

[Mandatory] prohibit the use of stored procedures, stored procedures, difficult to debug and extend, and no portability.

When [mandatory] data revision, when you delete and modify records, first select, to avoid accidentally deleted, confirmation in order to perform the update statement.

[Recommended] in operation can be avoided, avoid, if it can not avoid the need to carefully assess the number of elements in the set back control in the 1000's.

[Reference] If there is need for globalization, all the characters are stored and representation, are utf-8 encoding, note the difference between the character of statistical functions.

  • Description:
    • SELECT LENGTH ( "light work"); return 12
    • SELECT CHARACTER_LENGTH ( "light work"); return to 4 if needed to store expressions, then select utfmb4 for storage, pay attention to it and utf-8 encoded difference.

[Reference] TRUNCATE TABLE is faster than DELETE speed, and fewer system and transaction log resources used, but TRUNCATE no transaction does not trigger trigger, may cause an accident, it is not recommended to use this statement in the development of the code.

  • Description: TRUNCATE TABLE and DELETE statement with no WHERE clause is identical in functionality.

 

References:

1) https://mp.weixin.qq.com/s/YfCORbcCX1hymXBCrZbAZg

2)https://yq.aliyun.com/articles/73861

Guess you like

Origin www.cnblogs.com/Terry-Wu/p/11609743.html