Subqueries

Adaptive Server uses different methods to reduce the cost of processing subqueries. Parallel optimization depends on the type of subquery:

select count(*) from RA2 where not exists
(select * from RB2 where RA2.a1 = b1)

QUERY PLAN FOR STATEMENT 1 (at line 1).
Executed in parallel by coordinating process and 2 worker processes.

8 operator(s) under root


The type of query is SELECT.

ROOT:EMIT Operator

   |SCALAR AGGREGATE Operator
   |  Evaluate Ungrouped COUNT AGGREGATE.
   |
   |   |SQFILTER Operator has 2 children.
   |   |
   |   |   |EXCHANGE Operator (Merged)
   |   |   |Executed in parallel by 2 Producer 
               and 1 Consumer processes.

   |   |   |
   |   |   |   |EXCHANGE:EMIT Operator
   |   |   |   |
   |   |   |   |   |RESTRICT Operator
   |   |   |   |   |
   |   |   |   |   |   |SCAN Operator
   |   |   |   |   |   |  FROM TABLE
   |   |   |   |   |   |  RA2
   |   |   |   |   |   |  Index : RA2_NC2L
   |   |   |   |   |   |  Forward Scan.
   |   |   |   |   |   |  Executed in parallel with
                            a 2-way partition scan.
   |   |
   |   |  Run subquery 1 (at nesting level 1).
   |   |
   |   |  QUERY PLAN FOR SUBQUERY 1 (at nesting 
             level 1 and at line 2).
   |   |
   |   |   Correlated Subquery.
   |   |   Subquery under an EXISTS predicate.
   |   |
   |   |   |SCALAR AGGREGATE Operator
   |   |   |  Evaluate Ungrouped ANY AGGREGATE.
   |   |   |  Scanning only up to the first 
                  qualifying row.
   |   |   |
   |   |   |   |SCAN Operator
   |   |   |   |  FROM TABLE
   |   |   |   |  RB2
   |   |   |   |  Table Scan.
   |   |   |   |  Forward Scan.
   |   |
   |   |  END OF QUERY PLAN FOR SUBQUERY 1.

The following example shows an in subquery flattened into a semijoin. Adaptive Server converts this into an inner join to provide greater flexibility in shuffling the tables in the join order. As seen below, the table RB2, which was originally in the subquery, is now being accessed in parallel.

select * from RA2 where a1 in (select b1 from RB2)

QUERY PLAN FOR STATEMENT 1 (at line 1).
Executed in parallel by coordinating process and 5 worker processes.

10 operator(s) under root


The type of query is SELECT.

ROOT:EMIT Operator

   |EXCHANGE Operator (Merged)
   |Executed in parallel by 3 Producer and 1 Consumer processes.

   |
   |   |EXCHANGE:EMIT Operator
   |   |
   |   |   |MERGE JOIN Operator (Join Type: Inner Join)
   |   |   | Using Worktable3 for internal storage.
   |   |   |  Key Count: 1
   |   |   |  Key Ordering: ASC
   |   |   |
   |   |   |   |SORT Operator
   |   |   |   | Using Worktable1 for internal 
                     storage.
   |   |   |   |
   |   |   |   |   |SCAN Operator
   |   |   |   |   |  FROM TABLE
   |   |   |   |   |  RB2
   |   |   |   |   |  Table Scan.
   |   |   |   |   |  Executed in parallel with a 
                         3-way partition scan.
   |   |   |
   |   |   |   |SORT Operator
   |   |   |   | Using Worktable2 for internal 
                    storage.
   |   |   |   |
   |   |   |   |   |EXCHANGE Operator (Merged)
   |   |   |   |   |Executed in parallel by 2 
                      Producer and 3 Consumer
                      processes.

   |   |   |   |   |
   |   |   |   |   |   |EXCHANGE:EMIT Operator
   |   |   |   |   |   |
   |   |   |   |   |   |   |RESTRICT Operator
   |   |   |   |   |   |   |
   |   |   |   |   |   |   |   |SCAN Operator
   |   |   |   |   |   |   |   |  FROM TABLE
   |   |   |   |   |   |   |   |  RA2
   |   |   |   |   |   |   |   |  Index : RA2_NC2L
   |   |   |   |   |   |   |   |  Forward Scan.
   |   |   |   |   |   |   |   |  Positioning at 
                                     index start.
   |   |   |   |   |   |   |   |  Executed in 
                                      parallel with 
                                      a 2-way
                                       partition scan.