TiDB 如何知晓查询计划里每个算子预估的代价

为提高效率,请提供以下信息,问题描述清晰能够更快得到解决:

【概述】 我想知道如何在查询计划里知晓每个算子基于代价模型预估的代价

【应用框架及开发适配业务逻辑】

【背景】 做过哪些操作

【现象】 业务和数据库现象

【问题】 当前遇到的问题

【业务影响】

【TiDB 版本】 v7.5.1

【附件】 相关日志及监控

无论是 OB,PG 还是等一众主流数据库都有提供这样的信息,不知道 TiDB 如何获取?

你应该是想找这个
https://docs.pingcap.com/zh/tidb/stable/sql-statement-trace#trace

1 个赞

我觉得就是trace 了

肯定根据计划统计信息啊

优化器根据统计信息计算每个算子的代价,你是想了解优化器吗?
揭秘 TiDB 新优化器:Cascades Planner 原理解析 | PingCAP

是要显示每个算子的estcost么?

通用的做法是 统计扫描行的次数、是否使用临时表、是否需要排序

但是每个厂家又有自己微调的地方,这个不好说

是的,我想知道estcost

你好,谢谢,这个是看到每个链路的时间,我想看的是每个算子的预估 cost,类似于:

explain 查看执行计划时,加上 format=verbose ,会显示estcost列

1 个赞

对的,是想了解,但我想通过测试了解,查询计划里获悉不到

原来如此!感谢感谢

你好,想补充咨询一下,所以对于 tidb 的 verbose 格式,整体查询的cost 就是把所有算子的 cost 累加起来是吗?

COST就是花费

学习了

mysql> show variables like '%cost_model%';
+-------------------------+-------+
| Variable_name           | Value |
+-------------------------+-------+
| tidb_cost_model_version | 2     |
+-------------------------+-------+
1 row in set (0.00 sec)

在tidb_cost_model_version = 2的情况下可以查看执行计划cost成本代价计算方式:

mysql> explain analyze format=true_card_cost select * from customer where C_ADDRESS='abc' order by C_PHONE limit 100;
+------------------------------+-----------+-------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+---------+-----------+----------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+--------------------------------------------+-----------+------+
| id                           | estRows   | estCost     | costFormula                                                                                                                                                                                                                                                                                                                                                                                                                                | actRows | task      | access object  | execution info                                                                                                                                                                                                                       | operator info                              | memory    | disk |
+------------------------------+-----------+-------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+---------+-----------+----------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+--------------------------------------------+-----------+------+
| TopN_8                       | 1.43      | 3728257.14  | (((((cpu(150000*filters(1)*tikv_cpu_factor(49.9))) + (scan(150000*logrowsize(242.69)*tikv_scan_factor(40.7)))) + ((exprCPU(0*0*tikv_cpu_factor(49.9))) + (orderCPU(0*log(100)*tikv_cpu_factor(49.9)))) + (topMem(100*216*tikv_mem_factor(0.2)))) + (net(0*rowsize(216)*tidb_kv_net_factor(3.96))))/15.00) + ((exprCPU(0*0*tidb_cpu_factor(49.9))) + (orderCPU(0*log(100)*tidb_cpu_factor(49.9)))) + (topMem(100*216*tidb_mem_factor(0.2))) | 0       | root      |                | time:345.2ms, loops:1                                                                                                                                                                                                                | tpch.customer.c_phone, offset:0, count:100 | 0 Bytes   | N/A  |
| └─TableReader_16             | 1.43      | 3723937.14  | ((((cpu(150000*filters(1)*tikv_cpu_factor(49.9))) + (scan(150000*logrowsize(242.69)*tikv_scan_factor(40.7)))) + ((exprCPU(0*0*tikv_cpu_factor(49.9))) + (orderCPU(0*log(100)*tikv_cpu_factor(49.9)))) + (topMem(100*216*tikv_mem_factor(0.2)))) + (net(0*rowsize(216)*tidb_kv_net_factor(3.96))))/15.00                                                                                                                                    | 0       | root      |                | time:345.1ms, loops:2, cop_task: {num: 1, max: 344.8ms, proc_keys: 150000, tot_proc: 343ms, rpc_num: 1, rpc_time: 344.8ms, copr_cache_hit_ratio: 0.00, distsql_concurrency: 15}                                                      | data:TopN_15                               | 277 Bytes | N/A  |
|   └─TopN_15                  | 1.43      | 55859057.06 | ((cpu(150000*filters(1)*tikv_cpu_factor(49.9))) + (scan(150000*logrowsize(242.69)*tikv_scan_factor(40.7)))) + ((exprCPU(0*0*tikv_cpu_factor(49.9))) + (orderCPU(0*log(100)*tikv_cpu_factor(49.9)))) + (topMem(100*216*tikv_mem_factor(0.2)))                                                                                                                                                                                               | 0       | cop[tikv] |                | tikv_task:{time:343ms, loops:147}, scan_detail: {total_process_keys: 150000, total_process_keys_size: 30533765, total_keys: 150001, get_snapshot_time: 87.9µs, rocksdb: {key_skipped_count: 150000, block: {cache_hit_count: 501}}}  | tpch.customer.c_phone, offset:0, count:100 | N/A       | N/A  |
|     └─Selection_14           | 1.43      | 55854737.06 | (cpu(150000*filters(1)*tikv_cpu_factor(49.9))) + (scan(150000*logrowsize(242.69)*tikv_scan_factor(40.7)))                                                                                                                                                                                                                                                                                                                                  | 0       | cop[tikv] |                | tikv_task:{time:343ms, loops:147}                                                                                                                                                                                                    | eq(tpch.customer.c_address, "abc")         | N/A       | N/A  |
|       └─TableFullScan_13     | 211936.00 | 48369737.06 | scan(150000*logrowsize(242.69)*tikv_scan_factor(40.7))                                                                                                                                                                                                                                                                                                                                                                                     | 150000  | cop[tikv] | table:customer | tikv_task:{time:338ms, loops:147}                                                                                                                                                                                                    | keep order:false                           | N/A       | N/A  |
+------------------------------+-----------+-------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+---------+-----------+----------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+--------------------------------------------+-----------+------+
5 rows in set, 3 warnings (0.37 sec)

总的cost并不是简单的累加,对于存在并行的情况下也会做相应的处理。

3 个赞

dashborad里没有嘛?

非trace不能