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 root node represent the Cost of the execution plan?

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?

| username: FutureDB

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?

| username: Miracle | Original post link

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…

| username: redgame | Original post link

The first Cost

| username: dba远航 | Original post link

The first Cost is the cumulative result.

| username: FutureDB | Original post link

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?

| username: wangccsy | Original post link

I think it should be the sum of all accumulated values that represents the actual value.

| username: tidb菜鸟一只 | Original post link

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.

| username: FutureDB | Original post link

I understand that. How is the total Cost of the execution plan calculated?

| username: tidb菜鸟一只 | Original post link

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.

| username: zhanggame1 | Original post link

Obviously, duplicates cannot be directly added up.

| username: FutureDB | Original post link

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?

| username: tidb菜鸟一只 | Original post link

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.

| username: FutureDB | Original post link

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?

| username: FutureDB | Original post link

Is there any way to directly find out the total cost of a specific execution plan?

| username: tidb菜鸟一只 | Original post link

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.