因优化器问题导致TPCH的Q5语句执行过慢

【 TiDB 使用环境】测试
【 TiDB 版本】5.7.25-TiDB-v6.5.2
在测试TPCH的Q5语句时候发现执行的特别慢,主要原因怀疑有2点:
1、join reorder 没有对等价关联条件做转换。
2、优化器并没有自动选择成本更低的执行路径。

TPCH sf=30,Q5的语句和执行计划如下:

select 
        n_name,
        sum(l_extendedprice * (1 - l_discount)) as revenue
from
        customer,
        orders,
        lineitem,
        supplier,
        nation,
        region
where
        c_custkey = o_custkey
        and l_orderkey = o_orderkey
        and l_suppkey = s_suppkey
        and c_nationkey = s_nationkey
        and s_nationkey = n_nationkey
        and n_regionkey = r_regionkey
        and r_name = 'ASIA'
        and o_orderdate >= date '1994-01-01'
        and o_orderdate < date '1994-01-01' + interval '1' year
group by
        n_name
order by
        revenue desc;

--执行计划如下(explain format=verbose <sql>):
+----------------------------------------------------+--------------+----------------+-----------+----------------+---------------------------------------------------------------------------------------------------------------------------------+
| id                                                 | estRows      | estCost        | task      | access object  | operator info                                                                                                                   |
+----------------------------------------------------+--------------+----------------+-----------+----------------+---------------------------------------------------------------------------------------------------------------------------------+
| Sort_24                                            | 5.00         | 19000486279.90 | root      |                | Column#48:desc                                                                                                                  |
| └─Projection_26                                    | 5.00         | 19000485635.58 | root      |                | tpch.nation.n_name, Column#48                                                                                                   |
|   └─HashAgg_27                                     | 5.00         | 19000485634.58 | root      |                | group by:Column#51, funcs:sum(Column#49)->Column#48, funcs:firstrow(Column#50)->tpch.nation.n_name                              |
|     └─Projection_85                                | 6907156.71   | 17688680515.14 | root      |                | mul(tpch.lineitem.l_extendedprice, minus(1, tpch.lineitem.l_discount))->Column#49, tpch.nation.n_name, tpch.nation.n_name       |
|       └─Projection_28                              | 6907156.71   | 17618368422.68 | root      |                | tpch.lineitem.l_extendedprice, tpch.lineitem.l_discount, tpch.nation.n_name                                                     |
|         └─HashJoin_37                              | 6907156.71   | 17616300419.96 | root      |                | inner join, equal:[eq(tpch.orders.o_custkey, tpch.customer.c_custkey) eq(tpch.supplier.s_nationkey, tpch.customer.c_nationkey)] |
|           ├─TableReader_83(Build)                  | 4500000.00   | 137274545.64   | root      |                | data:TableFullScan_82                                                                                                           |
|           │ └─TableFullScan_82                     | 4500000.00   | 1488878184.55  | cop[tikv] | table:customer | keep order:false                                                                                                                |
|           └─HashJoin_51(Probe)                     | 6845023.00   | 16571634388.70 | root      |                | inner join, equal:[eq(tpch.lineitem.l_orderkey, tpch.orders.o_orderkey)]                                                        |
|             ├─TableReader_81(Build)                | 6845023.00   | 1344074466.68  | root      |                | data:Selection_80                                                                                                               |
|             │ └─Selection_80                       | 6845023.00   | 18860015028.30 | cop[tikv] |                | ge(tpch.orders.o_orderdate, 1994-01-01), lt(tpch.orders.o_orderdate, 1995-01-01)                                                |
|             │   └─TableFullScan_79                 | 45000000.00  | 14369015028.30 | cop[tikv] | table:orders   | keep order:false                                                                                                                |
|             └─HashJoin_54(Probe)                   | 26520727.87  | 13949359180.45 | root      |                | inner join, equal:[eq(tpch.supplier.s_suppkey, tpch.lineitem.l_suppkey)]                                                        |
|               ├─HashJoin_56(Build)                 | 43848.00     | 15030223.79    | root      |                | inner join, equal:[eq(tpch.nation.n_nationkey, tpch.supplier.s_nationkey)]                                                      |
|               │ ├─HashJoin_69(Build)               | 5.00         | 3005.46        | root      |                | inner join, equal:[eq(tpch.region.r_regionkey, tpch.nation.n_regionkey)]                                                        |
|               │ │ ├─TableReader_74(Build)          | 1.00         | 119.34         | root      |                | data:Selection_73                                                                                                               |
|               │ │ │ └─Selection_73                 | 1.00         | 1675.92        | cop[tikv] |                | eq(tpch.region.r_name, "ASIA")                                                                                                  |
|               │ │ │   └─TableFullScan_72           | 5.00         | 1426.42        | cop[tikv] | table:region   | keep order:false                                                                                                                |
|               │ │ └─TableReader_71(Probe)          | 25.00        | 784.55         | root      |                | data:TableFullScan_70                                                                                                           |
|               │ │   └─TableFullScan_70             | 25.00        | 7306.27        | cop[tikv] | table:nation   | keep order:false                                                                                                                |
|               │ └─TableReader_76(Probe)            | 300000.00    | 9037189.34     | root      |                | data:TableFullScan_75                                                                                                           |
|               │   └─TableFullScan_75               | 300000.00    | 97541840.03    | cop[tikv] | table:supplier | keep order:false                                                                                                                |
|               └─TableReader_78(Probe)              | 179998372.00 | 10336824370.54 | root      |                | data:TableFullScan_77                                                                                                           |
|                 └─TableFullScan_77                 | 179998372.00 | 63814790758.73 | cop[tikv] | table:lineitem | keep order:false                                                                                                                |
+----------------------------------------------------+--------------+----------------+-----------+----------------+---------------------------------------------------------------------------------------------------------------------------------+
24 rows in set, 2 warnings (0.00 sec)
mysql> show warnings;
+-------+------+------------------------------------------------------------------------------------------------+
| Level | Code | Message                                                                                        |
+-------+------+------------------------------------------------------------------------------------------------+
| Note  | 1105 | [customer] remain after pruning paths for customer given Prop{SortItems: [], TaskTp: rootTask} |
| Note  | 1105 | [lineitem] remain after pruning paths for lineitem given Prop{SortItems: [], TaskTp: rootTask} |
+-------+------+------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

