这是另一个优化案例:SQL 优化案例_(1) (qq.com)
在本案中,我填充了100万行测试数据,下面是对比结果
原始SQL运行了387秒还没结束,被我CTRL+C强制终止:
- # Query_time: 387.640089 Lock_time: 0.000002 Rows_sent: 0 Rows_examined: 1755869365 Thread_id: 4355 Errno: 1317 Killed: 0 Bytes_received: 663 Bytes_sent: 266 Read_first: 1757 Read_last: 0 Read_key: 1757 Read_next: 0 Read_prev: 0 Read_rnd: 0 Read_rnd_next: 1755871120 Sort_merge_passes: 0 Sort_range_count: 0 Sort_rows: 0 Sort_scan_count: 0 Created_tmp_disk_tables: 0 Created_tmp_tables: 1 Start: 2024-04-16T15:39:48.025906+08:00 End: 2024-04-16T15:46:15.665995+08:00 Schema: sbtest Rows_affected: 0
- # Tmp_tables: 1 Tmp_disk_tables: 0 Tmp_table_sizes: 85880
- # InnoDB_trx_id: 0
- # Full_scan: Yes Full_join: No Tmp_table: Yes Tmp_table_on_disk: No
- # Filesort: No Filesort_on_disk: No Merge_passes: 0
- # InnoDB_IO_r_ops: 0 InnoDB_IO_r_bytes: 0 InnoDB_IO_r_wait: 0.000000
- # InnoDB_rec_lock_wait: 0.000000 InnoDB_queue_wait: 0.000000
- # InnoDB_pages_distinct: 7485
- SET timestamp=1713253188;
- select S.syctime_day,
- (select count(*) from t1 SS where SS.syctime_day = S.syctime_day and SS.o_source = 'CDE') as 'CDE',
- (select count(*) from t1 SS where SS.syctime_day = S.syctime_day and SS.o_source = 'CDE') as 'SDE',
- (select count(*) from t1 SS where SS.syctime_day = S.syctime_day and SS.o_source = 'CDE') as 'PDE',
- (select count(*) from t1 SS where SS.syctime_day = S.syctime_day and SS.o_source = 'CDE') as 'CSE',
- (select count(*) from t1 SS where SS.syctime_day = S.syctime_day and SS.o_source = 'CDE') as 'SSE'
- from t1 S where S.syctime_day > '2024-01-01' and S.syctime_day < '2024-08-01'
- GROUP BY S.syctime_day order by S.syctime_day asc;
复制代码
改用Rapid引擎(无需改写SQL、无需添加索引等额外工作)后,秒出结果,详情如下:
- # Query_time: 0.022815 Lock_time: 0.000003 Rows_sent: 1 Rows_examined: 0 Thread_id: 4355 Errno: 0 Killed: 0 Bytes_received: 662 Bytes_sent: 223 Read_first: 0 Read_last: 0 Read_key: 0 Read_next: 0 Read_prev: 0 Read_rnd: 0 Read_rnd_next: 0 Sort_merge_passes: 0 Sort_range_count: 0 Sort_rows: 0 Sort_scan_count: 0 Created_tmp_disk_tables: 0 Created_tmp_tables: 0 Start: 2024-04-16T15:57:40.723431+08:00 End: 2024-04-16T15:57:40.746246+08:00 Schema: sbtest Rows_affected: 0
- # Tmp_tables: 0 Tmp_disk_tables: 0 Tmp_table_sizes: 0
- # Full_scan: Yes Full_join: Yes Tmp_table: No Tmp_table_on_disk: No
- # Filesort: No Filesort_on_disk: No Merge_passes: 0
- # No InnoDB statistics available for this query
- SET timestamp=1713254260;
- select S.syctime_day,
- (select count(*) from t2 SS where SS.syctime_day = S.syctime_day and SS.o_source = 'CDE') as 'CDE',
- (select count(*) from t2 SS where SS.syctime_day = S.syctime_day and SS.o_source = 'CDE') as 'SDE',
- (select count(*) from t2 SS where SS.syctime_day = S.syctime_day and SS.o_source = 'CDE') as 'PDE',
- (select count(*) from t2 SS where SS.syctime_day = S.syctime_day and SS.o_source = 'CDE') as 'CSE',
- (select count(*) from t2 SS where SS.syctime_day = S.syctime_day and SS.o_source = 'CDE') as 'SSE'
- from t2 S where S.syctime_day > '2024-01-01' and S.syctime_day < '2024-08-01'
复制代码
效率提升不止数万倍。
以上。
|