Programmers' pursuit of SQL performance

1. Try not to use select * for query SQL, but select specific fields, and avoid returning to the table as much as possible.

2. If you know that there is only one query result or only one record, it is recommended to use limit 1

3. Try to avoid using or to join conditions in the where clause, because the index will be abandoned

4. Optimize limit paging. When the offset is particularly large, the offset is skipped, and the query efficiency is greatly improved.

5. Optimize your like statement, putting% in front is not indexing

6. Use the where condition to limit the data to be queried for a record to avoid returning redundant rows

7. Try to avoid using mysql's built-in functions on index columns

8. Try to avoid performing expression operations on fields in the where clause, which will cause the system to abandon the use of indexes and perform full table scans

9. Inner join, left join, and right join. Inner join is preferred. If it is left join, the result of the left table should be as small as possible

10. Try to avoid using the != or <> operator in the where clause, otherwise the index will be abandoned for a full table scan.

11. When using a joint index, pay attention to the order of the index columns and generally follow the leftmost matching principle.

12. To optimize the query, consider building indexes on the columns involved in where and order by, and try to avoid full table scans.

13. If you insert too much data, consider batch inserting.

14. When appropriate, use covering indexes.

15. Use the distinct keyword with caution. If there are many fields, it will greatly reduce the query efficiency.

16. Remove redundant and duplicate indexes

17. If the amount of data is large, optimize your modify/delete statement.

18. Consider using default values ​​instead of null in the where clause.

19. Do not have more than 5 table connections

20. Reasonable use of exist&in, choose the outermost loop small

21, try to replace union with union all

22. The index should not be too many, generally within 5.

23. Use numeric fields as much as possible. If fields containing only numeric information, try not to design them as characters

24. Indexes are not suitable to be built on fields with a large amount of repeated data, such as gender database words

25. Try to avoid returning too much data to the client.

26. When connecting multiple tables in a SQL statement, please use the alias of the table and prefix each column with the alias to make the semantics clearer.

27. Use varchar/nvarchar instead of char/nchar as much as possible.

28. In order to improve the efficiency of the group by statement, you can filter out unnecessary records before executing the statement.

29. If the field type is a string, the where must be enclosed in quotation marks, otherwise the index will fail

30. Use explain a lot to analyze your SQL statements

If you have any questions or different opinions, please leave a message and exchange together. The blogger will reply as soon as he sees it...

Guess you like

Origin blog.csdn.net/mrhs_dhls/article/details/105991587