当前位置:网站首页>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值的行。
边栏推荐
- R language ggplot2 visualization: use the ggqqplot function of ggpubr package to visualize the QQ graph (Quantitative quantitative plot)
- Specify the version of OpenCV non-standard installation
- 杰理之发起对耳配对、回连、开启可发现、可连接的轮循函数【篇】
- Throughput
- Make this crmeb single merchant wechat mall system popular, so easy to use!
- LeetCode 535(C#)
- 9 原子操作类之18罗汉增强
- Classification automatique des cellules de modules photovoltaïques par défaut dans les images de lecture électronique - notes de lecture de thèse
- 项目经理『面试八问』,看了等于会了
- Tp6 realize Commission ranking
猜你喜欢
Simulate the implementation of string class
关于ssh登录时卡顿30s左右的问题调试处理
The project manager's "eight interview questions" is equal to a meeting
Classification automatique des cellules de modules photovoltaïques par défaut dans les images de lecture électronique - notes de lecture de thèse
648. 单词替换
国家网信办公布《数据出境安全评估办法》:累计向境外提供10万人信息需申报
CMD command enters MySQL times service name or command error (fool teaching)
Research and practice of super-resolution technology in the field of real-time audio and video
Automatic classification of defective photovoltaic module cells in electroluminescence images-論文閱讀筆記
Kirin Xin'an cloud platform is newly upgraded!
随机推荐
L1-027 rental (Lua)
R语言ggplot2可视化:使用ggpubr包的ggecdf函数可视化分组经验累积密度分布函数曲线、linetype参数指定不同分组曲线的线型
openEuler 有奖捉虫活动,来参与一下?
“本真”是什么意思
Flink并行度和Slot详解
LeetCode力扣(剑指offer 36-39)36. 二叉搜索树与双向链表37. 序列化二叉树38. 字符串的排列39. 数组中出现次数超过一半的数字
网信办公布《数据出境安全评估办法》,9 月 1 日起施行
2022.07.05
Is PMP beneficial to work? How to choose a reliable platform to make it easier to prepare for the exam!!!
mock.js从对象数组中任选数据返回一个数组
PMP對工作有益嗎?怎麼選擇靠譜平臺讓備考更省心省力!!!
指定opencv非标准安装的版本
2022.07.02
R language ggplot2 visualization: use the ggqqplot function of ggpubr package to visualize the QQ graph (Quantitative quantitative plot)
[Verilog advanced challenge of Niuke network question brushing series] ~ multi bit MUX synchronizer
R语言使用ggplot2函数可视化需要构建泊松回归模型的计数目标变量的直方图分布并分析构建泊松回归模型的可行性
我的创作纪念日
The research group of the Hunan Organizing Committee of the 24th China Association for science and technology visited Kirin Xin'an
R语言ggplot2可视化:使用ggpubr包的ggdensity函数可视化分组密度图、使用stat_overlay_normal_density函数为每个分组的密度图叠加正太分布曲线
Automatic classification of defective photovoltaic module cells in electroluminescence images-論文閱讀筆記