Summary of MySQL query optimization methods

1. Try to avoid using the != or <> operator in the where clause, otherwise the engine will give up the use of the index and perform a full table scan.

2. To optimize the query, full table scan should be avoided as much as possible. First, you should consider building an index on the columns that are frequently involved in where and order by.

3. 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 a default value of 0 on num, make sure there are no null values ​​in the num column in the table, and then query like this:

  select id from t where num=0

4. Try to avoid using or to join conditions in the where clause, otherwise it will cause the engine to give up using 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. The following query will also result in a full table scan: (the percent sign cannot be prepended)

  select id from t where name like ‘%abc%’

  To improve efficiency, consider full-text search.

6. In and not in should also be used with caution, otherwise it will result in a full table scan, such as:

  select id from t where num in(1,2,3)

  For consecutive values, use between instead of in:

  select id from t where num between 1 and 3

7. 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. Such as:

  select id from t where num/2=100

  Should be changed to:

  select id from t where num=100*2

8. 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.

9. Many times it is a good choice to use exists instead of in:

  select num from a where num in(select num from b)

  Replace with the following statement:

  select num from a where exists(select 1 from b where num=a.num)

10. Not all indexes are valid for queries. SQL optimizes queries based on the data in the table. When a large amount of data in the index column is repeated, the SQL query may not use the index. For example, there are fields sex, male, The female is almost half and half, so even if an index is built on the sex, it will not affect the query efficiency.

11. The more the index, the better. Although the index can improve the efficiency of the corresponding select, it also reduces the efficiency of the insert and update, because the index may be rebuilt during the insert or update, so how to build the index needs to be carefully considered. As the case may be. The number of indexes in a table should not exceed 6. If there are too many indexes, you should consider whether it is necessary to build indexes on some infrequently used columns.

12. Avoid updating the clustered index data column as much as possible, because the order of the clustered index data column is the physical storage order of the table records. Once the value of this column changes, the order of the entire table records will be adjusted, which will consume considerable resources. If the application system needs to update the clustered index data column frequently, it needs to consider whether the index should be built as a clustered index.

13. Use numeric fields as much as possible. If the fields only contain numeric information, try not to design them as character fields, which will reduce the performance of query and connection and increase the storage overhead. This is because the engine compares each character of the string one by one when processing queries and joins, whereas only one comparison is required for numbers.

14. Use varchar/nvarchar instead of char/nchar as much as possible, because first of all, the storage space of variable-length fields is small, which can save storage space. Secondly, for queries, the search efficiency in a relatively small field is obviously higher.

15. Do not use select * from t anywhere, replace "*" with a list of specific fields, and do not return any fields that are not used.

16. Avoid frequent creation and deletion of temporary tables to reduce the consumption of system table resources.

17. Temporary tables are not unusable, and their proper use can make some routines more efficient, for example, when a large table or a dataset in a frequently used table needs to be repeatedly referenced. However, for one-time events, it is better to use an export table.

18. When creating a new temporary table, if a large amount of data is inserted at one time, you can use select into instead of create table to avoid causing a large number of logs to improve the speed; if the amount of data is not large, in order to ease the resources of the system table, you should first create table, then insert.

19. If temporary tables are used, all temporary tables must be explicitly deleted at the end of the stored procedure, first truncate table, and then drop table, which can avoid long-term locking of system tables.

20. Try to avoid using the cursor, because the efficiency of the cursor is poor, if the data operated by the cursor exceeds 10,000 rows, then you should consider rewriting.

21. Before using the cursor-based method or the temporary table method, you should look for a set-based solution to solve the problem, and the set-based method is usually more efficient.

22. Like temporary tables, cursors are not unavailable. Using FAST_FORWARD cursors for small datasets is often preferable to other row-by-row processing methods, especially if several tables must be referenced to obtain the required data. Routines that include "totals" in the result set are usually faster than using cursors. If development time allows, try both the cursor-based approach and the set-based approach to see which one works better.

23. Set SET NOCOUNT ON at the beginning of all stored procedures and triggers, and set SET NOCOUNT OFF at the end. There is no need to send a DONE_IN_PROC message to the client after each statement of stored procedures and triggers is executed.

24. Try to avoid returning a large amount of data to the client. If the amount of data is too large, you should consider whether the corresponding demand is reasonable.

25. Try to avoid large transaction operations and improve system concurrency capabilities.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325149810&siteId=291194637
Recommended