这些查询如何执行?

时间:2016-02-22 11:53:22

标签: sql teradata

1

select c.c_name, a.n_name
from    retail.client c left join retail.area a
on  c.c_nationkey = a.n_nationkey
and a.n_name is null;

2

select c.c_name, a.n_name
from    retail.client c left join retail.area a
on  c.c_nationkey = a.n_nationkey
where   a.n_name is null;

以下查询返回相同的行数。

当我做的时候

    select c.c_name, a.n_name
    from    retail.client c left join retail.area a
    on  c.c_nationkey = a.n_nationkey
    and a.n_name is null
MiNUS
    select c.c_name, a.n_name
    from    retail.client c left join retail.area a
    on  c.c_nationkey = a.n_nationkey
    where   a.n_name is null

它返回0行。  所以我认为他们执行的方式肯定会有所不同。任何一个解释。 我会很感激如果有人提到我的t-sql语句的执行顺序。我是Teradata sql的初学者。

互联网上是否有任何文章 - 在不同类型的数据库中解释sql语句的执行顺序。 非常感谢你。

1 个答案:

答案 0 :(得分:1)

这些查询将返回相同的结果。

Q1会在<TextBox ...> <TextBox.Style> <Style TargetType="{x:Type TextBox}"> <Setter Property="Foreground" Value="{...focusedcolor...}" /> <Style.Triggers> <Trigger Property="IsFocused" Value="False"> <Setter Property="Foreground" Value="{...unfocused...}" /> </Trigger> </Style.Triggers> </Style> </TextBox.Style> </TextBox> 中使用clientNULL返回所有行。当你n_name时,你会看到优化器消除了连接:

EXPLAIN

实际上这与

完全相同
  1) First, we lock retail.c for read on a reserved RowHash to prevent
     global deadlock.
  2) Next, we lock retail.c for read.
  3) We do an all-AMPs RETRIEVE step from retail.c by way of an
     all-rows scan with no residual conditions into Spool 1
     (group_amps), which is built locally on the AMPs.  The size of
     Spool 1 is estimated with low confidence to be 1,536 rows (
     136,704 bytes).  The estimated time for this step is 0.08 seconds.
  4) Finally, we send out an END TRANSACTION step to all AMPs involved
     in processing the request.
  -> The contents of Spool 1 are sent back to the user as the result of
     statement 1.  The total estimated time is 0.08 seconds.

但是,只有在select c.c_name, NULL S n_name from retail.client c c_nationkey不存在的情况下,Q2才会返回,即这类似于n_nationkey。在此示例数据库中的actaul数据中,将不会返回任何行。

关于SQL SELECT逻辑执行的详细信息,请查看Itzik Ben-Gan关于SQL Server的新文章:

Logical Query Processing: What It Is And What It Means to You

Logical Query Processing: The FROM Clause and Joins

Teradata几乎相同,只是不支持CROSS / OUTER APPLY,但添加了QUALIFY来过滤OLAP函数的结果:

FROM - WHERE - GROUP BY - HAVING - Windowed Aggregates / OLAP - QUALIFY - SELECT - ORDER BY

NOT EXISTS查询返回零行,它返回与Q1相同的结果,就像MINUS结果一样。