当前位置:网站首页>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
边栏推荐
猜你喜欢

Use photoshop2022 to create a wonderful gradient effect for pictures

Koa 快速入門

Oracle Recovery Tools实战批量坏块修复

Introduction to openresty

高并发的理解与设计方案

EMC、EMI、EMS的关系

Ctfhub web SQL injection - integer injection
![[redis] key hierarchy](/img/ab/a5d3bb61b4571966d0f47037af4f41.png)
[redis] key hierarchy

【Redis】List类型

Zhongyi technology resumed the review status of the gem IPO, and xuxiaofei no longer acted as a practicing lawyer
随机推荐
The thinkphp5.1 runtime file has been changed to 777 permission, but cannot be written
CTFHub-Web-密码口令-默认口令
MySQL queries the data of today, yesterday, this week, last week, this month, last month, this quarter, last quarter, this year, last year
Download and installation of MySQL
Cross border information station
Centos7 installation php7.2
Blog publishing test 1
【Redis】初识 Redis
【Redis】Set类型
Smart world 2030
String output
对补wasm环境的一些测试
微信小程序自定义组件
String attribute exercise
Which brokerage is safer and more convenient to open an account for compass mobile stock?
Some tests on complementary wasm environment
Pyinstaller打包pikepdf失败的问题排查
Which securities company is the largest and safest? Which securities company has good service
字符串属性练习
字符串分段组合