MySQL query optimization scheme

1. Optimization plan

  1. Use indexes: indexes can greatly improve query efficiency, and you can use the EXPLAIN command to check whether the SQL statement uses indexes.

  2. Avoid SELECT *: You should try to avoid using SELECT *, and only query the required data fields, which can reduce data transmission and query overhead.

  3. When you can use INNER JOIN, try not to use IN and EXISTS (join only matches once, and both IN and EXISTS need to match each row of data), otherwise try to use EXISTS. In special cases such as known data and a small amount of data, you can use IN( 1,2,3)

  4. Do not use too many JOINs in a sql query, temporary tables will take up space, and the query efficiency will be affected when the data is too large

  5. Use a suitable storage engine: Different storage engines are suitable for different application scenarios. For example, InnoDB supports transactions and row-level locking, which is suitable for applications with high concurrency and writing; MyISAM does not support transactions and row-level locking, but it has better performance and is suitable for a large number of application for read operations.

  6. Use sub-database and sub-table: If the amount of data is very large, you can use sub-database and sub-table to disperse the query pressure and improve query efficiency.

  7. Adjust the configuration of the MySQL optimizer: further improve query performance by adjusting the size of the query cache and the invalidation strategy of the query cache.

  8. Optimize SQL statements: For complex SQL statements, query performance can be further optimized by decomposing large queries, upgrading the MySQL version, or using better algorithms.

  9. If multiple large tables are associated with join, it may be slower, because each join will generate a temporary table. You can try to optimize as follows

SELECT b.name as name from a LEFT JOIN b on b.id = a.id  
改为
SELECT (SELECT b.name from b WHERE b.id = a.id limit 1) as name from a; 

Pros: After adding , limit 1it will return when a piece of data is queried.
Disadvantages: join will only match once, and the subquery will go to table b to match multiple times
.limit 1

2. Other conclusions

1. BETWEEN AND has the same efficiency as <= and >=

Example: id BETWEEN 1 AND 2, MySQL will convert BETWEEN into a combined condition of >= and <=, so the efficiency of this query is the same as id >= 1 AND id <= 2

2. IN and OR are the same

id IN (1,2) In terms of implementation, MySQL will convert the IN clause into multiple OR conditions, so the efficiency of this query is the same as id = 1 OR id = 2.

In some cases, the IN clause may be more efficient. For example, when multiple values ​​are included in the query condition, using the IN clause can reduce the length of the SQL statement, thereby reducing network transmission and execution time. In addition, using the IN clause can express the intent of the query more clearly, making the SQL statement easier to understand and maintain.

3. The difference between exists and in

exists语法:
SELECT ...
FROM table1
WHERE EXISTS (SELECT ...
              FROM table2
              WHERE ...);
in语法
SELECT ...
FROM table1
WHERE column1 IN (SELECT ...
                  FROM table2
                  WHERE ...);

当使用EXISTS时, the database will execute the subquery first. If the subquery returns at least one row of records, it will return True immediately and will not continue the query, otherwise it will return False. In this process, the data of the outer query will only be retrieved, and will not be put into memory. Therefore, using EXISTS can avoid loading a large amount of data into memory, thereby improving query efficiency.

当使用IN时, is to execute the subquery first, cache the subquery result set in memory, then match each row of the outer query, and finally return the result set. If the data in the subquery is large, the memory consumption is also large.

Summary: When it 外表小里表大is time, try to use exists. If the inner table is small, such as in (1,2,3), you can use in

4. JOIN generates a temporary table

In MySQL, the temporary table generated by the JOIN operation is automatically managed by the MySQL system. The storage method of the temporary table can be configured in the MySQL configuration file, specifically in the following two ways:

By default, MySQL stores temporary tables in memory, using a memory-based temporary table (Memory Storage Engine). This approach improves query performance because memory accesses are faster. However, the memory size is limited. If the amount of data in the temporary table is too large, it will cause insufficient memory.

If the memory is insufficient or the data volume of the temporary table is too large, MySQL will automatically store the temporary table on disk, using a disk-based temporary table (MyISAM Storage Engine). This method can avoid the problem of insufficient memory, but the speed of accessing the disk is slow, which may affect the query performance.

The size of the temporary table in memory can be controlled through the tmp_table_size and max_heap_table_size parameters in the MySQL configuration file, and the default value is 16MB. If the size of the temporary table exceeds this value, MySQL will store the temporary table on disk. It should be noted that the values ​​of these two parameters must be less than or equal to the available space of the partition where the directory specified by the tmpdir parameter in the MySQL configuration file resides.

Therefore, the temporary table generated by the JOIN operation in MySQL is a memory-based temporary table by default, but if the memory is insufficient or the data volume of the temporary table is too large, MySQL will automatically store the temporary table on disk. You can control the size of the temporary table in memory by configuring the tmp_table_size and max_heap_table_size parameters.

Guess you like

Origin blog.csdn.net/weixin_44183847/article/details/129824855