Time and Operator Correspondence in Execution Plan

Note:
This topic has been translated from a Chinese forum by GPT and might contain errors.

Original topic: 执行计划中时间与算子对应

| username: h5n1

[v5.2.3 Update Version Not Tested]
As shown in the execution plan in the attachment, after a simple condition filter, there is a group by. The cop task operator that occurs in TiKV has a maximum execution time in milliseconds, but when it comes to the TableReader_12 operator, the recorded cop task max time reaches 2.21 seconds. From the SQL and execution plan, it is inferred that the extra time should be the time for HashAgg_6. However, in the actual execution plan display, the cop_task times for HashAgg_6 and Selection_10 are the same. Is there an error in the display here? The execution time recorded for HashAgg_6 should be more.

Execution Plan.txt (4.7 KB)

| username: 人如其名 | Original post link

The cop_task records the time when the cop request occurs on the TiDB side, including tikvclient-grpc request timenetwork timetikvServer-grpc processing timeprocessing time in TiKV.
Looking at actRows here, after being filtered by Selection_10, the number of records passed to the HashAgg_6 operator in TiKV is 0. Therefore, the HashAgg_6 operator did not consume time in this step (because it processed 0 records). The maximum processing time for the sub-operators (Selection_10, TableFullScan_9) on the TiKV side is proc max: 540ms, so the overall maximum processing time for HashAgg_6 is 540ms.

Since the data returned to the TiDB server is 0, I understand that the cop_task max: 2.21s is mainly spent on gRPC here (possibly due to queuing on either the TiDB server side or the TiKV side). Check if there was a large amount of data being transferred from TiKV to TiDB at that time.

Simple scenario simulation:

The current cluster has only one PD, one TiDB, and one TiKV, all on the same machine.
# No pressure on gRPC: concurrent execution of predicate pushdown -> select * from lineitem where year(L_SHIPDATE) > year(L_COMMITDATE) + 1000
mysql> explain analyze select * from nation where n_name='123';
+-------------------------+---------+---------+-----------+---------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+--------------------------------+-----------+------+
| id                      | estRows | actRows | task      | access object | execution info                                                                                                                                                                                                                                    | operator info                  | memory    | disk |
+-------------------------+---------+---------+-----------+---------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+--------------------------------+-----------+------+
| TableReader_8           | 0.00    | 0       | root      |               | time:598.7µs, loops:1, RU:0.548323, cop_task: {num: 1, max: 530.8µs, proc_keys: 25, tot_proc: 77.7µs, tot_wait: 68.1µs, rpc_num: 1, rpc_time: 508.2µs, copr_cache_hit_ratio: 0.00, build_task_duration: 11.2µs, max_distsql_concurrency: 1}       | data:Selection_7               | 241 Bytes | N/A  |
| └─Selection_7           | 0.00    | 0       | cop[tikv] |               | tikv_task:{time:0s, loops:1}, scan_detail: {total_process_keys: 25, total_process_keys_size: 3109, total_keys: 26, get_snapshot_time: 20µs, rocksdb: {key_skipped_count: 25, block: {}}}                                                          | eq(tpch1.nation.n_name, "123") | N/A       | N/A  |
|   └─TableFullScan_6     | 25.00   | 25      | cop[tikv] | table:nation  | tikv_task:{time:0s, loops:1}                                                                                                                                                                                                                      | keep order:false               | N/A       | N/A  |
+-------------------------+---------+---------+-----------+---------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+--------------------------------+-----------+------+
3 rows in set (0.00 sec)

# Pressure on gRPC: concurrent execution without predicate pushdown -> select * from lineitem where TIMESTAMPDIFF(YEAR,L_SHIPDATE,L_COMMITDATE) > 1000
mysql> explain analyze select * from nation where n_name='123';
+-------------------------+---------+---------+-----------+---------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+--------------------------------+-----------+------+
| id                      | estRows | actRows | task      | access object | execution info                                                                                                                                                                                                                                 | operator info                  | memory    | disk |
+-------------------------+---------+---------+-----------+---------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+--------------------------------+-----------+------+
| TableReader_8           | 0.00    | 0       | root      |               | time:114.9ms, loops:1, RU:0.559090, cop_task: {num: 1, max: 114.8ms, proc_keys: 25, tot_proc: 110µs, tot_wait: 155.7µs, rpc_num: 1, rpc_time: 114.7ms, copr_cache_hit_ratio: 0.00, build_task_duration: 13.3µs, max_distsql_concurrency: 1}    | data:Selection_7               | 243 Bytes | N/A  |
| └─Selection_7           | 0.00    | 0       | cop[tikv] |               | tikv_task:{time:0s, loops:1}, scan_detail: {total_process_keys: 25, total_process_keys_size: 3109, total_keys: 26, get_snapshot_time: 39.9µs, rocksdb: {key_skipped_count: 25, block: {}}}                                                     | eq(tpch1.nation.n_name, "123") | N/A       | N/A  |
|   └─TableFullScan_6     | 25.00   | 25      | cop[tikv] | table:nation  | tikv_task:{time:0s, loops:1}                                                                                                                                                                                                                   | keep order:false               | N/A       | N/A  |
+-------------------------+---------+---------+-----------+---------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+--------------------------------+-----------+------+
3 rows in set (0.12 sec)

