How much do you know about database design principles and development specifications?

VOL 187

21

2020-12

Today is 11 days before 2021

This is the 187th tweet of ITester Software Testing Stack

Click on the top blue word " ITester software testing Flowering " I am concerned, every morning  08: 3 0 punctual push each month from time to time presented technical books .

WeChat official account backstage responds to " resources " and " test kit " to receive test resources, and responds to " WeChat exchange group " and " introduction group " to join the group to fight monsters.

This article is 4352 words, about 11 minutes to read

For a program, due to the absolute preciousness of database resources (cannot be expanded indefinitely), the database must be used reasonably. To ensure efficient, stable and safe operation. If the database is well designed, the system will be fast; if the database is not well designed, the system will stagger. Today we will introduce the design principles and specifications of the database. The more you know, the more you don’t know. How much do you know?

One

MySQL database design principles

1

Core principles

1. Do not do operations in the database;

2. The cpu calculation must be moved to the business layer ecs;

3. Control the number of columns (the number of fields is small and precise, the number of fields is recommended to be within 60);

4. Balance paradigm and redundancy (efficiency first; often sacrifice paradigm, but not excessive redundancy);

5. Reject large SQL statements, large things, and large batches;

2

Database principles

  

1. It is recommended that 单库no more than 100 tables;

2. It is recommended that the single meter 数据量does not exceed 100w;

3. It is best not to store important data and unimportant data in a library;

4. If the database field has Chinese, it is recommended to be 3 times the length of the field in the interface;

3

Basic table principle

  

The basic table is different from the intermediate table and temporary table because it has the following four 特性:

  • Atomicity: The fields in the basic table are not decomposable;

  • Originality: The records in the basic table are records of the original data (basic data);

  • Deductive: All output data can be derived from the data in the basic table and code table;

  • Stability: The structure of the basic table is relatively stable, and the records in the table must be stored for a long time;

4

Field class principle

  

1. Try to use numeric types (use appropriate field types to save space);

2. Convert characters to numbers (the best conversion that can be converted, which also saves space and improves query performance);

3. Try to use not null;

4. Use less texttypes (try to use varchar instead of text fields);

5. The field needs a clear comment;

6. It is recommended that the upper limit of the number of fields in a single table be controlled at 20-50;

7. It is forbidden to use different data types for the same field between different tables;

5

Index principle

 

1. Reasonable use of indexes (improve queries, slow down updates, and more indexes must not be better);

2. A prefix index must be built for character fields;

3. Do not perform column operations on the index;

4. Innodb primary key is recommended 自增列;

5. It is forbidden to use foreign keys (constrained by program guarantee);

6. Try not to use unique constraints (unable to use mysql, insert buffer, resulting in slow insert performance);

7. Large fields are not suitable for indexing, partition tables cannot use full-text indexing;

6

Business principles

1. Decoupling, minimize mutual dependence between modules and call each other;

2. Decoupling, it is forbidden to reuse the same query interface between interfaces;

3. Ensure the atomicity and consistency of transactions;

4. Ensure data integrity, security, and effectiveness, prevent illegal users from using the database or legal users illegally using the database to cause data leakage, modification or destruction, prevent cross-use by users of different modules, and encrypt sensitive data early;

5. The principle of scalability and scalability: the design of the database structure should fully consider the needs of development and transplantation, and has good scalability, scalability and moderate redundancy;

6. Standardization: The database design should follow the standardization theory. Standardized database design can reduce abnormalities and errors during database insertion, deletion, and modification operations, and reduce data redundancy;

7

SQL class principles

1. The sql statement is as simple as possible;
2. A simple transaction;

3. No need to select * (consumption of cpu, io, memory, bandwidth, this kind of program is not scalable);

4.OR is rewritten as IN (or efficiency is n level);
5.OR is rewritten as UNION;

6. Avoid negative %;

7. Use count(*) with caution;

8. Limit efficient paging (the larger the limit, the lower the efficiency);

9. Use union all instead of union (union has deduplication overhead);

10. Use less joins;

11. Use group by;
12. Please use the same type comparison;
13. Break up batch updates;

8

Clever use of performance analysis tools

1.show profile;
2.mysqlsla;
3.mysqldumpslow;
4.explain;
5.show slow log;
6.show processlist;

two

MySQL database development specifications

1

Common rules

1. The principle of least return, the database query to return client data requires network transmission overhead. You may not need to read these columns, but because laziness is written as SELECT *, the memory buffer pool is really buffered by these "garbage" data. Hot data has been washed out. Cause other query performance to slow down;

2. The one-to-one principle, each query interface is connected to only one application interface, and the same query interface is not allowed to be reused. This has a huge impact on the later stage, and the seesaw response;

3. Implicit conversion, the use of implicit conversion is prohibited, which may bring some surprises;

4. Try to avoid the situation of full table scan;

5. Avoid the association of more than three tables;

6. For large table queries, DBA needs to be notified in advance;

7. Write in the order of index in the where condition;

8. Core queries must use indexes, and covering indexes are preferred;

9. The hash index is valid only when it is equal;

10. Frequently use EXPLAIN diagnosis to avoid generating temporary tables and file system sorting;

11. Use large table paging with caution;

12. Try not to query too much data at once and narrow the scope of the query;

13. The sorting field must belong to the driving table in order to use the index on the driving table to complete the sorting;

2

Index taboos and restrictions

1. Do not create new indexes on columns with low dispersion;

2. Do not perform operations on the index, function operations;

