Mysql military upgrade version

1. Basic Specifications

  • Table storage engine must use InnoDB
  • The table character set uses utf8 by default, and utf8mb4 if necessary
    • Interpretation:
      (1) Universal, no risk of garbled characters, 3 bytes for Chinese characters, 1 byte for English
      (2) utf8mb4 is a superset of utf8, use it when storing 4 bytes such as emojis
  • Prohibit the use of stored procedures, views, triggers, events
    • Interpretation:
      (1) It has a great impact on the performance of the database. Internet business, which can be done by the site layer and the service layer, should not be handed over to the database layer
      . (2) It is difficult to debug, debug, and migrate, and has poor scalability.
  • It is forbidden to store large files in the database, such as photos, you can store large files in the object storage system, and store the path in the database
  • It is forbidden to do database stress testing in the online environment
  • Test, development, and online database environments must be isolated

Second, the naming convention

  • Library name, table name, column name must be in lowercase, separated by underscore
    • Interpretation: abc, Abc, ABC are all buried for themselves
  • The library name, table name, and column name must be well-known, and the length should not exceed 32 characters
    • Interpretation: tmp, wushan who TM knows what these libraries are for
  • The database backup must be prefixed with bak and suffixed with the date.
    The slave database must be suffixed with -s.
    The standby database must be suffixed with -ss.

3. Table Design Specifications

  • The number of single-instance tables must be controlled within 2000
  • The number of sub-tables in a single table must be controlled within 1024
  • The table must have a primary key, it is recommended to use an UNSIGNED integer as the primary key
    • Potential pit: delete a table without a primary key, if it is a master-slave architecture in row mode, the slave library will hang
  • Foreign keys are prohibited and should be implemented by the application if integrity is to be guaranteed
    • Interpretation: Foreign keys make tables coupled with each other, affect SQL performance such as update/delete, and may cause deadlocks. It is easy to become a database bottleneck under high concurrency.
  • It is recommended to split large fields and fields with low access frequency into separate tables for storage, and separate hot and cold data

4. Column Design Specifications

  • Use tinyint/int/bigint according to business distinction, which will occupy 1/4/8 bytes respectively
  • Use char/varchar according to business distinction
    • Interpretation:
      (1) For business scenarios with fixed field lengths or similar lengths, it is suitable to use char, which can reduce fragmentation and improve query performance
      (2) For business scenarios with large differences in field lengths or fewer updates, it is suitable to use varchar, which can reduce space
  • Use datetime/timestamp according to business distinction
    • Interpretation: the former occupies 5 bytes, the latter occupies 4 bytes, YEAR is used to store the year, DATE is used to store the date, and datetime is used to store the time.
  • Fields must be defined as NOT NULL and set to default
    • Interpretation:
      (1) NULL columns use indexes, index statistics, and values ​​are more complex, and MySQL is more difficult to optimize
      (2) NULL requires more storage space
      (3) NULL can only use IS NULL or IS NOT NULL, and in = There is a big pit when /!=/in/not in
  • Use INT UNSIGNED to store IPv4, don't use char(15)
    • select inet_aton ('1.1.1.1');
      select inet_ntoa (16843009);
  • Use varchar(20) to store phone numbers, don't use integers
    • Interpretation:
      (1) Involving the country code, characters such as +/-/() may appear, such as +86
      (2) The mobile phone number will not be used for mathematical operations
      (3) varchar can be used for fuzzy query, such as like '138%'
      -- 使用下面语句进行模糊查询
      select name from user where position('an' in name);
      select name from user where locate('an',name)>0;
      ​​​​​​​select name from user where instr(name,'an')>0;
  • Use TINYINT instead of ENUM
    • Interpretation: ENUM adds new values ​​to DDL operations

5. Index Specification

  • Unique indexes are named using uniq_[fieldname]
  • Non-unique indexes are named using idx_[field name]
  • The number of indexes in a single table is recommended to be controlled within 5
    • Interpretation:
      (1) For high-concurrency services on the Internet, too many indexes will affect the write performance
      (2) When generating an execution plan, if there are too many indexes, the performance will be reduced, and MySQL may not be able to select the optimal index
      (3) Very complicated For query requirements, you can choose more suitable storage methods such as ES
  • The number of combined index fields is not recommended to exceed 5
    • Interpretation: If the 5 fields can not greatly reduce the row range, 80% is a design problem
  • Indexing on frequently updated fields is not recommended
  • Do not perform JOIN query unless necessary. If you want to perform JOIN query, the fields to be JOIN must be of the same type and indexed.
    • Interpretation: Have you stepped on the pit of full table scan due to inconsistent JOIN field types?
  • Understand the leftmost prefix principle of combined index and avoid repeated index construction. If (a,b,c) is established, it is equivalent to establishing (a), (a,b), (a,b,c)

6. SQL Specification

  • Forbid the use of select *, only get the necessary fields
    • Interpretation:
      (1) select * will increase the consumption of cpu/io/memory/bandwidth
      (2) the specified field can effectively use the index coverage
      (3) the specified field query, when the table structure is changed, can guarantee no impact on the application
  • insert must specify a field, prohibit the use of insert into T values()
    • Interpretation: Inserting a specified field can ensure that it has no impact on the application when the table structure is changed.
  • Implicit type conversion will invalidate the index, resulting in a full table scan
  • Disallow the use of functions or expressions in where conditional columns
    • Interpretation: cause the index cannot be hit, full table scan
  • Negative queries and fuzzy queries starting with % are prohibited
    • Interpretation: cause the index cannot be hit, full table scan
  • Large table JOINs and subqueries are prohibited
  • OR on the same field must be rewritten to ask IN, and the value of IN must be less than 50
  • Applications must catch SQL exceptions
    • Interpretation: Easy to locate online problems

Note : This military regulation is suitable for typical Internet services with large concurrency and large amount of data. You can take it directly for reference, thank you.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325002053&siteId=291194637