当前位置:网站首页>SQL common optimization
SQL common optimization
2022-07-07 20:00:00 【Whiteye too white】
sql Optimize
1. Small tables drive large tables
First, query the table with less data , Then execute the query of the table with more data .
2. Index building , A table does not exceed 5 An index
Avoid large memory consumption .
3. Go to the index , Try to meet the leftmost match , Avoid index invalidation
Index failure :
(1)select *
(2)>、<、!=、between
(3) In front of % Of like Inquire about .
(4)where The field value type is inconsistent with the database , There is an automatic conversion .
(5)or Connect different fields .
(6)or Connect the same field , But there is >、<、!= Non indexed query .( Only when both left and right queries are indexes .)
(7) Function or operation causes index invalidation .
(8)IS NULL Don't walk index ,IS NOT NULL Go to the index ( Table design : When not necessary , The field should not be NULL, Set the default empty string or 0)
(9) When querying the range of composite cables , The following index is invalid .
(10)IN Can walk the index , But when IN When the value range of is large, the index will be invalid .
- ep_range_index_dive_limit This parameter affects in Use index or not ,MySQL 5.6 Default 10, MySQL.5.7 Default 200. But our code tends to be controlled in 50 Inside . A Table data is greater than B Table data , choice in Than exists High execution efficiency . contrary ,A Table data is less than B Table data , choice exists More efficient . in Execute subquery first ,exists First perform the appearance .
(11)not in Will invalidate the index , In either case not exists All ratio not in Efficient .
- Use left join or not exists To optimize not in operation .
4. Try to separate hot and cold data
Reduce the number of query table columns , Avoid cold data filtering .
5. Adjust the order of index columns
The uniqueness is better 、 Field is short 、 The frequently used column is placed on the far left of the union index
6. Override indexes are a priority for frequent queries
Query the data together when searching .
7. Avoid implicit conversions of data types
Avoid index invalidation .
8. No use SELECT * You have to use SELECT < Field list > Inquire about
There will be redundant fields , Consume more CPU And network bandwidth Will not be able to use overlay index .
9. Avoid subqueries , The subquery can be optimized to join operation
Subqueries will generate a large number of temporary tables without indexes .
10. Avoid using JOIN Too many tables associated
The more associated tables, the larger the associated cache .
11. Used when there is obviously no duplicate value UNION ALL instead of UNION
UNION ALL No de duplication operation , Speed up query .
12. Big SQL Split , More than one species SQL Batch processing
One SQL You can only use one cpu Calculate , After splitting, multiple cpu Parallel computing .
13. WHERE Functional transformations and calculations for columns are forbidden in clauses
Avoid index invalidation .
14. Use count(*) instead of count( Name )?
Comprehensive performance :count( Non primary key columns ) < count( Primary key ) < count(1) ≈ count()
count() yes SQL92 The syntax for defining the number of standard statistics lines , It's not about the database ,count(*) The statistical value is NULL The line of , and
count( Name ) This column is not counted NULL Row of values .
边栏推荐
- R语言ggplot2可视化:使用ggpubr包的ggviolin函数可视化小提琴图、设置palette参数自定义不同水平小提琴图的填充色、add参数在小提琴图添加箱图
- 使用高斯Redis实现二级索引
- 力扣 1961. 检查字符串是否为数组前缀
- pom.xml 配置文件标签:dependencies 和 dependencyManagement 区别
- Kunpeng developer summit 2022 | Kirin Xin'an and Kunpeng jointly build a new ecosystem of computing industry
- Flink并行度和Slot详解
- 模拟实现string类
- equals 方法
- Tp6 realize Commission ranking
- R语言ggplot2可视化:使用ggpubr包的ggqqplot函数可视化QQ图(Quantile-Quantile plot)
猜你喜欢
剑指 Offer II 013. 二维子矩阵的和
编译器优化那些事儿(4):归纳变量
mysql 的一些重要知识
Openeuler prize catching activities, to participate in?
干货分享|DevExpress v22.1原版帮助文档下载集合
使用高斯Redis实现二级索引
8 CAS
最多可以参加的会议数目[贪心 + 优先队列]
YoloV6:YoloV6+Win10---训练自己得数据集
Automatic classification of defective photovoltaic module cells in electroluminescence images-論文閱讀筆記
随机推荐
Detailed explanation of Flink parallelism and slot
时间工具类
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
what‘s the meaning of inference
ASP.NET幼儿园连锁管理系统源码
Welcome to the markdown editor
# 欢迎使用Markdown编辑器
关于自身的一些安排
让这个 CRMEB 单商户微信商城系统火起来,太好用了!
mock. JS returns an array from the optional data in the object array
9 atomic operation class 18 Rohan enhancement
R language ggplot2 visualization: use the ggstripchart function of ggpubr package to visualize the dot strip plot, set the position parameter, and configure the separation degree of different grouped
Boot 和 Cloud 的版本选型
sql 常用优化
Automatic classification of defective photovoltaic module cells in electroluminescence images-論文閱讀筆記
项目经理『面试八问』,看了等于会了
PMP practice once a day | don't get lost in the exam -7.7
【RT-Thread env 工具安装】
CSDN语法说明
R language dplyr package mutate_ At function and min_ The rank function calculates the sorting sequence number value and ranking value of the specified data column in the dataframe, and assigns the ra