1

我面临雪花EXPLAIN查询的一个问题。正如雪花文档中提到的,我在雪花实例中执行了以下查询,但它给出了不同的输出。

创建表:

create table z1 (id integer);
create table z2 (id integer);
create table z3 (id integer);

为查询生成表格格式的 EXPLAIN 计划:

explain using tabular select z1.id, z2.id 
    from z1, z2
    where z2.id = z1.id;

预期输出:

+------+----+--------+-------------+------------------------------+-------+--------------------------+-----------------+--------------------+---------------+
| step | id | parent | operation   | objects                      | alias | expressions              | partitionsTotal | partitionsAssigned | bytesAssigned |
|------+----+--------+-------------+------------------------------+-------+--------------------------+-----------------+--------------------+---------------|
| NULL | NULL |   NULL | GlobalStats | NULL                         | NULL  | NULL                     |               2 |                  2 |          1024 |
|    1 |  0 |   NULL | Result      | NULL                         | NULL  | Z1.ID, Z2.ID             |            NULL |               NULL |          NULL |
|    1 |  1 |      0 | InnerJoin   | NULL                         | NULL  | joinKey: (Z2.ID = Z1.ID) |            NULL |               NULL |          NULL |
|    1 |  2 |      1 | TableScan   | TESTDB.TEMPORARY_DOC_TEST.Z2 | NULL  | ID                       |               1 |                  1 |           512 |
|    1 |  3 |      1 | JoinFilter  | NULL                         | NULL  | joinKey: (Z2.ID = Z1.ID) |            NULL |               NULL |          NULL |
|    1 |  4 |      3 | TableScan   | TESTDB.TEMPORARY_DOC_TEST.Z1 | NULL  | ID                       |               1 |                  1 |           512 |
+------+----+--------+-------------+------------------------------+-------+--------------------------+-----------------+--------------------+---------------+

实际输出:

+-----+----+-------+-----------+--------+------+------------+----------------+-------------------+--------------+
|step |id  |parent |operation  |objects |alias |expressions |partitionsTotal |partitionsAssigned |bytesAssigned |
+-----+----+-------+-----------+--------+------+------------+----------------+-------------------+--------------+
|NULL |NULL|NULL   |GlobalStats|NULL    |NULL  |NULL        |0               |0                  |0             |
|1    |0   |NULL   |Result     |NULL    |NULL  |Z1.ID, Z2.ID|NULL            |NULL               |NULL          |
|1    |1   |0      |Generator  |NULL    |NULL  |0           |NULL            |NULL               |NULL          |
+-----+--- +-------+-----------+--------+------+------------+----------------+-------------------+--------------+

在实际输出中,我看不到 , 等操作的结果TableScanJoinFilterInnerJoin不确定是否必须在雪花实例中进行任何设置?

我们计划使用 的输出EXPLAINSELECT查询中提取表名。

4

1 回答 1

1

因为所有表都是空的,Snowflake SQL Optimizer 看到这些表将返回 0 行;它不是从这些表中读取数据,而是使用生成器作为数据源。它可能会跳过任何不必要的操作,例如连接...如果表中只有几行,则可能会发生这种情况。

例如,这个将同时显示 z1 和 z2:

create or replace table z1 (id integer);
create or replace table z2 (id integer);

insert into z1 values (1 ),(2);
insert into z2 values (1 );

explain using tabular select z1.id, z2.id 
    from z1, z2
    where z2.id = z1.id;

让我们在查询中包含 z3:

create or replace table z1 (id integer);
create or replace table z2 (id integer);
create or replace table z3 (id integer);

insert into z1 values (1 ),(2);
insert into z2 values (1 );
insert into z3 values (1000 );

explain using tabular select z1.id, z2.id 
    from z1, z2
    where z2.id = z1.id
    and z1.id < (select z3.id from z3);

如您所见,z3 不在说明计划中!

让我们在 z3 中再添加一行并重新运行解释计划:

insert into z3 values (1);

explain using tabular select z1.id, z2.id 
    from z1, z2
    where z2.id = z1.id
    and z1.id < (select z3.id from z3);

现在你应该在那里看到它。

所以从 EXPLAIN 中提取表名并不是一个好主意。检查此视图:

https://docs.snowflake.com/en/sql-reference/account-usage/access_history.html

于 2021-08-13T07:59:42.133 回答