当前位置:网站首页>Carefully organize 16 MySQL usage specifications to reduce problems by 80% and recommend sharing with the team
Carefully organize 16 MySQL usage specifications to reduce problems by 80% and recommend sharing with the team
2022-07-31 23:37:00 【One Light Architecture】
携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第3天,点击查看活动详情
The previous article explained how to create a suitable oneMySQL索引,Learn how to be more standardized today、更合理的使用MySQL?
合理规范的使用MySQL,It can greatly reduce the development workload and online problems,并提升SQL查询性能.
I summed it up nicely16条MySQL规约,分享给大家,欢迎评论指正.
1. 禁止使用select *
阿里开发规范中,有这么一句话:
select (*) All fields in the table will be queried,If the fields in the table have changed,必须修改SQL语句,不然就会执行错误.
Query out unnecessary fields,increase diskIO和网络延迟.
2. 用小表驱动大表
关联查询的时候,Use the small table to check the results first,Then use the results to query the large table,The number of connections can be greatly reduced.
For example, we want to query the employees under a certain department,Because the number of departments is far less than the number of employees.We can think of the department table as the driver table,The employee table is used as the driven table.
查询SQL类似这样:
select * from department
inner join employee
on department.id=employee.department_id
where department_name='部门1';
复制代码
3. joinAssociation tables should not be too many
joinAssociation tables are prohibited from exceeding3张,joinToo many associations,Not only will it increase the query time,降低查询性能,Temporary tables are also generated to cache the result data,It is recommended to break it into several small piecesSQL执行.
In addition, the types of the associated fields must be consistent,And in each table to establish the index of the associated field.
4. Left-fuzzy or full-fuzzy queries are prohibited
当我们在SQLWhen the query uses left-fuzzy or full-fuzzy matching,类似下面这样:
# 左模糊查询
select * from user where name='%一灯';
# 全模糊查询
select * from user where name='%一灯%';
复制代码
根据B+树的特性,即使我们在name字段上建立了索引,The index cannot be used when querying.
5. The index access type is at least up torange级别
There are several levels of common index access types,从上到下,性能由好到差.
要求SQLThe index access type is at least up to range级别,最好到const级别.
6. More elegant use of union indexes
Because the joint index has the leftmost matching principle,Therefore, it is necessary to prioritize the fields with high discrimination in the first column on the left.
For example, to count the distinction between the birthday field and the gender field in the user table,可以这样统计:
select
count(distinct birthday)/count(*),
count(distinct gender)/count(*)
from user;
复制代码
值越大,区分度越高.
Debut interview questions,下面这条SQLHow to create a joint index:
select a from table_name where b=1 order by c;
复制代码
SQL中用到abc三个字段,The order in which the union index is created is**(b,c,a)**.
This question also involves another point of knowledge,SQL执行的顺序:
from > on > join > where > group by > having > select > distinct > order by > limit
7. Be careful to avoid deep paging
MySQL深分页的时候,查询性能较差.
select * from user where name='一灯' limit 10000,10;
复制代码
We can use subqueries to optimize:
select * from user
where id in (
select id from user
where name='一灯'
limit 10000,10
);
复制代码
This can reduce the number of non-clustered index back-to-table queries.
8. Do not exceed single table fields30个
When there are too many fields in a single table,Loading large amounts of data can also slow down query performance.
If the field exceeds30个,不用看,It must be the unreasonable design of the table.
这时候,Can be split into multiple tables,Use a vertical split table,Separate hot and cold fields.
9. Do not use character types for enumeration fields
Character types take up more storage space,When we want to store enum values or indicate whether or not,可以采用tinyint数值类型,It is better to use unsigned integersunsigned tinyint.
10. Decimal types are prohibitedfloat和double
when storing and computing,float 和 double 都存在精度损失的问题,无法得到正确的结果.
So when it comes to storing decimals,必须使用decimal类型.
11. All fields must have default values set and are not allowednull
字段允许为null,会占用额外的存储空间.
Indexing does not indexnull值,所以查询nullThe index cannot be used when the value is used.
when numeric types are allowednull,When returning to the mapped entity class, a null pointer exception may also be reported.
12. A primary key must be created,Preferably an ordinal numeric type
If we do not set the primary key for the table ourselves,InnoDBA hidden primary key column is automatically added,We can't use it,And it also takes up more storage space,所以建表的时候,必须设置主键.
Ordinal values are more suitable for primary keys,插入数据的时候,由于是有序的,Not frequently adjustedB+树结构,性能更好.
13. Quickly determine whether a record exists
Generally, when we judge whether a record exists in the table,会使用count函数,然后判断返回值是否大于1.
select count(*) from user where name='一灯';
复制代码
InnoDBThe storage engine is not like thatMyIsAmThat way the total row count of the table is cached,Every query is calculated in real time,耗时较长.
我们可以采用limit加快查询效率:
select id from user where name='一灯' limit 1;
复制代码
limit 1Indicates that a match will be returned,The query efficiency is better,The result set is just returnedid,Covering indexes can also be used.
14. inThe number of conditions should not be too large
inThe number of conditions should not exceed1000个,Otherwise, it will take a long time,It can be split into multiple batches of queries.
15. Creation of reserved fields is prohibited
It is not possible to determine what this field is used for by the name of the reserved field.
Types of reserved fields are not necessarily appropriate.
Unable to create suitable index for reserved field.
16. Do not exceed the number of single table indexes5个
Creating appropriate indexes can improve query efficiency,But too many indexes,Not only does it take up more storage space,It will also slow down the updateSQL的性能.
所以,索引好用,适度即可.
知识点总结:
边栏推荐
猜你喜欢
随机推荐
Payment module implementation
[QNX Hypervisor 2.2用户手册]9.15 suppress
Dry goods | 10 tips for MySQL add, delete, change query performance optimization
消息队列消息存储设计(架构实战营 模块八作业)
Interview Blitz 69: Is TCP Reliable?Why?
Network security - crack WiFi through handshake packets (detailed tutorial)
SQL注入 Less42(POST型堆叠注入)
Program processes and threads (concurrency and parallelism of threads) and basic creation and use of threads
UOS - WindTerm use
一文带你了解 Grafana 最新开源项目 Mimir 的前世今生
C#中引用类型的变量做为参数在方法调用时加不加 ref 关键字的不同之处
编译型语言和解释型语言的区别
NgRx 里 first 和 take(1) 操作符的区别
"SDOI2016" Journey Problem Solution
如何撰写出一篇优质的数码类好物推荐文
"SDOI2016" Journey Problem Solution
Weekly Summary
IJCAI2022 | 代数和逻辑约束的混合概率推理
Shell常用脚本:Nexus批量上传本地仓库增强版脚本(强烈推荐)
硬件设备计算存储及数据交互杂谈