简体   繁体   中英

How do these queries execute?

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;

The following queries returns same number of rows.

when I do

    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

it returns 0 rows. so I think there must be some difference in the way they get executed.can any one explain. And I would be thankful If some one refers me execution order of t-sql statement. I am a beginner in Teradata sql.

Is there any article on the internet --explaining order of execution of sql statements in different types of databases. Thank you very much.

Those queries will not return the same result.

Q1 returns all rows from client with NULL in n_name . When you EXPLAIN it you will see that the optimizer eliminated the join:

  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.

In fact this is exactly the same as

select c.c_name, NULL S n_name
from    retail.client c

But Q2 returns only where c_nationkey doesn't exists in n_nationkey , ie this is similar to a NOT EXISTS . On the actaul data in this sample database there will be no row returned.

Regarding details on the logical execution of a SQL SELECT have a look at Itzik Ben-Gan's new articles on SQL Server:

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

Logical Query Processing: The FROM Clause and Joins

Teradata is almost the same, just doesn't support CROSS/OUTER APPLY, but adds QUALIFY for filtering the result of an OLAP-Function:

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

The MINUS query does not return zero rows, it returns the same as Q1, just as a DISTINCT result.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM