当前位置:网站首页>SQL optimization skills and precautions
SQL optimization skills and precautions
2022-07-24 10:55:00 【dotaer-df】
When you finish writing a sql, If you are not sure about the execution of this statement , It is recommended to use EXPLAIN keyword , According to the results of the execution , Conduct optimization analysis .EXPLAIN Keyword details
in Don't include too many values
Oracle in ,in The maximum number of parameters that can be placed in a statement is 1000 individual ,mysql in ,in The number of parameters in the statement is Don't limit Of . But for the whole paragraph sql The length of the statement is limited (max_allowed_packet Default bit 4M), however Better not to exceed 200 It's worth ( about in Performance tests that contain the number of values ).- select Statement indicates the field name
If used select * Unnecessary io Or back to the table ( Why not recommend select *) - Index the sorting field
The records in the database are organized in a set way , The records in a record set are often out of order ( Or there is a default order , For example, in the order of being stored in the database , But this order cannot be absolutely guaranteed ), And the index is ordered , So using index can greatly speed up the query - To avoid the where Field in the clause null Value judgement
about null The judgment of may cause the engine to give up using the index and perform a full table scan , Why is it possible ? It depends. mysql Is the cost of deciding whether to execute an index small enough . Found by testing , Suppose the number of data records remains unchanged , If most of the data is null value , that is null No index , A full scan will be performed , and is not null Will use the index , conversely , If most of them are not empty ,is null Use index ,is not null No index .(Mysql in null Whether the value uses the index ) - Don't use % Prefix fuzzy query
Such as like %name perhaps like %name%, This can also lead to index invalidation , and like name% I can walk the index - Avoid implicit type conversion
where Clause column Type conversion when the type of the field is inconsistent with the type of the parameter passed in , It is suggested that where Parameter types in . - Joint index , Follow the leftmost prefix rule
For example, a federated index contains fields id、name、school, It can be used directly id Field , It's fine too id、name In this order , They'll go through the index , however name;school Can't use this index . So when you create a union index, you must pay attention to the order of index fields , Common query fields are placed at the top .( The leftmost prefix rule )
hypothesis key1,key2,key3 Index for Federation select key1,key2,key3 from person where key2 = 'boss', In this case, do you think you don't go to the index ? In fact, it will also go through the index , Because it doesn't need to go back to the table , Directly in this secondary index ( Joint index ) Get value inside . - Don't operate on Columns
If the query conditions contain functions or expressions , It can also cause indexes to fail
for example select * from user where YEAR(birthday) < 2000 - You can use force index To force an index
sometimes MySQL The optimizer takes the index it thinks appropriate to retrieve SQL sentence , But maybe the index it uses is not what we want . At this point, it can be used forceindex To force the optimizer to use the index we specify .
边栏推荐
- 爬虫与反爬:一场无休止之战
- 零基础学习CANoe Panel(9)—— 组合框(ComboBox)
- Golang Li Kou leetcode 494. goals and
- MySQL - update data records in tables
- Zero basic learning canoe panel (8) -- hex/text editor
- 5个最佳WordPress广告插件
- How to build a node development environment efficiently
- N叉树、page_size、数据库严格模式修改、数据库中delect和drop的不同
- Binlog and iptables prevent nmap scanning, xtrabackup full + incremental backup, and the relationship between redlog and binlog
- 神器 ffmpeg —— 操作视频,极度舒适
猜你喜欢

很佩服的一个Google大佬,离职了。。

Qt程序最小化托盘后,再弹出个msgbox,点击确定后程序退出问题解决

Call bind apply simple summary

TwinCAT3各版本下载路径

【白帽子讲Web安全】第二章 浏览器安全

零基础学习CANoe Panel(5)——改变变量的值,控件图像也改变,这是怎么回事?

ECCV 2022 | Tsinghua proposes the first transformer to embed spectral sparsity
![[FPGA]: IP core ibert](/img/f9/ef4c8d44be2e27b6d85010ca8cdefa.png)
[FPGA]: IP core ibert

Sentinel flow control quick start

Volcanic engine: open ByteDance, the same AI infrastructure, a system to solve multiple training tasks
随机推荐
Five application scenarios of Bluetooth module
[interview: Basics 02: bubble sort]
[FPGA]: frequency measurement
二分查找法
Array element removal problem
零基础学习CANoe Panel(5)——改变变量的值,控件图像也改变,这是怎么回事?
rs485通信OSI模型网络层
[personal summary] end of July 17, 2022
Why should we "go up and down" when testing left shift and right shift?
Princeton chendanqi: how to make the "big model" smaller
神器 ffmpeg —— 操作视频,极度舒适
零基础学习CANoe Panel(8)—— 数据/文本编辑控件(Hex/Text Editor )
[FPGA]: IP core -- xadc
Modbus RTU通讯协议详解与实例演示
[ISE] development process plus bit, burning of MCS files
[dish of learning notes dog learning C] initial level of pointer
Distributed transaction processing scheme big PK!
UVM - two way communication
[FPGA]: IP core DDS
零基础学习CANoe Panel(4)——按钮(Button )