当前位置:网站首页>sql 常用优化
sql 常用优化
2022-07-07 17:52:00 【whiteye太白】
sql 优化
1. 小表驱动大表
先执行查询数据少的表,再执行查询数据多的表。
2. 建索引,一张表不超过5个索引
避免大量内存占用。
3. 走索引,尽量满足最左匹配,避免索引失效
索引失效:
(1)select *
(2)>、<、!=、between
(3)前置 % 的like查询。
(4)where字段值类型与数据库不一致,存在自动转换。
(5)or连接不同字段。
(6)or连接同一字段,但存在 >、<、!= 非索引查询。(左右查询均为索引时才有效。)
(7)函数或运算导致索引失效。
(8)IS NULL不走索引,IS NOT NULL走索引(表设计:非必要时,字段不要为NULL,设置默认空字符串或0)
(9)复合索出现范围查询时,后面的索引失效 。
(10)IN会走索引,但是当IN的取值范围较大时会导致索引失效。
- ep_range_index_dive_limit这个参数影响in是否使用索引,MySQL 5.6默认10, MySQL。5.7默认200。但是我们代码更倾向于控制在50内。 A表数据大于B表数据时,选择in比exists执行效率要高。 相反,A表数据小于B表数据时,选择exists比较高效。 in先执行子查询,exists先执行外表。
(11)not in会使索引失效,无论在哪种情况not exists 都比 not in 高效。
- 使用 left join 或 not exists 来优化not in 操作。
4. 尽量做到冷热数据分离
减少查询表列数,避免进行冷数据过滤。
5. 调整索引列的顺序
唯一性较好、字段较短、使用频繁的列放在联合索引的最左侧
6. 对于频繁的查询优先考虑使用覆盖索引
走索引时一并查询出数据。
7. 避免数据类型的隐式转换
避免索引失效。
8. 禁止使用 SELECT * 必须使用 SELECT <字段列表> 查询
会存在冗余字段,消耗更多的CPU和网络带宽 会无法使用覆盖索引。
9. 避免使用子查询,可以把子查询优化为 join 操作
子查询会产生大量的临时表也没有索引。
10. 避免使用 JOIN 关联太多的表
关联表越多关联缓存越大。
11. 在明显不会有重复值时使用 UNION ALL 而不是 UNION
UNION ALL不会进行去重操作,加快查询速度。
12. 大SQL拆分,多个同种SQL进行批处理
一个 SQL 只能使用一个cpu 进行计算,拆分后可由多个cpu并行计算。
13. WHERE 从句中禁止对列进行函数转换和计算
避免索引失效。
14. 使用count(*)而不是count(列名)?
综合性能:count(非主键列) < count(主键) < count(1) ≈ count()
count()是SQL92定义的标准统计行数的语法,跟数据库无关,count(*)会统计值为 NULL的行,而
count(列名)不会统计此列为 NULL值的行。
边栏推荐
- 2022.07.02
- Kirin Xin'an with heterogeneous integration cloud financial information and innovation solutions appeared at the 15th Hunan Financial Technology Exchange Conference
- el-upload上传组件的动态添加;el-upload动态上传文件;el-upload区分文件是哪个组件上传的。
- [sword finger offer] sword finger offer II 012 The sum of left and right subarrays is equal
- 杰理之开机自动配对【篇】
- Training IX basic configuration of network services
- 【剑指offer】剑指 Offer II 012. 左右两边子数组的和相等
- 注解。。。
- Interpretation of transpose convolution theory (input-output size analysis)
- R语言ggplot2可视化:使用ggpubr包的ggqqplot函数可视化QQ图(Quantile-Quantile plot)
猜你喜欢
8 CAS
项目经理『面试八问』,看了等于会了
【STL】vector
9 atomic operation class 18 Rohan enhancement
索引总结(突击版本)
论文解读(ValidUtil)《Rethinking the Setting of Semi-supervised Learning on Graphs》
[Verilog advanced challenge of Niuke network question brushing series] ~ multi bit MUX synchronizer
[RT thread env tool installation]
华南X99平台打鸡血教程
The project manager's "eight interview questions" is equal to a meeting
随机推荐
Solve the problem of remote rviz error reporting
2022如何评估与选择低代码开发平台?
【STL】vector
关于ssh登录时卡顿30s左右的问题调试处理
LeetCode_7_5
How to buy stocks on your mobile phone and open an account? Is it safe to open an account
The DBSCAN function of FPC package of R language performs density clustering analysis on data, checks the clustering labels of all samples, and the table function calculates the two-dimensional contin
L1-027 rental (Lua)
R语言ggplot2可视化:使用ggpubr包的ggqqplot函数可视化QQ图(Quantile-Quantile plot)
Semantic SLAM源码解析
微信公众号OAuth2.0授权登录并显示用户信息
2022.07.05
怎么在手机上买股票开户 股票开户安全吗
R语言使用ggplot2函数可视化需要构建泊松回归模型的计数目标变量的直方图分布并分析构建泊松回归模型的可行性
tp6 实现佣金排行榜
IP 工具类
2022.07.02
转置卷积理论解释(输入输出大小分析)
[sword finger offer] sword finger offer II 012 The sum of left and right subarrays is equal
R language uses ggplot2 function to visualize the histogram distribution of counting target variables that need to build Poisson regression model, and analyzes the feasibility of building Poisson regr