--相关执行信息如下:
+----------------------------------------------------+--------------+-----------+-----------+----------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------+-----------+-----------+
| id                                                 | estRows      | actRows   | task      | access object  | execution info                                                                                                                                                                                                                                                                                                                                                                               | operator info                                                                                                                   | memory    | disk      |
+----------------------------------------------------+--------------+-----------+-----------+----------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------+-----------+-----------+
| Sort_24                                            | 5.00         | 5         | root      |                | time:6m39.5s, loops:2                                                                                                                                                                                                                                                                                                                                                                        | Column#48:desc                                                                                                                  | 2.06 KB   | 0 Bytes   |
| └─Projection_26                                    | 5.00         | 5         | root      |                | time:6m39.5s, loops:2, Concurrency:OFF                                                                                                                                                                                                                                                                                                                                                       | tpch.nation.n_name, Column#48                                                                                                   | 2.02 KB   | N/A       |
|   └─HashAgg_27                                     | 5.00         | 5         | root      |                | time:6m39.5s, loops:2                                                                                                                                                                                                                                                                                                                                                                        | group by:Column#51, funcs:sum(Column#49)->Column#48, funcs:firstrow(Column#50)->tpch.nation.n_name                              | 79.4 KB   | 0 Bytes   |
|     └─Projection_85                                | 6907156.71   | 218784    | root      |                | time:6m39.4s, loops:217, Concurrency:5                                                                                                                                                                                                                                                                                                                                                       | mul(tpch.lineitem.l_extendedprice, minus(1, tpch.lineitem.l_discount))->Column#49, tpch.nation.n_name, tpch.nation.n_name       | 968.9 KB  | N/A       |
|       └─Projection_28                              | 6907156.71   | 218784    | root      |                | time:6m39.5s, loops:217, Concurrency:5                                                                                                                                                                                                                                                                                                                                                       | tpch.lineitem.l_extendedprice, tpch.lineitem.l_discount, tpch.nation.n_name                                                     | 1.05 MB   | N/A       |
|         └─HashJoin_37                              | 6907156.71   | 218784    | root      |                | time:6m39.5s, loops:217, build_hash_table:{total:7.49s, fetch:957.5ms, build:6.53s}, probe:{concurrency:5, total:33m17.3s, max:6m39.5s, probe:9.86s, fetch:33m7.4s}                                                                                                                                                                                                                          | inner join, equal:[eq(tpch.orders.o_custkey, tpch.customer.c_custkey) eq(tpch.supplier.s_nationkey, tpch.customer.c_nationkey)] | 424.6 MB  | 0 Bytes   |
|           ├─TableReader_83(Build)                  | 4500000.00   | 4500000   | root      |                | time:214.6ms, loops:4404, cop_task: {num: 170, max: 656.5ms, min: 1.44ms, avg: 102.1ms, p95: 310ms, max_proc_keys: 50144, p95_proc_keys: 50144, tot_proc: 10.6s, tot_wait: 2.58s, rpc_num: 170, rpc_time: 17.3s, copr_cache: disabled, distsql_concurrency: 15}                                                                                                                              | data:TableFullScan_82                                                                                                           | 8.43 MB   | N/A       |
|           │ └─TableFullScan_82                     | 4500000.00   | 4500000   | cop[tikv] | table:customer | tikv_task:{proc max:552ms, min:0s, avg: 61.2ms, p80:113ms, p95:223ms, iters:5066, tasks:170}, scan_detail: {total_process_keys: 4500000, total_process_keys_size: 915730563, total_keys: 4505296, get_snapshot_time: 41.3ms, rocksdb: {delete_skipped_count: 11269, key_skipped_count: 4505126, block: {cache_hit_count: 1485, read_count: 14985, read_byte: 335.9 MB, read_time: 324.5ms}}} | keep order:false                                                                                                                | N/A       | N/A       |
|           └─HashJoin_51(Probe)                     | 6845023.00   | 5476462   | root      |                | time:6m39.4s, loops:5352, build_hash_table:{total:11.6s, fetch:3.71s, build:7.84s}, probe:{concurrency:5, total:33m17.2s, max:6m39.5s, probe:4m55.8s, fetch:28m21.4s}                                                                                                                                                                                                                        | inner join, equal:[eq(tpch.lineitem.l_orderkey, tpch.orders.o_orderkey)]                                                        | 591.6 MB  | 364.5 MB  |
|             ├─TableReader_81(Build)                | 6845023.00   | 6825548   | root      |                | time:3.35s, loops:6645, cop_task: {num: 770, max: 1.03s, min: 1.22ms, avg: 136.9ms, p95: 481.3ms, max_proc_keys: 312379, p95_proc_keys: 217056, tot_proc: 1m18.7s, tot_wait: 8.73s, rpc_num: 770, rpc_time: 1m45.4s, copr_cache: disabled, distsql_concurrency: 15}                                                                                                                          | data:Selection_80                                                                                                               | 9.03 MB   | N/A       |
|             │ └─Selection_80                       | 6845023.00   | 6825548   | cop[tikv] |                | tikv_task:{proc max:993ms, min:0s, avg: 100.3ms, p80:190ms, p95:409ms, iters:47069, tasks:770}, scan_detail: {total_process_keys: 45000000, total_process_keys_size: 6834773077, total_keys: 45000770, get_snapshot_time: 436.2ms, rocksdb: {key_skipped_count: 45000000, block: {cache_hit_count: 6752, read_count: 113731, read_byte: 2.05 GB, read_time: 1.17s}}}                         | ge(tpch.orders.o_orderdate, 1994-01-01), lt(tpch.orders.o_orderdate, 1995-01-01)                                                | N/A       | N/A       |
|             │   └─TableFullScan_79                 | 45000000.00  | 45000000  | cop[tikv] | table:orders   | tikv_task:{proc max:985ms, min:0s, avg: 95.3ms, p80:178ms, p95:403ms, iters:47069, tasks:770}                                                                                                                                                                                                                                                                                                | keep order:false                                                                                                                | N/A       | N/A       |
|             └─HashJoin_54(Probe)                   | 26520727.87  | 36106879  | root      |                | time:6m29s, loops:35265, build_hash_table:{total:1.57s, fetch:1.52s, build:58.1ms}, probe:{concurrency:5, total:33m16.9s, max:6m39.4s, probe:32m7.5s, fetch:1m9.4s}                                                                                                                                                                                                                          | inner join, equal:[eq(tpch.supplier.s_suppkey, tpch.lineitem.l_suppkey)]                                                        | 5.31 MB   | 3.11 MB   |
|               ├─HashJoin_56(Build)                 | 43848.00     | 60186     | root      |                | time:1.57s, loops:64, build_hash_table:{total:196.9µs, fetch:194.5µs, build:2.43µs}, probe:{concurrency:5, total:7.87s, max:1.57s, probe:325.6ms, fetch:7.54s}                                                                                                                                                                                                                               | inner join, equal:[eq(tpch.nation.n_nationkey, tpch.supplier.s_nationkey)]                                                      | 2.21 KB   | 191 Bytes |
|               │ ├─HashJoin_69(Build)               | 5.00         | 5         | root      |                | time:173.5µs, loops:2, build_hash_table:{total:85.9µs, fetch:80.2µs, build:5.71µs}, probe:{concurrency:5, total:269.6µs, max:62.1µs, probe:20.9µs, fetch:248.6µs}                                                                                                                                                                                                                            | inner join, equal:[eq(tpch.region.r_regionkey, tpch.nation.n_regionkey)]                                                        | 25.7 KB   | 36 Bytes  |
|               │ │ ├─TableReader_74(Build)          | 1.00         | 1         | root      |                | time:55.4µs, loops:2, cop_task: {num: 1, max: 990.3µs, proc_keys: 5, rpc_num: 1, rpc_time: 956.5µs, copr_cache: disabled, distsql_concurrency: 15}                                                                                                                                                                                                                                           | data:Selection_73                                                                                                               | 267 Bytes | N/A       |
|               │ │ │ └─Selection_73                 | 1.00         | 1         | cop[tikv] |                | tikv_task:{time:0s, loops:1}, scan_detail: {total_process_keys: 5, total_process_keys_size: 559, total_keys: 6, get_snapshot_time: 191.4µs, rocksdb: {key_skipped_count: 5, block: {cache_hit_count: 9}}}                                                                                                                                                                                    | eq(tpch.region.r_name, "ASIA")                                                                                                  | N/A       | N/A       |
|               │ │ │   └─TableFullScan_72           | 5.00         | 5         | cop[tikv] | table:region   | tikv_task:{time:0s, loops:1}                                                                                                                                                                                                                                                                                                                                                                 | keep order:false                                                                                                                | N/A       | N/A       |
|               │ │ └─TableReader_71(Probe)          | 25.00        | 25        | root      |                | time:42.4µs, loops:2, cop_task: {num: 1, max: 1.09ms, proc_keys: 25, rpc_num: 1, rpc_time: 1.07ms, copr_cache: disabled, distsql_concurrency: 15}                                                                                                                                                                                                                                            | data:TableFullScan_70                                                                                                           | 1.00 KB   | N/A       |
|               │ │   └─TableFullScan_70             | 25.00        | 25        | cop[tikv] | table:nation   | tikv_task:{time:0s, loops:1}, scan_detail: {total_process_keys: 25, total_process_keys_size: 3109, total_keys: 26, get_snapshot_time: 107.2µs, rocksdb: {key_skipped_count: 25, block: {cache_hit_count: 9}}}                                                                                                                                                                                | keep order:false                                                                                                                | N/A       | N/A       |
|               │ └─TableReader_76(Probe)            | 300000.00    | 300000    | root      |                | time:1.53s, loops:295, cop_task: {num: 14, max: 410.3ms, min: 1.67ms, avg: 112.1ms, p95: 410.3ms, max_proc_keys: 50144, p95_proc_keys: 50144, tot_proc: 956ms, tot_wait: 397ms, rpc_num: 14, rpc_time: 1.57s, copr_cache: disabled, distsql_concurrency: 15}                                                                                                                                 | data:TableFullScan_75                                                                                                           | 1.53 MB   | N/A       |
|               │   └─TableFullScan_75               | 300000.00    | 300000    | cop[tikv] | table:supplier | tikv_task:{proc max:331ms, min:0s, avg: 62.4ms, p80:114ms, p95:331ms, iters:348, tasks:14}, scan_detail: {total_process_keys: 300000, total_process_keys_size: 54436630, total_keys: 300014, get_snapshot_time: 14.9ms, rocksdb: {key_skipped_count: 300000, block: {cache_hit_count: 122, read_count: 898, read_byte: 20.7 MB, read_time: 8.55ms}}}                                         | keep order:false                                                                                                                | N/A       | N/A       |
|               └─TableReader_78(Probe)              | 179998372.00 | 179998372 | root      |                | time:3.45s, loops:176140, cop_task: {num: 6613, max: 606.7ms, min: 776.4µs, avg: 70.1ms, p95: 164.8ms, max_proc_keys: 50144, p95_proc_keys: 50144, tot_proc: 5m14.1s, tot_wait: 10.8s, rpc_num: 6613, rpc_time: 7m42.8s, copr_cache: disabled, distsql_concurrency: 15}                                                                                                                      | data:TableFullScan_77                                                                                                           | 73.5 MB   | N/A       |
|                 └─TableFullScan_77                 | 179998372.00 | 179998372 | cop[tikv] | table:lineitem | tikv_task:{proc max:529ms, min:0s, avg: 37.9ms, p80:66ms, p95:105ms, iters:201899, tasks:6613}, scan_detail: {total_process_keys: 179998372, total_process_keys_size: 35379740257, total_keys: 180004985, get_snapshot_time: 1.67s, rocksdb: {key_skipped_count: 179998372, block: {cache_hit_count: 78559, read_count: 537544, read_byte: 8.30 GB, read_time: 8.09s}}}                      | keep order:false                                                                                                                | N/A       | N/A       |
+----------------------------------------------------+--------------+-----------+-----------+----------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------+-----------+-----------+
24 rows in set (6 min 39.50 sec)

