当前位置:网站首页>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
边栏推荐
- What is Mipi
- Trigonometric function calculation
- Learning Tai Chi Maker - mqtt Chapter II (IX) test of this chapter
- 大三下期末考試
- Exec function of PHP
- What is the Valentine's Day gift given by the operator to the product?
- Wechat applet custom component
- 【Redis】SortedSet类型
- Apache does not parse PHP files, but directly displays the source code
- 字符串方法练习
猜你喜欢

What is the dry goods microservice architecture? What are the advantages and disadvantages?

Regular expression (?: pattern)

How to use project Gantt chart to make project report
![[redis] list type](/img/c7/adec2aaea50d69a0aed95bfe683b9e.png)
[redis] list type

Redis master-slave replication

瀑布型项目管理最常用的10个小工具,可以自由搭建使用

安装kibana

CTFHub-Web-密码口令-默认口令

The 10 most commonly used gadgets for waterfall project management can be built and used freely

Has Moore's law come to an end?
随机推荐
Deploy redis high availability cluster
CTFHub-Web-密码口令-弱口令
安装mysql5.7 并修改密码
They all talk about interviews with big factories. When I interview with small factories, I invite people to drink tea?
Differences between web testing and app testing
mark
PHP的system函数
Matrix eigenvalue and eigenvector solution - eigenvalue decomposition (EVD)
The thinkphp5.1 runtime file has been changed to 777 permission, but cannot be written
[redis] key hierarchy
Trigonometric function calculation
Convert flat structure to tree structure
Has Moore's law come to an end?
在按钮禁用时消除hover效果
對補wasm環境的一些測試
Sysbench Pressure Test Oracle (installation and use examples)
Examen final de troisième année
Studies of relative costs for development in different languages
字符串替换
Blog publishing test 1