Note:
This topic has been translated from a Chinese forum by GPT and might contain errors.
Original topic: TiDB 中explain format = ‘verbose’ 中是所有算子的estCost是累加起来才是该执行计划的Cost,还是根节点的Cost就代表该执行计划的Cost?
As mentioned, in TiDB, does the estCost of all operators in explain format = ‘verbose’ add up to represent the Cost of the execution plan, or does the Cost of the first operator (root node) represent the Cost of the execution plan?
It should be similar to explain analyze, where the first Cost is the Cost of the entire execution plan.
I didn’t know before that explain has this format usage, learned something new…
The first Cost is the cumulative result.
I’m a bit confused. If the first operator is an accumulator, why do the subsequent operators have a higher cost than the first one?
I think it should be the sum of all accumulated values that represents the actual value.
Currently, TiDB’s computational tasks are divided into two different types: cop tasks and root tasks. Cop tasks refer to computational tasks executed using the Coprocessor in TiKV, while root tasks refer to computational tasks executed in TiDB.
I understand that. How is the total Cost of the execution plan calculated?
The top line’s cost is the total cost at the tidb-server level, while the cost at the tikv level should be viewed as the sum of the costs aggregated at the top of each operator.
Obviously, duplicates cannot be directly added up.
So the total Cost of the execution plan is equal to the sum of the total Cost at the TiDB level and the total Cost at the TiKV level?
You can understand it this way, but although TiKV seems to have a high cost, it is multi-node. A TiKV operator for a single SQL can be processed in parallel by multiple nodes, whereas TiDB-server is just the one you are connected to.
So when there are multiple execution plans, the choice of which execution plan to use is based on the Cost of the execution plan, selecting the one with the lowest Cost. Is the Cost shown here referring to the Cost at the TiDB Server level, or is it the sum of the total Cost at the TiDB level and the total Cost at the TiKV level?
Is there any way to directly find out the total cost of a specific execution plan?
You should definitely choose the one with the lower total cost between tidb-server and tikv, but it’s not a simple addition. The total cost of a certain execution plan can be viewed on the dashboard page at http://127.0.0.1:2379/dashboard/#/statement.