【 TiDB 使用环境】生产环境
【 TiDB 版本】V6.5.9
【复现路径】
1.查询慢的信息
1.1, 首次连接后,第一使用 select max(datetime) from tablename 时,延迟到达4.0秒左右
mysql>
mysql> explain analyze select max(DateTime) from TRESmartSumPer_PIT;
±-----------------------------±--------±--------±----------±---------------------------------------------------±-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------±-----------------------------------------------------------±----------±-----+
| id | estRows | actRows | task | access object | execution info | operator info | memory | disk |
±-----------------------------±--------±--------±----------±---------------------------------------------------±-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------±-----------------------------------------------------------±----------±-----+
| StreamAgg_10 | 1.00 | 1 | root | | time:954.7µs, loops:2 | funcs:max(data_pit.tresmartsumper_pit.datetime)->Column#31 | 220 Bytes | N/A |
| └─Limit_14 | 1.00 | 1 | root | | time:947.9µs, loops:2 | offset:0, count:1 | N/A | N/A |
| └─IndexReader_21 | 1.00 | 1 | root | | time:945.3µs, loops:1, cop_task: {num: 1, max: 1.53ms, proc_keys: 1, rpc_num: 1, rpc_time: 1.5ms, copr_cache_hit_ratio: 0.00, distsql_concurrency: 1} | index:Limit_20 | 331 Bytes | N/A |
| └─Limit_20 | 1.00 | 1 | cop[tikv] | | tikv_task:{time:0s, loops:1}, scan_detail: {total_process_keys: 1, total_process_keys_size: 117, total_keys: 2, get_snapshot_time: 33.4µs, rocksdb: {key_skipped_count: 2, block: {cache_hit_count: 15}}} | offset:0, count:1 | N/A | N/A |
| └─IndexFullScan_19 | 1.00 | 1 | cop[tikv] | table:TRESmartSumPer_PIT, index:DateTime(DateTime) | tikv_task:{time:0s, loops:1} | keep order:true, desc | N/A | N/A |
±-----------------------------±--------±--------±----------±---------------------------------------------------±-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------±-----------------------------------------------------------±----------±-----+
5 rows in set (3.52 sec)
1.2 SLOW log
Time: 2024-05-20T17:00:12.783209569+08:00
Txn_start_ts: 449890381783630477
User@Host: wmh[wmh] @ 192.168.202.9 [192.168.202.9]
Conn_ID: 1520453057400460761
Query_time: 3.517811043
Parse_time: 0.000059137
Compile_time: 0.000489663
Rewrite_time: 0.000393289
Optimize_time: 0.000274297
Wait_TS: 0.000011669
Cop_time: 0.000653512 Request_count: 1 Process_keys: 1 Total_keys: 2 Get_snapshot_time: 0.000 Rocksdb_key_skipped_count: 2 Rocksdb_block_cache_hit_co unt: 13 # DB: data_pit # Index_names: [TRESmartSumPer_PIT:DateTime]
Is_internal: false
Digest: 4274f68c3a414ba4dd783291f864a76f3ca99e415acc1fcc66acd8586bf4d232
Stats: TRESmartSumPer_PIT:449479787995725863
Num_cop_tasks: 1
Cop_proc_avg: 0 Cop_proc_addr: 10.0.3.93:20160
Cop_wait_avg: 0 Cop_wait_addr: 10.0.3.93:20160
Mem_max: 500 # Prepared: false
Plan_from_cache: false
Plan_from_binding: false
Has_more_results: false
KV_total: 0.000811246
PD_total: 3.338144114
Backoff_total: 0
Write_sql_response_total: 0.000021161
Result_rows: 0
Succ: true
IsExplicitTxn: false
IsSyncStatsFailed: false
Plan: tidb_decode_plan(‘5wWwMAk1XzEwCTAJMQlmdW5jczptYXgoZGF0YV9waXQudHJlc21hcnRzdW1wZXJfARNYZGF0ZXRpbWUpLT5Db2x1bW4jMzEJMQkBE6A6Njc3LjnCtXMsIGxvb3BzOjIJMjIwIEJ5dGVzCU4vQQoxCTE2XzE0CQFuPG9mZnNldDowLCBjb3VudDodRQgzLjIyRQAITi9BAQQgCjIJMzJfMjEJAT8waW5kZXg6TGltaXRfMgERDYEIMS4zHTwAMQFeaHBfdGFzazoge251bTogMSwgbWF4OiA4MzMuMQUrIHByb2Nfa2V5cwUdDHJwY18RKQEMBVgUIDgwMC42BS7YY29wcl9jYWNoZV9oaXRfcmF0aW86IDAuMDAsIGRpc3RzcWxfY29uY3VycmVuY3k6IDF9CTMzMT0GDDMJMTYFtAxfMAkxWggBBGt2CbUYe3RpbWU6MDVSXDF9LCBzY2FuX2RldGFpbDoge3Rvd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G4gEV1wgzLjIFfQnXADJaLQEYJ4eQlxsuVEItASg6ZnVuY3M6bWF4KFHcEC50cmVzQdsUc3VtcGVyQe9MLmRhdGV0aW1lKS0+Q29sdW1uIzMZggg3LjkyggA43AF4////////////ARgB’)
use data_pit;
explain analyze select max(DateTime) from TRESmartSumPer_PIT;
2.再次执行时,速度就快了
2.1 执行计划
mysql>
mysql> explain analyze select max(DateTime) from TRESmartSumPer_PIT;
±-----------------------------±--------±--------±----------±---------------------------------------------------±------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------±-----------------------------------------------------------±----------±-----+
| id | estRows | actRows | task | access object | execution info | operator info | memory | disk |
±-----------------------------±--------±--------±----------±---------------------------------------------------±------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------±-----------------------------------------------------------±----------±-----+
| StreamAgg_10 | 1.00 | 1 | root | | time:1.25ms, loops:2 | funcs:max(data_pit.tresmartsumper_pit.datetime)->Column#31 | 668 Bytes | N/A |
| └─Limit_14 | 1.00 | 1 | root | | time:1.24ms, loops:2 | offset:0, count:1 | N/A | N/A |
| └─IndexReader_21 | 1.00 | 1 | root | | time:1.24ms, loops:1, cop_task: {num: 1, max: 1.19ms, proc_keys: 1, rpc_num: 1, rpc_time: 1.14ms, copr_cache_hit_ratio: 0.00, distsql_concurrency: 1} | index:Limit_20 | 338 Bytes | N/A |
| └─Limit_20 | 1.00 | 1 | cop[tikv] | | tikv_task:{time:0s, loops:1}, scan_detail: {total_process_keys: 1, total_process_keys_size: 117, total_keys: 2, get_snapshot_time: 329.4µs, rocksdb: {key_skipped_count: 2, block: {cache_hit_count: 13}}} | offset:0, count:1 | N/A | N/A |
| └─IndexFullScan_19 | 1.00 | 1 | cop[tikv] | table:TRESmartSumPer_PIT, index:DateTime(DateTime) | tikv_task:{time:0s, loops:1} | keep order:true, desc | N/A | N/A |
±-----------------------------±--------±--------±----------±---------------------------------------------------±------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------±-----------------------------------------------------------±----------±-----+
5 rows in set (0.05 sec)
2.2 对应的慢SQL
Time: 2024-05-20T17:07:33.29944025+08:00
Txn_start_ts: 449890498175565982
User@Host: wmh[wmh] @ 192.168.202.9 [192.168.202.9]
Conn_ID: 1520453057400460761
Query_time: 0.044330099
Parse_time: 0.000025138
Compile_time: 0.000299687
Rewrite_time: 0.000249411
Optimize_time: 0.000163697
Wait_TS: 0.000005949
Cop_time: 0.000934951 Request_count: 1 Process_keys: 1 Total_keys: 2 Get_snapshot_time: 0.000 Rocksdb_key_skipped_count: 2 Rocksdb_block_cache_hit_co unt: 14 Rocksdb_block_read_count: 1 Rocksdb_block_read_byte: 8595 Rocksdb_block_read_time: 0.000
DB: data_pit # Index_names: [TRESmartSumPer_PIT:DateTime]
Is_internal: false
Digest: 4274f68c3a414ba4dd783291f864a76f3ca99e415acc1fcc66acd8586bf4d232
Stats: TRESmartSumPer_PIT:449479787995725863
Num_cop_tasks: 1
Cop_proc_avg: 0 Cop_proc_addr: 10.0.3.93:20160
Cop_wait_avg: 0 Cop_wait_addr: 10.0.3.93:20160
Mem_max: 1014 # Prepared: false # Plan_from_cache: false
Plan_from_binding: false # Has_more_results: false
KV_total: 0.000918619 # PD_total: 0.000004261
Backoff_total: 0
Write_sql_response_total: 0.000015934
Result_rows: 0
Succ: true
IsExplicitTxn: false
IsSyncStatsFailed: false
Plan: tidb_decode_plan(‘nQawMAk1XzEwCTAJMQlmdW5jczptYXgoZGF0YV9waXQudHJlc21hcnRzdW1wZXJfARNYZGF0ZXRpbWUpLT5Db2x1bW4jMzEJMQkBE6A6OTUzLjHCtXMsIGxvb3BzO jIJNjY4IEJ5dGVzCU4vQQoxCTE2XzE0CQFuPG9mZnNldDowLCBjb3VudDoZRQQ0OTJDAAhOL0EBBCAKMgkzMl8yMQkBPTBpbmRleDpMaW1pdF8yAREJfww0Ny41HTwAMQFcaHBfdGFzazoge251bTog MSwgbWF4OiA4NzYuNAUrIHByb2Nfa2V5cwUdDHJwY18RKQEMBVg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BgxNCwgcmVhFSwAMQ0PMGJ5dGU6IDguMzkgS0INFAl1JDEyLjHCtXN9fX1aRgEYN7bb9wfSPjLhASgBQAFSDmluZGV4Okk+DDIwWhYFUBA5NDcuNQXELZ0UYoMBY29wKboUIHtudW06AZUkbWF4OiA4NzYuNAUsIUstGwGyCHBjXxEpAQwFWRAgODU2LikeEGNvcHJfGfuYcmF0aW86IDAuMDAsIGRpc3RzcWxfY29uY3VycmVuY3k6IDF9cNoCLhoCXtQARhoCJFoUdGltZTo5NDkFqQnVADJaKwEYJ4eQlxsuVEIrASg6ZnVuY3M6bWF4KHESEC50cmVzYREUc3VtcGVyYSUQLmRhdGUBbiwpLT5Db2x1bW4jMzExVwQ1MyWnUfRAMnCcBXj///////////8BGAE=’)
explain analyze select max(DateTime) from TRESmartSumPer_PIT;
【遇到的问题:问题现象及影响】连接TIDB后,第一次执行 select max(DateTime) from tablename 时,速度会出乎意料的慢。
【资源配置】进入到 TiDB Dashboard -集群信息 (Cluster Info) -主机(Hosts) 截图此页面
【附件:截图/日志/监控】
1,表结构