当前位置:网站首页>Talk about SQL optimization
Talk about SQL optimization
2022-06-29 02:31:00 【Promise__ shallow】
## One . Index failure
1. If there is... In the condition or, Even if there is a conditional index, it will not be used (or Conditions are indexed independently , use UNION Replace or)
2. For multi-column indexes , Not the first part of use , Index will not be used
3.like The query is based on % start ( With % The ending is OK )
4. If the column type is string , Be sure to quote the data in the condition , Otherwise, the index is not used
5. If mysql It is estimated that full table scanning is faster than indexing , Index is not used
6. Should try to avoid in where Field in the clause null Value judgement , Failing to do so will cause the engine to abandon the index for a full table scan , Such as
select id from t where num is null
- 1.
( It's better not to leave it in the database NULL, Use as much as possible NOT NULL Fill the database .)
7. Should try to avoid in where Used in clauses != or <> The operator , Otherwise, the engine will discard the index and scan the whole table .
8.in and not in Be careful with , Otherwise, it will result in a full table scan ( For continuous values , It works between Don't use in 了 )( use exists Instead of in)
select num from a where exists(select 1 from b where num=a.num)
- 1.
9. If in where Use parameters in Clause , It can also lead to a full table scan . because SQL Local variables are resolved only at runtime , But the optimizer can't delay the choice of access plan to run time ; It has to be selected at compile time . however and , If an access plan is established at compile time , The value of the variable is still unknown , Therefore, it cannot be selected as an input item of index . As the following statement will scan the whole table :
select id from t where num = @num
- 1.
You can force queries to use indexes instead :
select id from t with(index( Index name )) where num = @num
- 1.
10. Should try to avoid in where The clause performs an expression operation on the field , This causes the engine to abandon the use of indexes for a full table scan . Such as :
select id from t where num/2 = 100
- 1.
Should be changed to
select id from t where num = 100*2
- 1.
11. Should try to avoid in where Clause to function fields , This causes the engine to abandon the use of indexes for a full table scan . Such as
select id from t where substring(name,1,3) = ’abc’ -–name With abc At the beginning id
select id from t where datediff(day,createdate,’2005-11-30′) = 0 -–‘2005-11-30’ -- Generated id
- 1.
- 2.
Should be changed to
select id from t where name like 'abc%'
select id from t where createdate >= '2005-11-30' and createdate < '2005-12-1'
- 1.
- 2.
12. When using index fields as conditions , If the index is a composite index , Then the first field in the index must be used as a condition to ensure that the system uses the index , Otherwise the index will not be used , And try to make the order of the fields consistent with the order of the index .
## Other optimization
1. Try to use numeric fields , If only contains the numerical value information the field as far as possible does not design for the character type , This reduces the performance of queries and connections , And it increases storage overhead . This is because the engine is processing queries and joins Then it compares each character in the string one by one , For digital models, only one comparison is enough .
2. Use as much as possible varchar/nvarchar Instead of char/nchar , Because first of all the longer fields have less storage space , You can save storage space , Second, for queries , Searching in a relatively small field is obviously more efficient .
3. Don't use... Anywhere select * from t , Replace... With a specific list of fields “*”, Do not return any fields that are not available .
4. Try to avoid using cursors , Because the efficiency of cursors is poor
5. Break up the big DELETE or INSERT sentence , Submit in bulk SQL sentence
while(1){
// Do it every time 1000 strip
mysql_query(“delete from logs where log_date <= ’2012-11-01’ limit 1000”);
if(mysql_affected_rows() == 0){
// Delete complete , sign out !
break;
}
// Pause for a while at a time , Release the table so that other processes / Thread access .
usleep(50000)
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
Reference article :
http://www.bxoon.com/archives/%E8%B0%88%E8%B0%88sql%E4%BC%98%E5%8C%96
边栏推荐
猜你喜欢

【Redis】List类型

KOA Quick Start
![[redis] sortedset type](/img/7f/f5f1aa603c8994b669d52a435fed7e.png)
[redis] sortedset type

Target detection - ADAS practice

EMC、EMI、EMS的关系

How to use project Gantt chart to make project report

【无标题】

China's flexible employment has reached 200million

Regular expression (?: pattern)

Use photoshop2022 to create a wonderful gradient effect for pictures
随机推荐
Smart world 2030
The meaning of cross multiplication and dot multiplication (simple formula memory method)
组合数据类型之元组小练习
String length
pvcreate asm disk导致asm磁盘组异常恢复---惜分飞
apache不解析PHP文件,直接显示源码
大智慧手机股票开户哪个券商更安全更方便?
Zhongyi technology resumed the review status of the gem IPO, and xuxiaofei no longer acted as a practicing lawyer
[redis] data introduction & General Command & string type
Centos7 installation php7.2
【Redis】SortedSet类型
String attribute exercise
Studies of relative costs for development in different languages
矩阵特征值和特征向量求解——特征值分解(EVD)
瀑布型项目管理最常用的10个小工具,可以自由搭建使用
【無標題】
【学习笔记】子集和问题
【无标题】
Redis master-slave replication
字符串长度