该语句总的cost为:19000486279.90 ,语句执行耗时6分多钟,从上面执行信息中可以看出最耗时的部分是:HashJoin_54,且主要时间花费在hashjoin的probe端,也就是TableReader_78,对lineitem的读取耗时最高。
分析该SQL语句,经过region的r_name = 'ASIA’的过滤数据量为原来的20%,orders的o_orderdate条件过滤orders表数据量由总的45000000变为6845023,过滤后数据量为原来的15%,因此如果先走这两个过滤条件再和lineitem表走inl_join,那么效率可能会更高。根据关联条件表的选择路径为:region,nation,customer,orders,lineitem,supplier。

mysql> explain  select count(*) from region where  r_name = 'ASIA';
+----------------------------+---------+-----------+---------------+--------------------------------+
| id                         | estRows | task      | access object | operator info                  |
+----------------------------+---------+-----------+---------------+--------------------------------+
| StreamAgg_10               | 1.00    | root      |               | funcs:count(1)->Column#4       |
| └─TableReader_17           | 1.00    | root      |               | data:Selection_16              |
|   └─Selection_16           | 1.00    | cop[tikv] |               | eq(tpch.region.r_name, "ASIA") |
|     └─TableFullScan_15     | 5.00    | cop[tikv] | table:region  | keep order:false               |
+----------------------------+---------+-----------+---------------+--------------------------------+
4 rows in set (0.01 sec)

