当前位置:网站首页>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的性能.
所以,索引好用,适度即可.
知识点总结:
边栏推荐
- Pytest first experience
- 【MATLAB项目实战】LDPC-BP信道编码
- 简单的vim配置
- TypeScript 的组件
- [QNX Hypervisor 2.2用户手册]9.15 suppress
- [Cloud Residency Co-Creation] [HCSD Big Celebrity Live Broadcast] Personally teach the secrets of interviews in big factories
- SQL injection Less42 (POST type stack injection)
- 22年8月推广大使额外奖励规则
- To help the construction of digital government, the three parties of China Science and Technology build a domain name security system
- I don't know what to do with sync issues
猜你喜欢
Pytest初体验
SVN服务器搭建+SVN客户端+TeamCity集成环境搭建+VS2019开发
SVN server construction + SVN client + TeamCity integrated environment construction + VS2019 development
(26) About menu of the top menu of Blender source code analysis
NIO programming
【MATLAB项目实战】LDPC-BP信道编码
Design of Fire and Anti-theft System Based on Single Chip GSM
嵌入式开发没有激情了,正常吗?
How to reduce the gap between software design and implementation
手写一个简单的web服务器(B/S架构)
随机推荐
网易云信圈组上线实时互动频道,「破冰」弱关系社交
[Cloud Residency Co-Creation] [HCSD Big Celebrity Live Broadcast] Personally teach the secrets of interviews in big factories
NIO编程
什么时候可以使用 PushGateway
SQL注入 Less47(报错注入) 和Less49(时间盲注)
Flutter教程之 01配置环境并运行demo程序 (教程含源码)
2022-07-31:给出一个有n个点,m条有向边的图, 你可以施展魔法,把有向边,变成无向边, 比如A到B的有向边,权重为7。施展魔法之后,A和B通过该边到达彼此的代价都是7。 求,允许施展一次魔法
Handwritten a simple web server (B/S architecture)
Flink 1.13(八)CDC
Unity - by casting and cloning method dynamic control under various UGUI create and display
cobaltstrike
基于RT1052 Aworks nanopb string 类型固定长度使用方式(二十七)
Payment module implementation
Components of TypeScript
标段参数说明
什么是动态规划,什么是背包问题
简单的vim配置
The difference between adding or not adding the ref keyword when a variable of reference type is used as a parameter in a method call in C#
Input and output optimization
新产品如何进行网络推广?