3. Like wildcard at the beginning, the index cannot be used;

4. Type conversion, index failure;

5. Be cautious about range query, only the first column can really use the index;

6. The index should not be too many, the index improves the query performance, but reduces the insert and update performance;

7. The index is updated as little as possible. If it reaches a certain level, the optimizer will give up the index and go through the full table scan;

8."!=", "!>", "!<", "NOT", "NOT EXISTS", "NOT IN", "NOT LIKE",like %aaa%‟ 尽量不用;

3

DML

1. It is forbidden for a single SQL to update multiple tables;

2. All updates must use the index (to achieve high concurrency, reduce lock resource contention);

3. The use of delete is prohibited, and the program user has no delete permission;

4. It is forbidden to use limit in DML;

4

DDL

1. For DDL, please use the Yearning work order. Yearning has an automatic DDL review mechanism and will automatically choose whether to use online or third-party tools;

2. If multiple DDLs can be merged, try to merge them (to reduce the cost);

3. DDL is arranged in the evening;

5

TCL

1. For the core part, please use transactions to ensure data consistency;

2. Non-core part of the business, can split large transactions, reduce the lock time;

6

Typesetting and injection

1. Please format SQL statements, Navicat can beautify SQL, the general principle is to make the code clear and readable;

2. The comments are as detailed and comprehensive as possible, and the comments are placed in front of the implementation code to improve readability;

three

MySQL database design specification

1

Database naming convention

1. Use 26 English letters (case sensitive) and 0-9 natural numbers (often not needed) plus an underscore _ as the database name;
2. The name should be concise and clear (the length cannot exceed 30 characters);
for example: user, stat, log, wifi_user, wifi_log, wifi_stat can also add a prefix to the database;

3. Unless it is a backup database, you can add a natural number of 0-9: user_db_2021220;

2

Data table naming convention

1. Basic table: It is composed of 26 English letters and natural numbers 0-9 plus an underscore'_'. The naming is as concise and clear as possible. Multiple words are separated by underscore'_'. The same piece of business must be consistent at the beginning. When you see the table name, you know the source of the table;

例如:user_login, user_profile, user_detail, user_role, user_role_relation,user_role_right, user_role_right_relation;

2. Relationship table: For example, start with rel_ or end with _rel, the name is concise and clear, and the relationship is clear at a glance;

3. The table must have a clear comment;

4. The series table must be consistent at the beginning;

3

Database field name naming convention

1. It is composed of 26 English letters and natural numbers 0-9 plus underscore'_', generally no more than 3 words;
2. The name is concise and clear, and multiple words are separated by underscore'_';
for example: user_login table field user_id , user_name, pass_word, eamil, status, mobile, add_time;
3. Each table must have an auto-incrementing primary key, add_time (default system time);
4. The names of related fields between tables must be as the same as possible;

5. Each table must contain fixed fields, logical deletion, creation time, creator, modification time, modification person, remarks;

6. Each field must clearly state the comment, and update the comment in time when it is modified;

7. Put the columns with high dispersion forward, create the index according to the order of the columns, and query according to the order of the columns, keeping the three consistent;

8. Do not use MySQL keywords, reserved words;

4

Database field type specification

1. Use as little storage space as possible to store the data of a field;
for example: if you can use int, don’t use varchar, char, if you can use varchar(16), don’t use varchar(256);
2. IP address is best to use int type;
3. It is best to use char for fixed-length types, such as zip code;
4. If you can use tinyint, do not use smallint, int;
5. It is best to give each field a default value, preferably not null;

5

Data table index specification

1. The naming is concise and clear, for example: the index of the user_name field of the user_login table should be the user_name_index unique index;
2. Create a primary key index
for each table ; 3. Create a reasonable index for each table;
4. Please be cautious when building a composite index;

6

Three paradigms of database

First Normal Form (1NF): The field value is atomic and cannot be divided (all relational database systems meet the first normal form);
for example: name field, where the surname and first name are a whole, if you distinguish between surname and first name, you must set it up Two independent fields;

Second Normal Form (2NF): A table must have a primary key, that is, each row of data can be uniquely distinguished;
Note: The first normal form must be satisfied;

Third Normal Form (3NF): A table cannot contain the information of non-key fields in other related tables, that is, the data table cannot have redundant fields;
Note: The second normal form must be satisfied first;

the above


That‘s all

More series articles

Stay tuned


ITester software testing stack (ID: ITestingA), focuses on software testing technology and treasure dry goods sharing, updates original technical articles on time every week, and gives technical books irregularly every month. Please indicate the source for third-party reprints.

ITester software testing stack

Past content favored

1. Python interface automation-interface basics (1)


2. Python interface automation-interface basics (2)


3. Python interface automation-requests module get request


4. Python interface automation-requests module post request


5. Cookie and session application for Python interface automation


6. Detailed explanation and application of Token for Python interface automation


7. Python interface automation requests request packaging


8. Python interface automation of pymysql database operation


9. Python interface automation logging log


10. Python interface automation logging package and actual combat

Want to get more latest dry goods content

Come star and follow me

See you at 08:30 every Monday, Wednesday and Friday

<< Swipe to view the next picture >>


 Backstage  reply "resources" to take dry goods

Reply to " WeChat Group" to fight monsters and upgrade

Personal WeChat: Cc2015123

Please indicate your intention to add :)

True love Silian, gain more knowledge~

Guess you like

Origin blog.csdn.net/weixin_42485712/article/details/111503482