[Database] SQL statement optimization skills

1. Content

1、select

1-1. Index

  • Try to use "=" . Avoid using "<>" to increase index usage.
  • Use (not) in with caution . It may result in a full table scan, which can be replaced by between xx and yy.
  • Avoid or join conditions . Or consider letting each condition column have an index.
  • Avoid calculating fields . Such as functions, four arithmetic, etc.
  • The first field of the composite index can guarantee the use of the index .
  • Indexes are not as many as possible . Insert/update will cause the index to be rebuilt. Generally no more than six.

1-2. Quantity

1. Limit is used to query a limited number of results, and can also be used for paging. Avoid full table scans. Reduce unnecessary network transmission between databases and applications
2. Select the appropriate data type .

  • Replace SMALLINT with TINYINT to reduce memory and disk consumption. Varchar replaces char, which can save storage space and improve query efficiency.
  • Numerical fields replace character types to reduce the number of comparisons. The string compares each character one by one, while the numeric type compares only once.

3. If allowed, replace UNION with UNION ALL . Because UNION ALL does not de-duplicate, it is more efficient. 
4. Avoid use SELECT * . Avoid full table scans, can't effectively use indexes, and increase network IO overhead.
5. Try to index the condition fields . The columns in the WHERE, JOIN, and ORDER BY clauses should be indexed as much as possible.

2. Insert statement

1. Load text files are faster than insert multiple files .
2. Split large INSERT, DELETE, and UPDATE queries into multiple small queries.

3、Group by

1. Specify order by null to prohibit sorting . By default, group by col1, col2 and order by col1, col2 will sort the results similarly; Extra: filesort is time-consuming.

4、Order by

1. Where conditions and order by use the same index && order by order and index order are the same && order by fields are in ascending or descending order

 例:下列SQL可以使用索引:
SELECT * FROM t1 ORDER BY key_part1, key_part2,... ; 
SELECT * FROM t1 WHERE key_part1=1 ORDER BY key_part1 DESC, key_part2 DESC; 
SELECT * FROM t1 ORDER BY key_part1 DESC, key_part2 DESC; 
 
但以下几种情况下则不使用索引: 
SELECT * FROM t1 ORDER BY key_part1 DESC, key_part2 ASC;  
--order by的字段混合ASC和DESC 
SELECT * FROM t1 WHERE key2=constant ORDER BY key1;  
--用于查询行的关键字与ORDER BY中所使用的不相同 
SELECT * FROM t1 ORDER BY key1, key2;  
--对不同的关键字使用ORDER BY:

5. Nested queries

1. In some cases, the subquery is replaced by JOIN

# SQL1
select * from sales2 where company_id not in ( select id from 
company2 )

# SQL2 
select * from sales2 left join company2 on sales2.company_id = 
company2.id where sales2.company_id is null

# SQL1 VS SQL2
1、这里join之所以更快,是因为不需要在内存中创建临时表完成SQL1中两个步骤的查询工作。 
2、当company2表的id有索引,性能会更好。

6. Other

1. Index hint:

  • USE INDEX prompts MySQL to refer to the reference index, ignoring others.
  • IGNORE INDEX ignores one or more indexes.
  • FORCE INDEX forces the use of reference indexes.

2. Multiple executions of the same result set keep the SQL statement consistent and make full use of the query buffer.

例:1次:SELECT price FROM order WHERE id = 123456 and region = 'BEIJING';2次:同样查询,请保持语句一致性,如不要将where里id和region调换。 

3. SQL tuning is best tested in the development environment with the closest real data set and hardware environment, and then released to the production environment.

Two, reference

1. "Learning MySQL in a Simple Way" - Chapter 18 SQL Optimization

Guess you like

Origin blog.csdn.net/HeavenDan/article/details/112224299