Army Regulation MySQL upgrade version (rpm)

First, the basic norms

  • You must use the InnoDB storage engine tables
  • Table default character set utf8, when necessary, use utf8mb4

Interpretation:
(1) general, the risk of distortion-free, three-byte characters, English one byte
(2) utf8mb4 utf8 is a superset of, for example, a memory 4 bytes emoticons, use it

  • Prohibit the use of stored procedures, views, triggers, Event

Interpretation:
(1) a 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

  • Prohibit large files are stored in a database, such as photos, large files can be stored in the object storage system, the database storage path
  • Prohibition database environment do stress testing on-line
  • Testing, development, online database environment must be isolated


Second, the naming convention

  • Library name, table names, column names must be in lower case, separated by underscores adopted

Interpretation: abc, Abc, ABC is to their gravesites

  • Database names, table names, column names must see to know the name of justice, not longer than 32 characters

Interpretation: tmp, wushan TM know who is doing these libraries

  • Database backup must bak as a prefix, suffix date
  • From the library must be suffix -s
  • Library equipment must -ss suffix


Third, the design rules of Table

  • The number of single-instance table must be controlled within 2000
  • Single table in the number of part tables must be controlled within 1024
  • The table must have a primary key, it is recommended to use UNSIGNED integer primary key
  • 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

 

  • Prohibit the use of foreign keys, if you want to ensure the integrity, the application shall achieve

Interpretation: 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

  • Recommend breaking large field, a field of low access frequency are stored in a separate table, the separation of hot and cold data

Interpretation: Specific participate in the "how to implement database vertical split"

four column design specifications

  • The service differentiation using tinyint / int / bigint, respectively 1/4/8 byte occupies
  • The service differentiation using char / varchar

Interpretation:
(1) a 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, can be reduced space

  • Use datetime / timestamp service differentiation according to

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

  • The field must be defined as NOT NULL and set default values

Interpretation:
(. 1) using a column NULL index, index statistics, values are more complex, MySQL harder optimization
(2) NULL requires more storage space
(3) NULL only using IS NULL or IS NOT NULL, in = /! = / in / not have time in the pit

  • Use INT UNSIGNED storage IPv4, do not use char (15)

 

  • Use varchar (20) to store the phone number, do not use integer

Interpretation:
(1) involve the country code, may appear + / - / () characters such as, for example, +86
(2) phone number will not be used to do the math
(3) varchar can be fuzzy query, for example, like '138%'

  • TINYINT instead of using ENUM

Interpretation: ENUM add new value to DDL operations

V. index specification

  • The only index to use uniq_ [field name] named
  • Use non-unique index idx_ [field name] named
  • Single table index number of recommended control in less than five

Interpretation:
(1) high concurrency Internet 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 choice MySQL index
(3) unusually complex query needs, you can choose a more suitable ES and other stored

  • Composite index number field does not recommend more than five

Interpretation: If the five fields not greatly narrow row range Eighty per cent of the design problem

  • Not recommended based on field frequently updated index
  • Do not make unnecessary JOIN query, if you want to JOIN query, the field must be the same type of JOIN and index

Interpretation: Because stepped inconsistent JOIN field type, which led to a full table scan of the pit it?

  • The most left-prefix understand the principle of the composite index, avoid duplication index, if the establishment of (a, b, c), equivalent to the establishment of (a), (a, b), (a, b, c)


Six, SQL specification

  • Prohibit the use of select *, only to obtain the necessary field

Interpretation:
(1) the SELECT * will increase the cpu / io / memory / bandwidth consumption
(2) effective use of the specified field index covering
(3) specify the query field, when the table structure changes, to ensure no impact on the application

  • insert fields must be specified, prohibit the use of insert into T values ​​()

Interpretation: the specified field is inserted when the table structure changes, to ensure no impact on the application

  • Implicit type conversion index will fail, resulting in a full table scan

 

  • Prohibit the use of the column function or expression where condition

Interpretation: cause can not hit the index, a full table scan

  • Prohibit fuzzy query negative at the beginning of the inquiry and%

Interpretation: cause can not hit the index, a full table scan

  • JOIN prohibit large tables and subqueries
  • OR same field must be rewritten on a Q IN, IN, the value must be less than 50
  • Applications must catch SQL exceptions

Interpretation: easy to locate online issue

Description : This military regulations applicable to concurrent large , large amount of data typical of the Internet business , reference can be directly taken away, do not mention it.

Army regulations Exercise: Why can not hit the phone following SQL index?
select uid from user where phone = 13811223344

Guess you like

Origin www.cnblogs.com/chenlinlab/p/11610066.html