You can see that under pressure, gRPC may cause the cop_task time to be long while the tikv_task time is very short. Your issue might be similar to this test.

| username: 人如其名 | Original post link

I found something even stranger: tikv_task:{proc max:264ms is larger than cop_task: {num: 231, max: 122.8ms… This doesn’t make sense!

mysql> set tidb_distsql_scan_concurrency=1;
Query OK, 0 rows affected (0.00 sec)

mysql> explain analyze select L_LINESTATUS,count(*) from lineitem group by L_LINESTATUS;
+-----------------------------+-------------+----------+-----------+----------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------+-----------+------+
| id                          | estRows     | actRows  | task      | access object  | execution info                                                                                                                                                                                                                                                                                                                                                              | operator info                                                                                                                                        | memory    | disk |
+-----------------------------+-------------+----------+-----------+----------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------+-----------+------+
| Projection_4                | 2.00        | 2        | root      |                | time:16.8s, loops:3, RU:136186.721368, Concurrency:OFF                                                                                                                                                                                                                                                                                                                      | tpch10.lineitem.l_linestatus, Column#17                                                                                                              | 10.1 KB   | N/A  |
| └─HashAgg_9                 | 2.00        | 2        | root      |                | time:16.8s, loops:3, partial_worker:{wall_time:16.784835207s, concurrency:5, task_num:1, tot_wait:1m23.923794073s, tot_exec:74.76µs, tot_time:1m23.923876186s, max:16.784780073s, p95:16.784780073s}, final_worker:{wall_time:0s, concurrency:5, task_num:2, tot_wait:1m23.924050297s, tot_exec:10.939µs, tot_time:1m23.924063135s, max:16.784820007s, p95:16.784820007s}   | group by:tpch10.lineitem.l_linestatus, funcs:count(Column#18)->Column#17, funcs:firstrow(tpch10.lineitem.l_linestatus)->tpch10.lineitem.l_linestatus | 42.4 KB   | N/A  |
|   └─TableReader_10          | 2.00        | 462      | root      |                | time:16.8s, loops:2, cop_task: {num: 231, max: 122.8ms, min: 135µs, avg: 72.6ms, p95: 117.5ms, max_proc_keys: 270519, p95_proc_keys: 270481, tot_proc: 16.6s, tot_wait: 14.7ms, rpc_num: 231, rpc_time: 16.8s, copr_cache_hit_ratio: 0.22, build_task_duration: 105.1µs, max_distsql_concurrency: 1}                                                                        | data:HashAgg_5                                                                                                                                       | 483 Bytes | N/A  |
|     └─HashAgg_5             | 2.00        | 462      | cop[tikv] |                | tikv_task:{proc max:264ms, min:62ms, avg: 105.4ms, p80:122ms, p95:172ms, iters:54368, tasks:231}, scan_detail: {total_process_keys: 43624661, total_process_keys_size: 8554694658, total_keys: 43626890, get_snapshot_time: 3.4ms, rocksdb: {key_skipped_count: 43626709, block: {cache_hit_count: 141194, read_count: 129419, read_byte: 1.31 GB, read_time: 307.1ms}}}    | group by:tpch10.lineitem.l_linestatus, funcs:count(1)->Column#18                                                                                     | N/A       | N/A  |
|       └─TableFullScan_8     | 55525508.00 | 55525508 | cop[tikv] | table:lineitem | tikv_task:{proc max:248ms, min:44ms, avg: 91.3ms, p80:108ms, p95:155ms, iters:54368, tasks:231}                                                                                                                                                                                                                                                                             | keep order:false                                                                                                                                     | N/A       | N/A  |
+-----------------------------+-------------+----------+-----------+----------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------+-----------+------+
5 rows in set (16.78 sec)
mysql> select version();
+--------------------+
| version()          |
+--------------------+
| 8.0.11-TiDB-v7.4.0 |
+--------------------+
1 row in set (0.00 sec)