更新时间:2025-03-13 GMT+08:00

执行计划

以如下SQL语句为例:

1
2
3
4
5
gaussdb=# DROP TABLE IF EXISTS t1;
gaussdb=# DROP TABLE IF EXISTS t2;
gaussdb=# CREATE TABLE t1 (c1 int, c2 int);
gaussdb=# CREATE TABLE t2 (c1 int, c2 int);
gaussdb=# SET explain_perf_mode = pretty;
执行EXPLAIN的输出为:
gaussdb=# EXPLAIN SELECT * FROM t1,t2 WHERE t1.c1 = t2.c2;
                            QUERY PLAN                             
-------------------------------------------------------------------
 Hash Join  (cost=23.73..341.30 rows=16217 width=180)
   Hash Cond: (t1.c1 = t2.c2)
   ->  Seq Scan on t1  (cost=0.00..122.17 rows=5317 width=76)
   ->  Hash  (cost=16.10..16.10 rows=610 width=104)
         ->  Seq Scan on t2  (cost=0.00..16.10 rows=610 width=104)
(5 rows)

--删除表。
gaussdb=# DROP TABLE t1,t2;

执行计划层级解读(纵向):

  • 第一层:Seq Scan on t2

    表扫描算子,用Seq Scan的方式扫描表t2。这一层的作用是把表t2的数据从buffer或者磁盘上读上来输送给上层节点参与计算。

  • 第二层:Hash

    Hash算子,作用是把下层计算输送上来的算子计算hash值,为后续hash join操作做数据准备。

  • 第三层:Seq Scan on t1

    表扫描算子,用Seq Scan的方式扫描表t1。这一层的作用是把表t1的数据从buffer或者磁盘上读上来输送给上层节点参与hash join计算。

  • 第四层:Hash Join

    join算子,主要作用是将t1表和t2表的数据通过hash join的方式连接,并输出结果数据。