mysql> explain select count(*) from orders where o_orderdate >= date '1994-01-01' and o_orderdate < date '1994-01-01' + interval '1' year;
+------------------------------+-------------+-----------+---------------+----------------------------------------------------------------------------------+
| id                           | estRows     | task      | access object | operator info                                                                    |
+------------------------------+-------------+-----------+---------------+----------------------------------------------------------------------------------+
| StreamAgg_20                 | 1.00        | root      |               | funcs:count(Column#12)->Column#10                                                |
| └─TableReader_21             | 1.00        | root      |               | data:StreamAgg_9                                                                 |
|   └─StreamAgg_9              | 1.00        | cop[tikv] |               | funcs:count(1)->Column#12                                                        |
|     └─Selection_19           | 6845023.00  | cop[tikv] |               | ge(tpch.orders.o_orderdate, 1994-01-01), lt(tpch.orders.o_orderdate, 1995-01-01) |
|       └─TableFullScan_18     | 45000000.00 | cop[tikv] | table:orders  | keep order:false                                                                 |
+------------------------------+-------------+-----------+---------------+----------------------------------------------------------------------------------+
5 rows in set (0.00 sec)

强制执行region,nation,customer,orders,lineitem,supplier的执行路径观察执行计划:

explain format=verbose
select /*+ LEADING(region,nation,customer,orders,lineitem,supplier) */
        n_name,
        sum(l_extendedprice * (1 - l_discount)) as revenue
from
        customer,
        orders,
        lineitem,
        supplier,
        nation,
        region
where
        c_custkey = o_custkey
        and l_orderkey = o_orderkey
        and l_suppkey = s_suppkey
        and c_nationkey = s_nationkey
        and s_nationkey = n_nationkey
        and n_regionkey = r_regionkey
        and r_name = 'ASIA'
        and o_orderdate >= date '1994-01-01'
        and o_orderdate < date '1994-01-01' + interval '1' year
group by
        n_name
order by
        revenue desc;

+----------------------------------------------------+--------------+----------------+-----------+----------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| id                                                 | estRows      | estCost        | task      | access object  | operator info                                                                                                                                                                            |
+----------------------------------------------------+--------------+----------------+-----------+----------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Sort_22                                            | 5.00         | 54405488396.22 | root      |                | Column#48:desc                                                                                                                                                                           |
| └─Projection_24                                    | 5.00         | 54405487751.90 | root      |                | tpch.nation.n_name, Column#48                                                                                                                                                            |
|   └─HashAgg_25                                     | 5.00         | 54405487750.90 | root      |                | group by:Column#51, funcs:sum(Column#49)->Column#48, funcs:firstrow(Column#50)->tpch.nation.n_name                                                                                       |
|     └─Projection_81                                | 141292146.77 | 27571365950.04 | root      |                | mul(tpch.lineitem.l_extendedprice, minus(1, tpch.lineitem.l_discount))->Column#49, tpch.nation.n_name, tpch.nation.n_name                                                                |
|       └─Projection_26                              | 141292146.77 | 26133068412.75 | root      |                | tpch.lineitem.l_extendedprice, tpch.lineitem.l_discount, tpch.nation.n_name                                                                                                              |
|         └─HashJoin_35                              | 141292146.77 | 26090765544.01 | root      |                | inner join, equal:[eq(tpch.lineitem.l_suppkey, tpch.supplier.s_suppkey) eq(tpch.customer.c_nationkey, tpch.supplier.s_nationkey) eq(tpch.nation.n_nationkey, tpch.supplier.s_nationkey)] |
|           ├─TableReader_79(Build)                  | 300000.00    | 9037189.34     | root      |                | data:TableFullScan_78                                                                                                                                                                    |
|           │ └─TableFullScan_78                     | 300000.00    | 97541840.03    | cop[tikv] | table:supplier | keep order:false                                                                                                                                                                         |
|           └─IndexHashJoin_44(Probe)                | 140161809.60 | 20424667418.49 | root      |                | inner join, inner:TableReader_39, outer key:tpch.orders.o_orderkey, inner key:tpch.lineitem.l_orderkey, equal cond:eq(tpch.orders.o_orderkey, tpch.lineitem.l_orderkey)                  |
|             ├─HashJoin_49(Build)                   | 34535783.56  | 2724210810.47  | root      |                | inner join, equal:[eq(tpch.customer.c_custkey, tpch.orders.o_custkey)]                                                                                                                   |
|             │ ├─TableReader_75(Build)              | 6845023.00   | 1344074466.68  | root      |                | data:Selection_74                                                                                                                                                                        |
|             │ │ └─Selection_74                     | 6845023.00   | 18860015028.30 | cop[tikv] |                | ge(tpch.orders.o_orderdate, 1994-01-01), lt(tpch.orders.o_orderdate, 1995-01-01)                                                                                                         |
|             │ │   └─TableFullScan_73               | 45000000.00  | 14369015028.30 | cop[tikv] | table:orders   | keep order:false                                                                                                                                                                         |
|             │ └─HashJoin_52(Probe)                 | 22500000.00  | 182189330.59   | root      |                | CARTESIAN inner join                                                                                                                                                                     |
|             │   ├─HashJoin_65(Build)               | 5.00         | 3005.46        | root      |                | inner join, equal:[eq(tpch.region.r_regionkey, tpch.nation.n_regionkey)]                                                                                                                 |
|             │   │ ├─TableReader_70(Build)          | 1.00         | 119.34         | root      |                | data:Selection_69                                                                                                                                                                        |
|             │   │ │ └─Selection_69                 | 1.00         | 1675.92        | cop[tikv] |                | eq(tpch.region.r_name, "ASIA")                                                                                                                                                           |
|             │   │ │   └─TableFullScan_68           | 5.00         | 1426.42        | cop[tikv] | table:region   | keep order:false                                                                                                                                                                         |
|             │   │ └─TableReader_67(Probe)          | 25.00        | 784.55         | root      |                | data:TableFullScan_66                                                                                                                                                                    |
|             │   │   └─TableFullScan_66             | 25.00        | 7306.27        | cop[tikv] | table:nation   | keep order:false                                                                                                                                                                         |
|             │   └─TableReader_72(Probe)            | 4500000.00   | 137274545.64   | root      |                | data:TableFullScan_71                                                                                                                                                                    |
|             │     └─TableFullScan_71               | 4500000.00   | 1488878184.55  | cop[tikv] | table:customer | keep order:false                                                                                                                                                                         |
|             └─TableReader_39(Probe)                | 34535783.56  | 47.55          | root      |                | data:TableRangeScan_38                                                                                                                                                                   |
|               └─TableRangeScan_38                  | 34535783.56  | 333.12         | cop[tikv] | table:lineitem | range: decided by [eq(tpch.lineitem.l_orderkey, tpch.orders.o_orderkey)], keep order:false                                                                                               |
+----------------------------------------------------+--------------+----------------+-----------+----------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
24 rows in set, 1 warning (0.00 sec)

mysql> show warnings;
+-------+------+------------------------------------------------------------------------------------------------+
| Level | Code | Message                                                                                        |
+-------+------+------------------------------------------------------------------------------------------------+
| Note  | 1105 | [customer] remain after pruning paths for customer given Prop{SortItems: [], TaskTp: rootTask} |
+-------+------+------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

--该语句不进行执行,因为发生了笛卡尔积执行效率非常低下

经过强制指定连接顺序后,cost为:54405488396.22 大于未指定连接顺序时候的19000486279.90。通过观察上面的执行计划可以看到HashJoin_52这一步发生了笛卡尔积,原因是强制指定了region,nation,customer,orders,lineitem,supplier的连接顺序后region,nation输出的字段并没有和customer存在关联条件导致了笛卡尔积的产生,但是通过观察整体的关联条件:
where
c_custkey = o_custkey
and l_orderkey = o_orderkey
and l_suppkey = s_suppkey
and c_nationkey = s_nationkey
** and s_nationkey = n_nationkey**
and n_regionkey = r_regionkey
and r_name = ‘ASIA’
and o_orderdate >= date ‘1994-01-01’
and o_orderdate < date ‘1994-01-01’ + interval ‘1’ year
可以看到customer的c_nationkey是和supplier的s_nationkey做的关联,在指定连接顺序后join reorder并没有对关联条件做等价转换即:and c_nationkey = s_nationkey 转变为:and c_nationkey = n_nationkey。
接下来我们手工对条件进行转换如下:
where
c_custkey = o_custkey
and l_orderkey = o_orderkey
and l_suppkey = s_suppkey
and c_nationkey = n_nationkey
and s_nationkey = n_nationkey
and n_regionkey = r_regionkey
and r_name = ‘ASIA’
and o_orderdate >= date ‘1994-01-01’
and o_orderdate < date ‘1994-01-01’ + interval ‘1’ year
观察其执行计划:

explain format=verbose
select /*+ LEADING(region,nation,customer,orders,lineitem,supplier) */
        n_name,
        sum(l_extendedprice * (1 - l_discount)) as revenue
from
        customer,
        orders,
        lineitem,
        supplier,
        nation,
        region
where
        c_custkey = o_custkey
        and l_orderkey = o_orderkey
        and l_suppkey = s_suppkey
        and c_nationkey = n_nationkey
        and s_nationkey = n_nationkey
        and n_regionkey = r_regionkey
        and r_name = 'ASIA'
        and o_orderdate >= date '1994-01-01'
        and o_orderdate < date '1994-01-01' + interval '1' year
group by
        n_name
order by
        revenue desc;

+------------------------------------------------------+-------------+----------------+-----------+----------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| id                                                   | estRows     | estCost        | task      | access object  | operator info                                                                                                                                                           |
+------------------------------------------------------+-------------+----------------+-----------+----------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Sort_22                                              | 5.00        | 11808740465.25 | root      |                | Column#48:desc                                                                                                                                                          |
| └─Projection_24                                      | 5.00        | 11808739820.93 | root      |                | tpch.nation.n_name, Column#48                                                                                                                                           |
|   └─HashAgg_25                                       | 5.00        | 11808739819.93 | root      |                | group by:Column#51, funcs:sum(Column#49)->Column#48, funcs:firstrow(Column#50)->tpch.nation.n_name                                                                      |
|     └─Projection_81                                  | 28004229.08 | 6490191373.94  | root      |                | mul(tpch.lineitem.l_extendedprice, minus(1, tpch.lineitem.l_discount))->Column#49, tpch.nation.n_name, tpch.nation.n_name                                               |
|       └─Projection_26                                | 28004229.08 | 6205119523.56  | root      |                | tpch.lineitem.l_extendedprice, tpch.lineitem.l_discount, tpch.nation.n_name                                                                                             |
|         └─HashJoin_35                                | 28004229.08 | 6196735057.37  | root      |                | inner join, equal:[eq(tpch.lineitem.l_suppkey, tpch.supplier.s_suppkey) eq(tpch.nation.n_nationkey, tpch.supplier.s_nationkey)]                                         |
|           ├─TableReader_79(Build)                    | 300000.00   | 9037189.34     | root      |                | data:TableFullScan_78                                                                                                                                                   |
|           │ └─TableFullScan_78                       | 300000.00   | 97541840.03    | cop[tikv] | table:supplier | keep order:false                                                                                                                                                        |
|           └─IndexHashJoin_44(Probe)                  | 27780195.25 | 5309127325.23  | root      |                | inner join, inner:TableReader_39, outer key:tpch.orders.o_orderkey, inner key:tpch.lineitem.l_orderkey, equal cond:eq(tpch.orders.o_orderkey, tpch.lineitem.l_orderkey) |
|             ├─HashJoin_50(Build)                     | 6845023.00  | 1803070298.85  | root      |                | inner join, equal:[eq(tpch.customer.c_custkey, tpch.orders.o_custkey)]                                                                                                  |
|             │ ├─HashJoin_52(Build)                   | 882112.00   | 227099580.09   | root      |                | inner join, equal:[eq(tpch.nation.n_nationkey, tpch.customer.c_nationkey)]                                                                                              |
|             │ │ ├─HashJoin_65(Build)                 | 5.00        | 3005.46        | root      |                | inner join, equal:[eq(tpch.region.r_regionkey, tpch.nation.n_regionkey)]                                                                                                |
|             │ │ │ ├─TableReader_70(Build)            | 1.00        | 119.34         | root      |                | data:Selection_69                                                                                                                                                       |
|             │ │ │ │ └─Selection_69                   | 1.00        | 1675.92        | cop[tikv] |                | eq(tpch.region.r_name, "ASIA")                                                                                                                                          |
|             │ │ │ │   └─TableFullScan_68             | 5.00        | 1426.42        | cop[tikv] | table:region   | keep order:false                                                                                                                                                        |
|             │ │ │ └─TableReader_67(Probe)            | 25.00       | 784.55         | root      |                | data:TableFullScan_66                                                                                                                                                   |
|             │ │ │   └─TableFullScan_66               | 25.00       | 7306.27        | cop[tikv] | table:nation   | keep order:false                                                                                                                                                        |
|             │ │ └─TableReader_72(Probe)              | 4500000.00  | 137274545.64   | root      |                | data:TableFullScan_71                                                                                                                                                   |
|             │ │   └─TableFullScan_71                 | 4500000.00  | 1488878184.55  | cop[tikv] | table:customer | keep order:false                                                                                                                                                        |
|             │ └─TableReader_75(Probe)                | 6845023.00  | 1344074466.68  | root      |                | data:Selection_74                                                                                                                                                       |
|             │   └─Selection_74                       | 6845023.00  | 18860015028.30 | cop[tikv] |                | ge(tpch.orders.o_orderdate, 1994-01-01), lt(tpch.orders.o_orderdate, 1995-01-01)                                                                                        |
|             │     └─TableFullScan_73                 | 45000000.00 | 14369015028.30 | cop[tikv] | table:orders   | keep order:false                                                                                                                                                        |
|             └─TableReader_39(Probe)                  | 6845023.00  | 47.55          | root      |                | data:TableRangeScan_38                                                                                                                                                  |
|               └─TableRangeScan_38                    | 6845023.00  | 333.12         | cop[tikv] | table:lineitem | range: decided by [eq(tpch.lineitem.l_orderkey, tpch.orders.o_orderkey)], keep order:false                                                                              |
+------------------------------------------------------+-------------+----------------+-----------+----------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
24 rows in set, 1 warning (0.01 sec)

mysql> show warnings;
+-------+------+------------------------------------------------------------------------------------------------+
| Level | Code | Message                                                                                        |
+-------+------+------------------------------------------------------------------------------------------------+
| Note  | 1105 | [customer] remain after pruning paths for customer given Prop{SortItems: [], TaskTp: rootTask} |
+-------+------+------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

可以看到走了理想的执行计划,观察其执行耗时:

+------------------------------------------------------+-------------+----------+-----------+----------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-----------+---------+
| id                                                   | estRows     | actRows  | task      | access object  | execution info                                                                                                                                                                                                                                                                                                                                                                             | operator info                                                                                                                                                           | memory    | disk    |
+------------------------------------------------------+-------------+----------+-----------+----------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-----------+---------+
| Sort_22                                              | 5.00        | 5        | root      |                | time:34.7s, loops:2                                                                                                                                                                                                                                                                                                                                                                        | Column#48:desc                                                                                                                                                          | 2.69 KB   | 0 Bytes |
| └─Projection_24                                      | 5.00        | 5        | root      |                | time:34.7s, loops:2, Concurrency:OFF                                                                                                                                                                                                                                                                                                                                                       | tpch.nation.n_name, Column#48                                                                                                                                           | 2.65 KB   | N/A     |
|   └─HashAgg_25                                       | 5.00        | 5        | root      |                | time:34.7s, loops:2                                                                                                                                                                                                                                                                                                                                                                        | group by:Column#51, funcs:sum(Column#49)->Column#48, funcs:firstrow(Column#50)->tpch.nation.n_name                                                                      | 79.4 KB   | 0 Bytes |
|     └─Projection_81                                  | 28004229.08 | 218784   | root      |                | time:34.5s, loops:217, Concurrency:5                                                                                                                                                                                                                                                                                                                                                       | mul(tpch.lineitem.l_extendedprice, minus(1, tpch.lineitem.l_discount))->Column#49, tpch.nation.n_name, tpch.nation.n_name                                               | 968.9 KB  | N/A     |
|       └─Projection_26                                | 28004229.08 | 218784   | root      |                | time:34.7s, loops:217, Concurrency:5                                                                                                                                                                                                                                                                                                                                                       | tpch.lineitem.l_extendedprice, tpch.lineitem.l_discount, tpch.nation.n_name                                                                                             | 1.05 MB   | N/A     |
|         └─HashJoin_35                                | 28004229.08 | 218784   | root      |                | time:34.7s, loops:217, build_hash_table:{total:701.8ms, fetch:463.7ms, build:238.1ms}, probe:{concurrency:5, total:2m53.4s, max:34.7s, probe:5.36s, fetch:2m48s}                                                                                                                                                                                                                           | inner join, equal:[eq(tpch.lineitem.l_suppkey, tpch.supplier.s_suppkey) eq(tpch.nation.n_nationkey, tpch.supplier.s_nationkey)]                                         | 27.1 MB   | 0 Bytes |
|           ├─TableReader_79(Build)                    | 300000.00   | 300000   | root      |                | time:462.4ms, loops:295, cop_task: {num: 14, max: 119.7ms, min: 897.9µs, avg: 44.7ms, p95: 119.7ms, max_proc_keys: 50144, p95_proc_keys: 50144, tot_proc: 420ms, tot_wait: 20ms, rpc_num: 14, rpc_time: 625.8ms, copr_cache: disabled, distsql_concurrency: 15}                                                                                                                            | data:TableFullScan_78                                                                                                                                                   | 1.53 MB   | N/A     |
|           │ └─TableFullScan_78                       | 300000.00   | 300000   | cop[tikv] | table:supplier | tikv_task:{proc max:115ms, min:0s, avg: 27.4ms, p80:65ms, p95:115ms, iters:348, tasks:14}, scan_detail: {total_process_keys: 300000, total_process_keys_size: 54436630, total_keys: 300014, get_snapshot_time: 588.7µs, rocksdb: {key_skipped_count: 300000, block: {cache_hit_count: 132, read_count: 888, read_byte: 20.4 MB, read_time: 8.52ms}}}                                       | keep order:false                                                                                                                                                        | N/A       | N/A     |
|           └─IndexHashJoin_44(Probe)                  | 27780195.25 | 5450573  | root      |                | time:34.6s, loops:5326, inner:{total:2m43.5s, concurrency:5, task:61, construct:2.62s, fetch:2m33.7s, build:575.6ms, join:7.17s}                                                                                                                                                                                                                                                           | inner join, inner:TableReader_39, outer key:tpch.orders.o_orderkey, inner key:tpch.lineitem.l_orderkey, equal cond:eq(tpch.orders.o_orderkey, tpch.lineitem.l_orderkey) | 67.7 MB   | N/A     |
|             ├─HashJoin_50(Build)                     | 6845023.00  | 1362236  | root      |                | time:17.5s, loops:1335, build_hash_table:{total:1.41s, fetch:519.1ms, build:887.5ms}, probe:{concurrency:5, total:2m41.5s, max:32.3s, probe:1m26.5s, fetch:1m15s}                                                                                                                                                                                                                          | inner join, equal:[eq(tpch.customer.c_custkey, tpch.orders.o_custkey)]                                                                                                  | 77.5 MB   | 0 Bytes |
|             │ ├─HashJoin_52(Build)                   | 882112.00   | 899362   | root      |                | time:406.7ms, loops:883, build_hash_table:{total:30ms, fetch:30ms, build:6.66µs}, probe:{concurrency:5, total:7.02s, max:1.41s, probe:5.44s, fetch:1.58s}                                                                                                                                                                                                                                  | inner join, equal:[eq(tpch.nation.n_nationkey, tpch.customer.c_nationkey)]                                                                                              | 2.21 KB   | 0 Bytes |
|             │ │ ├─HashJoin_65(Build)                 | 5.00        | 5        | root      |                | time:30ms, loops:2, build_hash_table:{total:29.9ms, fetch:29.9ms, build:7.93µs}, probe:{concurrency:5, total:149.5ms, max:29.9ms, probe:37.4µs, fetch:149.5ms}                                                                                                                                                                                                                             | inner join, equal:[eq(tpch.region.r_regionkey, tpch.nation.n_regionkey)]                                                                                                | 25.7 KB   | 0 Bytes |
|             │ │ │ ├─TableReader_70(Build)            | 1.00        | 1        | root      |                | time:29.8ms, loops:2, cop_task: {num: 1, max: 30.8ms, proc_keys: 5, rpc_num: 1, rpc_time: 30.7ms, copr_cache: disabled, distsql_concurrency: 15}                                                                                                                                                                                                                                           | data:Selection_69                                                                                                                                                       | 269 Bytes | N/A     |
|             │ │ │ │ └─Selection_69                   | 1.00        | 1        | cop[tikv] |                | tikv_task:{time:0s, loops:1}, scan_detail: {total_process_keys: 5, total_process_keys_size: 559, total_keys: 6, get_snapshot_time: 229.8µs, rocksdb: {key_skipped_count: 5, block: {cache_hit_count: 9}}}                                                                                                                                                                                  | eq(tpch.region.r_name, "ASIA")                                                                                                                                          | N/A       | N/A     |
|             │ │ │ │   └─TableFullScan_68             | 5.00        | 5        | cop[tikv] | table:region   | tikv_task:{time:0s, loops:1}                                                                                                                                                                                                                                                                                                                                                               | keep order:false                                                                                                                                                        | N/A       | N/A     |
|             │ │ │ └─TableReader_67(Probe)            | 25.00       | 25       | root      |                | time:7.85ms, loops:2, cop_task: {num: 1, max: 8.72ms, proc_keys: 25, rpc_num: 1, rpc_time: 8.71ms, copr_cache: disabled, distsql_concurrency: 15}                                                                                                                                                                                                                                          | data:TableFullScan_66                                                                                                                                                   | 1.00 KB   | N/A     |
|             │ │ │   └─TableFullScan_66               | 25.00       | 25       | cop[tikv] | table:nation   | tikv_task:{time:0s, loops:1}, scan_detail: {total_process_keys: 25, total_process_keys_size: 3109, total_keys: 26, get_snapshot_time: 519.1µs, rocksdb: {key_skipped_count: 25, block: {cache_hit_count: 9}}}                                                                                                                                                                              | keep order:false                                                                                                                                                        | N/A       | N/A     |
|             │ │ └─TableReader_72(Probe)              | 4500000.00  | 4500000  | root      |                | time:258.4ms, loops:4405, cop_task: {num: 170, max: 177.1ms, min: 946.4µs, avg: 53.4ms, p95: 120.6ms, max_proc_keys: 50144, p95_proc_keys: 50144, tot_proc: 6.03s, tot_wait: 271ms, rpc_num: 170, rpc_time: 9.07s, copr_cache: disabled, distsql_concurrency: 15}                                                                                                                          | data:TableFullScan_71                                                                                                                                                   | 7.67 MB   | N/A     |
|             │ │   └─TableFullScan_71                 | 4500000.00  | 4500000  | cop[tikv] | table:customer | tikv_task:{proc max:168ms, min:0s, avg: 32.9ms, p80:61ms, p95:98ms, iters:5066, tasks:170}, scan_detail: {total_process_keys: 4500000, total_process_keys_size: 915730563, total_keys: 4505296, get_snapshot_time: 17.8ms, rocksdb: {delete_skipped_count: 11269, key_skipped_count: 4505126, block: {cache_hit_count: 1582, read_count: 14888, read_byte: 333.7 MB, read_time: 205.6ms}}} | keep order:false                                                                                                                                                        | N/A       | N/A     |
|             │ └─TableReader_75(Probe)                | 6845023.00  | 6825548  | root      |                | time:13.2s, loops:6645, cop_task: {num: 770, max: 2.04s, min: 4.07ms, avg: 355.7ms, p95: 924.9ms, max_proc_keys: 312379, p95_proc_keys: 217056, tot_proc: 3m29.3s, tot_wait: 42.9s, rpc_num: 770, rpc_time: 4m33.8s, copr_cache: disabled, distsql_concurrency: 15}                                                                                                                        | data:Selection_74                                                                                                                                                       | 7.66 MB   | N/A     |
|             │   └─Selection_74                       | 6845023.00  | 6825548  | cop[tikv] |                | tikv_task:{proc max:1.94s, min:0s, avg: 270.8ms, p80:477ms, p95:814ms, iters:47069, tasks:770}, scan_detail: {total_process_keys: 45000000, total_process_keys_size: 6834773077, total_keys: 45000770, get_snapshot_time: 2.99s, rocksdb: {key_skipped_count: 45000000, block: {cache_hit_count: 6885, read_count: 113598, read_byte: 2.05 GB, read_time: 1.57s}}}                         | ge(tpch.orders.o_orderdate, 1994-01-01), lt(tpch.orders.o_orderdate, 1995-01-01)                                                                                        | N/A       | N/A     |
|             │     └─TableFullScan_73                 | 45000000.00 | 45000000 | cop[tikv] | table:orders   | tikv_task:{proc max:1.94s, min:0s, avg: 265.1ms, p80:467ms, p95:808ms, iters:47069, tasks:770}                                                                                                                                                                                                                                                                                             | keep order:false                                                                                                                                                        | N/A       | N/A     |
|             └─TableReader_39(Probe)                  | 6845023.00  | 5450573  | root      |                | time:2m30.6s, loops:5772, cop_task: {num: 4974, max: 1.55s, min: 8.45ms, avg: 287.2ms, p95: 608.2ms, max_proc_keys: 5088, p95_proc_keys: 3040, tot_proc: 16m42.2s, tot_wait: 4m59.2s, rpc_num: 4974, rpc_time: 23m48.6s, copr_cache: disabled, distsql_concurrency: 15}                                                                                                                    | data:TableRangeScan_38                                                                                                                                                  | N/A       | N/A     |
|               └─TableRangeScan_38                    | 6845023.00  | 5450573  | cop[tikv] | table:lineitem | tikv_task:{proc max:1.47s, min:0s, avg: 200.9ms, p80:323ms, p95:527ms, iters:23217, tasks:4974}, scan_detail: {total_process_keys: 5450573, total_process_keys_size: 1071363879, total_keys: 6816602, get_snapshot_time: 21s, rocksdb: {key_skipped_count: 5450573, block: {cache_hit_count: 11799375, read_count: 511690, read_byte: 7.90 GB, read_time: 8.06s}}}                         | range: decided by [eq(tpch.lineitem.l_orderkey, tpch.orders.o_orderkey)], keep order:false                                                                              | N/A       | N/A     |
+------------------------------------------------------+-------------+----------+-----------+----------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-----------+---------+
24 rows in set (34.68 sec)

34秒即完成了查询,且cost为:11808740465.25 小于优化器默认选择的执行成本。既然优化器不会对关联条件 c_nationkey = s_nationkey做转换,那么我们人工做转换成:c_nationkey = n_nationkey,那么在不指定表连接顺序后优化器的选择路径时怎么样的呢?

explain format=verbose
select 
        n_name,
        sum(l_extendedprice * (1 - l_discount)) as revenue
from
        customer,
        orders,
        lineitem,
        supplier,
        nation,
        region
where
        c_custkey = o_custkey
        and l_orderkey = o_orderkey
        and l_suppkey = s_suppkey
        and c_nationkey = n_nationkey
        and s_nationkey = n_nationkey
        and n_regionkey = r_regionkey
        and r_name = 'ASIA'
        and o_orderdate >= date '1994-01-01'
        and o_orderdate < date '1994-01-01' + interval '1' year
group by
        n_name
order by
        revenue desc;

+----------------------------------------------------+--------------+----------------+-----------+----------------+-------------------------------------------------------------------------------------------------------------------------------+
| id                                                 | estRows      | estCost        | task      | access object  | operator info                                                                                                                 |
+----------------------------------------------------+--------------+----------------+-----------+----------------+-------------------------------------------------------------------------------------------------------------------------------+
| Sort_25                                            | 5.00         | 19000486279.90 | root      |                | Column#48:desc                                                                                                                |
| └─Projection_27                                    | 5.00         | 19000485635.58 | root      |                | tpch.nation.n_name, Column#48                                                                                                 |
|   └─HashAgg_28                                     | 5.00         | 19000485634.58 | root      |                | group by:Column#51, funcs:sum(Column#49)->Column#48, funcs:firstrow(Column#50)->tpch.nation.n_name                            |
|     └─Projection_86                                | 6907156.71   | 17688680515.14 | root      |                | mul(tpch.lineitem.l_extendedprice, minus(1, tpch.lineitem.l_discount))->Column#49, tpch.nation.n_name, tpch.nation.n_name     |
|       └─Projection_29                              | 6907156.71   | 17618368422.68 | root      |                | tpch.lineitem.l_extendedprice, tpch.lineitem.l_discount, tpch.nation.n_name                                                   |
|         └─HashJoin_38                              | 6907156.71   | 17616300419.96 | root      |                | inner join, equal:[eq(tpch.orders.o_custkey, tpch.customer.c_custkey) eq(tpch.nation.n_nationkey, tpch.customer.c_nationkey)] |
|           ├─TableReader_84(Build)                  | 4500000.00   | 137274545.64   | root      |                | data:TableFullScan_83                                                                                                         |
|           │ └─TableFullScan_83                     | 4500000.00   | 1488878184.55  | cop[tikv] | table:customer | keep order:false                                                                                                              |
|           └─HashJoin_52(Probe)                     | 6845023.00   | 16571634388.70 | root      |                | inner join, equal:[eq(tpch.lineitem.l_orderkey, tpch.orders.o_orderkey)]                                                      |
|             ├─TableReader_82(Build)                | 6845023.00   | 1344074466.68  | root      |                | data:Selection_81                                                                                                             |
|             │ └─Selection_81                       | 6845023.00   | 18860015028.30 | cop[tikv] |                | ge(tpch.orders.o_orderdate, 1994-01-01), lt(tpch.orders.o_orderdate, 1995-01-01)                                              |
|             │   └─TableFullScan_80                 | 45000000.00  | 14369015028.30 | cop[tikv] | table:orders   | keep order:false                                                                                                              |
|             └─HashJoin_55(Probe)                   | 26520727.87  | 13949359180.45 | root      |                | inner join, equal:[eq(tpch.supplier.s_suppkey, tpch.lineitem.l_suppkey)]                                                      |
|               ├─HashJoin_57(Build)                 | 43848.00     | 15030223.79    | root      |                | inner join, equal:[eq(tpch.nation.n_nationkey, tpch.supplier.s_nationkey)]                                                    |
|               │ ├─HashJoin_70(Build)               | 5.00         | 3005.46        | root      |                | inner join, equal:[eq(tpch.region.r_regionkey, tpch.nation.n_regionkey)]                                                      |
|               │ │ ├─TableReader_75(Build)          | 1.00         | 119.34         | root      |                | data:Selection_74                                                                                                             |
|               │ │ │ └─Selection_74                 | 1.00         | 1675.92        | cop[tikv] |                | eq(tpch.region.r_name, "ASIA")                                                                                                |
|               │ │ │   └─TableFullScan_73           | 5.00         | 1426.42        | cop[tikv] | table:region   | keep order:false                                                                                                              |
|               │ │ └─TableReader_72(Probe)          | 25.00        | 784.55         | root      |                | data:TableFullScan_71                                                                                                         |
|               │ │   └─TableFullScan_71             | 25.00        | 7306.27        | cop[tikv] | table:nation   | keep order:false                                                                                                              |
|               │ └─TableReader_77(Probe)            | 300000.00    | 9037189.34     | root      |                | data:TableFullScan_76                                                                                                         |
|               │   └─TableFullScan_76               | 300000.00    | 97541840.03    | cop[tikv] | table:supplier | keep order:false                                                                                                              |
|               └─TableReader_79(Probe)              | 179998372.00 | 10336824370.54 | root      |                | data:TableFullScan_78                                                                                                         |
|                 └─TableFullScan_78                 | 179998372.00 | 63814790758.73 | cop[tikv] | table:lineitem | keep order:false                                                                                                              |
+----------------------------------------------------+--------------+----------------+-----------+----------------+-------------------------------------------------------------------------------------------------------------------------------+
24 rows in set, 2 warnings (0.01 sec)

mysql> show warnings;
+-------+------+------------------------------------------------------------------------------------------------+
| Level | Code | Message                                                                                        |
+-------+------+------------------------------------------------------------------------------------------------+
| Note  | 1105 | [customer] remain after pruning paths for customer given Prop{SortItems: [], TaskTp: rootTask} |
| Note  | 1105 | [lineitem] remain after pruning paths for lineitem given Prop{SortItems: [], TaskTp: rootTask} |
+-------+------+------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

可以看到即使手工做了c_nationkey关联条件的转换,但是优化器还是走了和原本Q5语句未指定hint时一样的执行计划,而且cost成本也是:19000486279.90 。该cost成本大于上面指定hint时的cost成本:11808740465.25。

因此,经过手工做关联条件的等价转换且指定表的连接顺序后,SQL执行耗时由6分39秒缩短为34秒,效率提升10多倍!。
所以有两个疑问:
1、为何优化器不对可以等价变换的关联条件做搜索路径呢?
2、为何优化器不选择成本更低的执行计划呢?

1 个赞

高手问题,很难回答。虽然没有完全看完你的实验过程,但是我觉得你想了解的是优化器做执行计划时是怎么做出选择的,我看到SQL是6表关联,执行计划有很多种组合,实际优化器应该不会把所有组合都走一遍再做出最优的执行计划,优化器应该只会按最可能的最优执行计划去走,优化器没有走最优执行计划的可能原因很多。但是不知道TiDB是否有像oracle 10053 event一样的功能,可以dump出来优化器计算执行计划的过程。可能这方面还得查找一下资料。

你的测试环境tidb/tikv的配置是怎样的,我看了下内部发版测试环境,sf=50, Q5用的是第一个执行计划,执行时间是35s,1 tidb + 3tikv 每个节点16c 48G

默认执行计划有重复执行过Q5吗,排除一次缓存的影响

+-------------------------------------------+--------------+-----------+-----------+----------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------+-----------+---------+
| ID                                        | ESTROWS      | ACTROWS   | TASK      | ACCESS OBJECT  | EXECUTION INFO                                                                                                                                                                                                                                                                                                                                                                              | OPERATOR INFO                                                                                                                   | MEMORY    | DISK    |
+-------------------------------------------+--------------+-----------+-----------+----------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------+-----------+---------+
| Sort_24                                   | 5.00         | 5         | root      |                | time:35.2s, loops:2                                                                                                                                                                                                                                                                                                                                                                         | Column#49:desc                                                                                                                  | 8.13 KB   | 0 Bytes |
| └─Projection_26                           | 5.00         | 5         | root      |                | time:35.2s, loops:5, Concurrency:OFF                                                                                                                                                                                                                                                                                                                                                        | test.nation.n_name, Column#49                                                                                                   | 63.3 KB   | N/A     |
|   └─HashAgg_27                            | 5.00         | 5         | root      |                | time:35.2s, loops:5, partial_worker:{wall_time:35.234740037s, concurrency:5, task_num:359, tot_wait:2m56.090073425s, tot_exec:83.033806ms, tot_time:2m56.173261564s, max:35.234709875s, p95:35.234709875s}, final_worker:{wall_time:35.234746689s, concurrency:5, task_num:20, tot_wait:2m56.173541835s, tot_exec:58.463µs, tot_time:2m56.173605659s, max:35.234723343s, p95:35.234723343s} | group by:Column#52, funcs:sum(Column#50)->Column#49, funcs:firstrow(Column#51)->test.nation.n_name                              | 1.12 MB   | N/A     |
|     └─Projection_85                       | 11455118.33  | 364651    | root      |                | time:35.2s, loops:360, Concurrency:5                                                                                                                                                                                                                                                                                                                                                        | mul(test.lineitem.l_extendedprice, minus(1, test.lineitem.l_discount))->Column#50, test.nation.n_name, test.nation.n_name       | 1.02 MB   | N/A     |
|       └─Projection_28                     | 11455118.33  | 364651    | root      |                | time:35.2s, loops:360, Concurrency:5                                                                                                                                                                                                                                                                                                                                                        | test.lineitem.l_extendedprice, test.lineitem.l_discount, test.nation.n_name                                                     | 1.05 MB   | N/A     |
|         └─HashJoin_37                     | 11455118.33  | 364651    | root      |                | time:35.2s, loops:360, build_hash_table:{total:3.15s, fetch:540.4ms, build:2.61s}, probe:{concurrency:5, total:2m56.2s, max:35.2s, probe:4.05s, fetch:2m52.1s}                                                                                                                                                                                                                              | inner join, equal:[eq(test.orders.o_custkey, test.customer.c_custkey) eq(test.supplier.s_nationkey, test.customer.c_nationkey)] | 517.5 MB  | 0 Bytes |
|           ├─TableReader_83(Build)         | 7500000.00   | 7500000   | root      |                | time:533.6ms, loops:7332, cop_task: {num: 16, max: 1.45s, min: 666µs, avg: 693.8ms, p95: 1.45s, max_proc_keys: 515357, p95_proc_keys: 515357, tot_proc: 9.94s, tot_wait: 52ms, rpc_num: 16, rpc_time: 11.1s, copr_cache_hit_ratio: 0.00, distsql_concurrency: 15}                                                                                                                           | data:TableFullScan_82                                                                                                           | 94.4 MB   | N/A     |
|           │ └─TableFullScan_82            | 7500000.00   | 7500000   | cop[tikv] | table:customer | tikv_task:{proc max:1.1s, min:0s, avg: 595.2ms, p80:661ms, p95:1.1s, iters:7393, tasks:16}, scan_detail: {total_process_keys: 7500000, total_process_keys_size: 1526085547, total_keys: 7500016, get_snapshot_time: 57.9ms, rocksdb: {key_skipped_count: 7500000, block: {cache_hit_count: 24921}}}                                                                                         | keep order:false                                                                                                                | N/A       | N/A     |
|           └─HashJoin_51(Probe)            | 11455118.33  | 9115151   | root      |                | time:35.2s, loops:8905, build_hash_table:{total:6.22s, fetch:2.07s, build:4.15s}, probe:{concurrency:5, total:2m56.2s, max:35.2s, probe:21.4s, fetch:2m34.8s}                                                                                                                                                                                                                               | inner join, equal:[eq(test.lineitem.l_orderkey, test.orders.o_orderkey)]                                                        | 1.06 GB   | 0 Bytes |
|             ├─TableReader_81(Build)       | 11455118.33  | 11380211  | root      |                | time:2.28s, loops:11078, cop_task: {num: 109, max: 1.47s, min: 518.9µs, avg: 561.2ms, p95: 1.01s, max_proc_keys: 700329, p95_proc_keys: 700213, tot_proc: 59.7s, tot_wait: 9ms, rpc_num: 109, rpc_time: 1m1.2s, copr_cache_hit_ratio: 0.00, distsql_concurrency: 15}                                                                                                                        | data:Selection_80                                                                                                               | 29.5 MB   | N/A     |
|             │ └─Selection_80              | 11455118.33  | 11380211  | cop[tikv] |                | tikv_task:{proc max:1.27s, min:0s, avg: 539.2ms, p80:567ms, p95:953ms, iters:73728, tasks:109}, scan_detail: {total_process_keys: 75000000, total_process_keys_size: 11391895327, total_keys: 75000109, get_snapshot_time: 4.23ms, rocksdb: {key_skipped_count: 75000000, block: {cache_hit_count: 156639, read_count: 32803, read_byte: 550.9 MB, read_time: 152.1ms}}}                    | ge(test.orders.o_orderdate, 1994-01-01 00:00:00.000000), lt(test.orders.o_orderdate, 1995-01-01 00:00:00.000000)                | N/A       | N/A     |
|             │   └─TableFullScan_79        | 75000000.00  | 75000000  | cop[tikv] | table:orders   | tikv_task:{proc max:1.22s, min:0s, avg: 499.5ms, p80:527ms, p95:895ms, iters:73728, tasks:109}                                                                                                                                                                                                                                                                                              | keep order:false                                                                                                                | N/A       | N/A     |
|             └─HashJoin_54(Probe)          | 52849021.36  | 60064410  | root      |                | time:29.7s, loops:58660, build_hash_table:{total:28.2ms, fetch:6.5ms, build:21.7ms}, probe:{concurrency:5, total:2m56.2s, max:35.2s, probe:2m14.9s, fetch:41.2s}                                                                                                                                                                                                                            | inner join, equal:[eq(test.supplier.s_suppkey, test.lineitem.l_suppkey)]                                                        | 10.4 MB   | 0 Bytes |
|               ├─HashJoin_56(Build)        | 87837.60     | 100102    | root      |                | time:12.3ms, loops:101, build_hash_table:{total:224.4µs, fetch:212.9µs, build:11.6µs}, probe:{concurrency:5, total:137.2ms, max:27.5ms, probe:114.2ms, fetch:23ms}                                                                                                                                                                                                                          | inner join, equal:[eq(test.nation.n_nationkey, test.supplier.s_nationkey)]                                                      | 2.21 KB   | 0 Bytes |
|               │ ├─HashJoin_69(Build)      | 5.00         | 5         | root      |                | time:212.2µs, loops:2, build_hash_table:{total:64.8µs, fetch:60.4µs, build:4.42µs}, probe:{concurrency:5, total:420.2µs, max:98.2µs, probe:36.4µs, fetch:383.8µs}                                                                                                                                                                                                                           | inner join, equal:[eq(test.region.r_regionkey, test.nation.n_regionkey)]                                                        | 35.2 KB   | 0 Bytes |
|               │ │ ├─TableReader_74(Build) | 1.00         | 1         | root      |                | time:51.8µs, loops:2, cop_task: {num: 1, max: 686.7µs, proc_keys: 5, rpc_num: 1, rpc_time: 662.5µs, copr_cache_hit_ratio: 0.00, distsql_concurrency: 15}                                                                                                                                                                                                                                    | data:Selection_73                                                                                                               | 275 Bytes | N/A     |
|               │ │ │ └─Selection_73        | 1.00         | 1         | cop[tikv] |                | tikv_task:{time:0s, loops:1}, scan_detail: {total_process_keys: 5, total_process_keys_size: 559, total_keys: 6, get_snapshot_time: 327µs, rocksdb: {key_skipped_count: 5, block: {cache_hit_count: 2}}}                                                                                                                                                                                     | eq(test.region.r_name, "MIDDLE EAST")                                                                                           | N/A       | N/A     |
|               │ │ │   └─TableFullScan_72  | 5.00         | 5         | cop[tikv] | table:region   | tikv_task:{time:0s, loops:1}                                                                                                                                                                                                                                                                                                                                                                | keep order:false                                                                                                                | N/A       | N/A     |
|               │ │ └─TableReader_71(Probe) | 25.00        | 25        | root      |                | time:43.1µs, loops:2, cop_task: {num: 1, max: 1.04ms, proc_keys: 25, rpc_num: 1, rpc_time: 1.02ms, copr_cache_hit_ratio: 0.00, distsql_concurrency: 15}                                                                                                                                                                                                                                     | data:TableFullScan_70                                                                                                           | 1.00 KB   | N/A     |
|               │ │   └─TableFullScan_70    | 25.00        | 25        | cop[tikv] | table:nation   | tikv_task:{time:0s, loops:1}, scan_detail: {total_process_keys: 25, total_process_keys_size: 3109, total_keys: 26, get_snapshot_time: 395µs, rocksdb: {key_skipped_count: 25, block: {cache_hit_count: 2}}}                                                                                                                                                                                 | keep order:false                                                                                                                | N/A       | N/A     |
|               │ └─TableReader_76(Probe)   | 500000.00    | 500000    | root      |                | time:4.43ms, loops:490, cop_task: {num: 1, max: 654.8µs, proc_keys: 0, rpc_num: 1, rpc_time: 636.6µs, copr_cache_hit_ratio: 1.00, distsql_concurrency: 15}                                                                                                                                                                                                                                  | data:TableFullScan_75                                                                                                           | 7.64 MB   | N/A     |
|               │   └─TableFullScan_75      | 500000.00    | 500000    | cop[tikv] | table:supplier | tikv_task:{time:278ms, loops:493}, scan_detail: {get_snapshot_time: 285µs, rocksdb: {block: {}}}                                                                                                                                                                                                                                                                                            | keep order:false                                                                                                                | N/A       | N/A     |
|               └─TableReader_78(Probe)     | 300005811.00 | 300005811 | root      |                | time:8.46s, loops:293178, cop_task: {num: 570, max: 1.5s, min: 1.34ms, avg: 699.7ms, p95: 911.9ms, max_proc_keys: 528339, p95_proc_keys: 528089, tot_proc: 4m44.6s, tot_wait: 324ms, rpc_num: 570, rpc_time: 6m38.8s, copr_cache_hit_ratio: 0.00, distsql_concurrency: 15}                                                                                                                  | data:TableFullScan_77                                                                                                           | 725.5 MB  | N/A     |
|                 └─TableFullScan_77        | 300005811.00 | 300005811 | cop[tikv] | table:lineitem | tikv_task:{proc max:859ms, min:0s, avg: 386.1ms, p80:415ms, p95:494ms, iters:295454, tasks:570}, scan_detail: {total_process_keys: 300005811, total_process_keys_size: 59595430182, total_keys: 300006381, get_snapshot_time: 512.2ms, rocksdb: {key_skipped_count: 300005811, block: {cache_hit_count: 956963, read_count: 17454, read_byte: 282.8 MB, read_time: 121.5ms}}}               | keep order:false                                                                                                                | N/A       | N/A     |
+-------------------------------------------+--------------+-----------+-----------+----------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------+-----------+---------+

我的只有一个虚拟机,tikv、pd、tidb混合部署,全部一个节点,且操作系统内存共有62GB、12CPU。
看你这个执行计划可以看到,你这个应该是调整了 tidb_mem_quota_query参数,因为默认的1GB的情况下会落盘(参考我那个),落盘的情况下tidb会非常慢,可以参考我之前的帖子:对于hashAgg算子非并行模式下还是发生OOMSort算子落盘性能太慢,希望产品层面进行优化 。另一方面你三个节点配置相对高一些,所以tikv并发读取数据效率也会更好一些,我感觉如果你走了手工调优后的执行计划应该会更快。
我这边所有的操作都是多次重复执行后做的结论。
还有我禁用掉了tikv-client.copr-cache.capacity-mb=0,目的是避免region查询结果缓存,这种语句一般在生产中短时间重复执行概率较低不太容易命中缓存。
我机器和集群配置情况:

[tidb@host0 ~]$ tiup cluster display tidb-test
tiup is checking updates for component cluster ...
A new version of cluster is available:
   The latest version:         v1.12.3
   Local installed version:    v1.11.0
   Update current component:   tiup update cluster
   Update all components:      tiup update --all

Starting component `cluster`: /home/tidb/.tiup/components/cluster/v1.11.0/tiup-cluster display tidb-test
Cluster type:       tidb
Cluster name:       tidb-test
Cluster version:    v6.5.2
Deploy user:        tidb
SSH type:           builtin
Dashboard URL:      http://192.168.31.201:2379/dashboard
Grafana URL:        http://192.168.31.201:3000
ID                    Role          Host            Ports                            OS/Arch       Status   Data Dir                           Deploy Dir
--                    ----          ----            -----                            -------       ------   --------                           ----------
192.168.31.201:9093   alertmanager  192.168.31.201  9093/9094                        linux/x86_64  Up       /data/tidb-data/alertmanager-9093  /data/tidb-deploy/alertmanager-9093
192.168.31.201:3000   grafana       192.168.31.201  3000                             linux/x86_64  Up       -                                  /data/tidb-deploy/grafana-3000
192.168.31.201:2379   pd            192.168.31.201  2379/2380                        linux/x86_64  Up|L|UI  /data/tidb-data/pd-2379            /data/tidb-deploy/pd-2379
192.168.31.201:9090   prometheus    192.168.31.201  9090/12020                       linux/x86_64  Up       /data/tidb-data/prometheus-9090    /data/tidb-deploy/prometheus-9090
192.168.31.201:4000   tidb          192.168.31.201  4000/10080                       linux/x86_64  Up       -                                  /data/tidb-deploy/tidb-4000
192.168.31.201:9000   tiflash       192.168.31.201  9000/8123/3930/20170/20292/8234  linux/x86_64  Up       /data/tidb-data/tiflash-9000       /data/tidb-deploy/tiflash-9000
192.168.31.201:20160  tikv          192.168.31.201  20160/20180                      linux/x86_64  Up       /data/tidb-data/tikv-20160         /data/tidb-deploy/tikv-20160
Total nodes: 7
[tidb@host0 ~]$ tiup cluster exec tidb-test --command "free -g && lscpu" 2>/dev/null
+ [ Serial ] - SSHKeySet: privateKey=/home/tidb/.tiup/storage/cluster/clusters/tidb-test/ssh/id_rsa, publicKey=/home/tidb/.tiup/storage/cluster/clusters/tidb-test/ssh/id_rsa.pub
+ [Parallel] - UserSSH: user=tidb, host=192.168.31.201
+ [Parallel] - UserSSH: user=tidb, host=192.168.31.201
+ [Parallel] - UserSSH: user=tidb, host=192.168.31.201
+ [Parallel] - UserSSH: user=tidb, host=192.168.31.201
+ [Parallel] - UserSSH: user=tidb, host=192.168.31.201
+ [Parallel] - UserSSH: user=tidb, host=192.168.31.201
+ [Parallel] - UserSSH: user=tidb, host=192.168.31.201
+ [ Serial ] - Shell: host=192.168.31.201, sudo=false, command=`free -g && lscpu`
Run command on 192.168.31.201(sudo:false): free -g && lscpu
Outputs of free -g && lscpu on 192.168.31.201:22:
stdout:
              total        used        free      shared  buff/cache   available
Mem:             62          28          16           0          18          33
Swap:             1           1           0
Architecture:        x86_64
CPU op-mode(s):      32-bit, 64-bit
Byte Order:          Little Endian
CPU(s):              12
On-line CPU(s) list: 0-11
Thread(s) per core:  1
Core(s) per socket:  2
Socket(s):           6
NUMA node(s):        1
Vendor ID:           GenuineIntel
CPU family:          6
Model:               151
Model name:          12th Gen Intel(R) Core(TM) i7-12700
Stepping:            2
CPU MHz:             2112.001
BogoMIPS:            4224.00
Hypervisor vendor:   VMware
Virtualization type: full
L1d cache:           48K
L1i cache:           32K
L2 cache:            1280K
L3 cache:            25600K
NUMA node0 CPU(s):   0-11
Flags:               fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon rep_good nopl xtopology tsc_reliable nonstop_tsc cpuid pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch invpcid_single ssbd ibrs ibpb stibp ibrs_enhanced fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid rdseed adx smap clflushopt clwb sha_ni xsaveopt xsavec xgetbv1 xsaves avx_vnni arat umip pku ospke gfni vaes vpclmulqdq rdpid movdiri movdir64b fsrm md_clear serialize flush_l1d arch_capabilities

另外,我执行时候禁用了tiflash,主要考察tidb的运算能力,毕竟很多情况下不都每个集群都加上tiflash,且这种语句不算很复杂。

我重新设置了tidb_mem_quota_query参数,设置成10GB不让其落盘。执行花费了1分20秒,相比上面经过改良的SQL来说还是慢了1倍多。

mysql> select          n_name,         sum(l_extendedprice * (1 - l_discount)) as revenue from         customer,         orders,         lineitem,         supplier,         nation,         region where         c_custkey = o_custkey         and l_orderkey = o_orderkey         and l_suppkey = s_suppkey         and c_nationkey = s_nationkey         and s_nationkey = n_nationkey         and n_regionkey = r_regionkey         and r_name = 'ASIA'         and o_orderdate >= date '1994-01-01'         and o_orderdate < date '1994-01-01' + interval '1' year group by         n_name order by         revenue desc;
+-----------+-----------------+
| n_name    | revenue         |
+-----------+-----------------+
| CHINA     | 1606166264.1445 |
| VIETNAM   | 1591069581.4506 |
| JAPAN     | 1590793368.4572 |
| INDONESIA | 1589786701.1305 |
| INDIA     | 1583446012.8972 |
+-----------+-----------------+
5 rows in set (1 min 24.66 sec)

mysql> select          n_name,         sum(l_extendedprice * (1 - l_discount)) as revenue from         customer,         orders,         lineitem,         supplier,         nation,         region where         c_custkey = o_custkey         and l_orderkey = o_orderkey         and l_suppkey = s_suppkey         and c_nationkey = s_nationkey         and s_nationkey = n_nationkey         and n_regionkey = r_regionkey         and r_name = 'ASIA'         and o_orderdate >= date '1994-01-01'         and o_orderdate < date '1994-01-01' + interval '1' year group by         n_name order by         revenue desc;
+-----------+-----------------+
| n_name    | revenue         |
+-----------+-----------------+
| CHINA     | 1606166264.1445 |
| VIETNAM   | 1591069581.4506 |
| JAPAN     | 1590793368.4572 |
| INDONESIA | 1589786701.1305 |
| INDIA     | 1583446012.8972 |
+-----------+-----------------+
5 rows in set (1 min 17.92 sec)

一般TPCH,会处理一些参数,版本啥的。