MySQL database design principles

table of Contents

Core principles

Field class principle

Index principle

SQL-like principles


 

Core principles

 

1. Do not perform calculations in the database;
2. Move the CPU calculation to the business layer;
3. Control the number of columns (fields are few and precise , the number of fields is recommended to be within 20);
4. Balance paradigm and redundancy (efficiency first; often sacrifice paradigm )
5. Reject 3B (reject big SQL statement: big sql, reject big transaction: big transaction, reject big batch: big batch);

 

 

Field class principle


1. Use good numeric types (use suitable field types to save space);
2. Convert characters to numbers (the best conversion that can be converted, also save space and improve query performance);
3. Avoid using NULL fields (NULL fields are difficult Query optimization, the index of the NULL field requires additional space, and the compound index of the NULL field is invalid);
4. Use less text type (try to use varchar instead of the text field);

 

 

Index principle


1. Use the index reasonably (improve the query, slow down the update, the index must not be as much as possible);
2. The character field must build a prefix index (specify the data type range of the index field)

ALTER TABLE `city_demo` ADD KEY `idx_city` (`city`(7))

3. do not in the index column operation;
4.innodb recommend the use of an auto-increment primary key (primary key build clustered index, primary key should not be modified, the string should not be a primary key) (understanding the structure of the index save Innodb will know);
5 . No foreign key (restricted by program guarantee);

 

 

SQL-like principles

 

1. The sql statement is as simple as possible (one sql can only operate on one cpu, the big statement splits the small statement, reducing the lock time, one big sql can block the entire library);
2. simple transactions;
3. avoid using trig / func (Triggers and functions are not replaced by client programs);
4. No select * (consumption of CPU, io, memory, bandwidth, this program is not scalable);
5. OR is rewritten as IN (or efficiency is n level) ;
6.OR rewritten as UNION (mysql index merge is very mentally handicapped);

select id from t where phone = ’159′ or name = ‘john’;
select id from t where phone=’159′
union
select id from t where name=’jonh’

7. Avoid negative%;
8. Use count (*) with caution;
9.limit efficient paging (the greater the limit, the lower the efficiency);
10. Use union all instead of union (union has deduplication overhead);
11. Use less Connect join;
12. Please use the same type comparison;
13. Break up batch updates;

Published 568 original articles · Like 180 · Visits 180,000+

Guess you like

Origin blog.csdn.net/Delicious_Life/article/details/105611748
Recommended