SQL执行速度狂飙,500w数据从35秒到2.5秒的极致优化
01
—
现象
02
—
分析
mysql> select min(start_time),max(start_time) from job_history;+---------------------+---------------------+| min(start_time) | max(start_time) |+---------------------+---------------------+| 2023-12-29 02:36:28 | 2024-01-19 06:44:01 |+---------------------+---------------------+1 row in set (0.02 sec)mysql> show table status like 'job_history'\G*************************** 1. row ***************************Name: job_historyEngine: InnoDBVersion: 10Row_format: DynamicRows: 4819722Avg_row_length: 376Data_length: 1816133632Max_data_length: 0Index_length: 1232748544Data_free: 108003328Auto_increment: 4961289Create_time: 2024-01-23 17:20:22Update_time: NULLCheck_time: NULLCollation: utf8mb4_binChecksum: NULLCreate_options:Comment:1 row in set (0.00 sec)
03
—
start_time > '2024-01-17 02:36:28'改写成一个等价的条件:
id>=(select max(id) from job_history where start_time <= '2024-01-17 02:36:28')测试一下改写后的SQL的运行效率:
04
—