1. To optimize the query, full table scan should be avoided as much as possible. First, you should consider building indexes on the columns involved in where and order by.
2. Try to avoid the null value judgment of the field in the where clause, otherwise the engine will give up the use of the index and perform a full table scan, such as: select id from t where num is null You can set the default value of 0 on num to ensure that There is no null value in the num column in the table, and then query like this: select id from t where num=0
3. Try to avoid using the != or <> operator in the where clause, otherwise the engine will give up using the index and perform a full table scan.
4. Try to avoid using or to connect conditions in the where clause, otherwise the engine will give up the use of the index and perform a full table scan, such as: select id from t where num=10 or num=20 You can query like this: select id from t where num=10 union all select id from t where num=20
5. In and not in should also be used with caution, otherwise it will lead to a full table scan, such as: select id from t where num in(1,2,3) For continuous values, if you can use between, do not use in: select id from t where num between 1 and 3
6. The following query will also result in a full table scan: select id from t where name like '%Li%' To improve efficiency, full-text search can be considered.
7. If a parameter is used in the where clause, it will also cause a full table scan. Because SQL resolves local variables only at runtime, the optimizer cannot defer the choice of an access plan to runtime; it must choose it at compile time. However, if the access plan is built at compile time, the value of the variable is unknown and cannot be used as an input for index selection. For example, the following statement will perform a full table scan: select id from t where num=@num can be changed to force the query to use the index: select id from t with(index(index name)) where num=@num
8. The expression operation on the field in the where clause should be avoided as much as possible, which will cause the engine to give up the use of the index and perform a full table scan. For example: select id from t where num/2=100 should be changed to: select id from t where num=100*2
9. You should try to avoid functional operations on fields in the where clause, which will cause the engine to give up the use of indexes and perform full table scans. For example: select id from t where substring(name,1,3)='abc' , the id whose name starts with abc should be changed to:
select id from t where name like ‘abc%’
10. Do not perform functions, arithmetic operations or other expression operations on the left side of the "=" in the where clause, otherwise the system may not be able to use the index correctly.
11. When using an index field as a condition, if the index is a composite index, 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 should be used. As much as possible, make the field order consistent with the index order.
12. Don't write some meaningless queries, such as generating an empty table structure: select col1,col2 into #t from t where 1=0
This kind of code will not return any result set, but will consume system resources, it should be changed to this:
create table #t(…)
13.很多时候用 exists 代替 in 是一个好的选择:select num from a where num in(select num from b)
用下面的语句替换:
select num from a where exists(select 1 from b where num=a.num)