In DB2 for LUW SQL Reference Volume 1
Quote:
The clauses of the subselect are processed in the following sequence:
1. FROM clause
2. WHERE clause
3. GROUP BY clause
4. HAVING clause
5. SELECT clause
6. ORDER BY clause
7. FETCH FIRST clause
|
The sequence is logical sequence.
If same result could be produced more efficiently with another way,
DB2 optimizer is smart enough to use the way(access path) in actual access.
Example 1: DB2 may produce the result of the following query without accessing the employee table.
Because, empno column was defined NOT NULL.
Then "(number of rows with empno is null) = 0" is apparent without accessing the table.
Code:
------------------------------ Commands Entered ------------------------------
SELECT COUNT(*) AS count_rows
FROM employee
WHERE empno IS NULL
;
------------------------------------------------------------------------------
COUNT_ROWS
-----------
0
1 record(s) selected.
Example 2: Another popular example is DB2 try to apply conditions in WHERE clause as early as possible.
It may be the time accessing tables in FROM clause,
and it may be before joining with another tables,
if the same result was guaranteed as processed in the logical sequence.