当前位置:网站首页>N methods of SQL optimization
N methods of SQL optimization
2022-07-27 02:25:00 【Current affairs reader hjj】
List of articles
- SQL Optimization of the N Methods
- 1.SQL In the sentence IN Should not contain too many values :
- 2.SELECT The statement must indicate the field name :
- 3. When only one piece of data is queried , Use limit 1
- 4. To avoid the where Field in the clause null Value judgement
- 5. To avoid the where The clause performs an expression operation on the field :
- 6. For federated indexes , Follow the leftmost prefix rule :
- 7. Use as much as possible inner join, avoid left join:
- 8. Note the range query statement :
- 9. Not recommended % Prefix fuzzy query :
- 10. stay where Used in clauses or To join the conditions , If or One of the connection conditions has no index , Will cause the engine to discard the index and scan the whole table
- 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
- 12. String type fields If you don't use quotation marks when querying '' , Will result in automatic implicit conversion , Then the index fails
- My learning forum
SQL Optimization of the N Methods
1.SQL In the sentence IN Should not contain too many values :
for example :select id from handsome where number in(1,2,3) For continuous values , It works between Don't use in 了 .
2.SELECT The statement must indicate the field name :
No use * To query , No use * To query , No use * To query , Important things are to be repeated for 3 times , Find which field , Just write specific fields .
3. When only one piece of data is queried , Use limit 1
【 This is very useful 】
4. To avoid the where Field in the clause null Value judgement
5. To avoid the where The clause performs an expression operation on the field :
select id from user WHERE id*100=500;
above sql Just do the arithmetic operation on the field , This will cause the engine to give up using indexes , It is suggested to change it to :
select id from user WHERE id=500/100;
6. For federated indexes , Follow the leftmost prefix rule :
For example, composite index (id,name,sex) When you use it , Sure id perhaps id,name . No direct name, perhaps sex Will cause the federated index to fail
Be careful : id, name,sex The order of filling in these three fields will not affect , mysql It will be automatically optimized to the leftmost matching order
The first three sql Can hit the index , The middle two do not conform to the leftmost matching principle , Index failure
The last item sql Due to the leftmost index id So the index is partially successful , Partial failure . id Field index used successfully
explain select * from `user_test` where uid=10 ;
explain select * from `user_test` where uid=10 and name=' A man of current affairs ';
explain select * from `user_test` where uid=10 and name=' A man of current affairs ' and phone='13047967256';
explain select * from `user_test` where name=' A man of current affairs ' and phone='13047967256';
explain select * from `user_test` where name=' A man of current affairs ';
explain select * from `user_test` where uid=10 and phone='13047967256';
7. Use as much as possible inner join, avoid left join:
If the connection is inner join, In the absence of other filter conditions MySQL Will automatically select the small table as the driving table , however left join In the selection of driving table, we follow the principle of left driving right , namely left join The table on the left is called the drive table .
8. Note the range query statement :
For federated indexes , If there are scope queries , such as between、>、< Under equal conditions , It will invalidate the following index fields .
terms of settlement : Where business permits , Use >= perhaps <= This does not affect the use of indexes
9. Not recommended % Prefix fuzzy query :
for example : LIKE“%name” perhaps LIKE“%name%”, This kind of query will lead to index failure and full table scanning . But you can use LIKE “name%”.
10. stay where Used in clauses or To join the conditions , If or One of the connection conditions has no index , Will cause the engine to discard the index and scan the whole table
terms of settlement : take or Both sides of the connection are indexed , You can use
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
12. String type fields If you don't use quotation marks when querying ’’ , Will result in automatic implicit conversion , Then the index fails
My learning forum
HandsomeForum: use Java Prepared Learning Forum , Build our own circle !(http://huangjunjie.vip:66)
The article links :http://huangjunjie.vip:66/blog/read/zmkyghlz0rf8i3bes1
边栏推荐
猜你喜欢

Codeforces Round #809 (Div. 2), problem: (C) Qpwoeirut And The City

CAN总线通信应用

数字集成电路:CMOS反相器(一)静态特性

Esp8266wi fi access cloud platform
NPM reports an error, error: eperm: operation not permitted, MKDIR

C language - value range of data type and basic data type

oSPF基础实验配置

记录第N次SQL异常

HCIA Basics (1)

数字集成电路:MOS管器件章(二)
随机推荐
今天浅讲一下转义字符【萌新版】
About unsafe problems such as fopen and strError encountered in vs2022 or advanced version running environment
Experiment exercise of two-layer packaging technology (HDLC, ppp--pap\chap, GRE)
lvs+keepalived项目实战
NPM reports an error, error: eperm: operation not permitted, MKDIR
Golang bufio Reader 源码详解
HCIP oSPF综合实验
Guangguangzai's CSDN journey
Lvs+keepalived project practice
Esp8266wi fi data communication
HCIA Basics (1)
C语言——第一个程序、打印、变量和常量
多线程中 synchronized 锁升级的原理是什么?
HCIP 双向重发布以及路由策略
Nb-iot networking communication
(史上最详细)Codeforces Round #805 (Div. 3)E. Split Into Two Sets
Esp8266wi fi access cloud platform
Simple application of rip V2 (V2 configuration, announcement, manual summary, ripv2 authentication, silent interface, accelerating convergence)
打开编程的大门
Codeforces Round #810 (Div. 2), problem: (B) Party