Summary of predicate processing
You can improve performance of your SQL statements by specifying predicates that are evaluated at earlier stages.
Processing order
Predicates are applied by stage in the following order:
- Indexable predicates that match on index key columns are applied and evaluated when the index is accessed.
- Stage 1 index screening predicates that have not been picked as index matching predicates but still refer to index columns, are applied to the index.
- Stage 1 page range screening predicates refer to partitioning columns are applied to limit the number of partitions that are accessed.
- Other stage 1 predicates are applied to the data, after data page access.
- The stage 2 predicates are applied on the returned data rows.
The STAGE column of DSN_FILTER_TABLE indicates the stage at which a predicate was applied.
Within each stage after the indexable stage, predicates are applied in the following order, by type:
- Equality predicates (including IN predicates that contain only a single item and BETWEEN predicates that contain the same value twice)
- Range predicates and predicates of the form column IS NOT NULL
- Other predicate types
After both sets of rules are applied, predicates are evaluated in the order in which they appear in the query. Because you specify that order, you have some control over the order of evaluation. However, Regardless of coding order, non-correlated subqueries are evaluated before correlated subqueries, unless Db2 correlates, de-correlates, or transforms the subquery into a join.
Predicate types and processing by stage
In general, if you form a compound predicate by combining several simple predicates with OR operators, the result of the operation has the same characteristics as the simple predicate that is evaluated latest. For example, if two indexable predicates are combined with an OR operator, the result is indexable. If a stage 1 predicate and a stage 2 predicate are combined with an OR operator, the result is stage 2.
- Indexable and stage 1 predicates 31
- The following predicates might be evaluated by matching index access, during index screening, or
after data page access during stage 1 processing.
COL = value
16, 31COL = noncol expr
9, 11, 12, 15, 29, 31, 32COL IS NULL
20, 21COL op value
13, 31COL op noncol expr
9, 11, 12, 13, 29, 31, 32value BETWEEN COL1 AND COL2
13, 32COL BETWEEN value1 AND value2
13COL BETWEEN noncol expr 1 AND noncol expr 2
9, 11, 12, 13, 23, 29COL BETWEEN expr-1 AND expr-2
6, 7, 11, 12, 13, 14, 15, 27, 29COL LIKE 'pattern'
29COL IN (list)
17, 18COL IS NOT NULL
21COL LIKE host variable
2, 29COL LIKE UPPER ('pattern')
29COL LIKE UPPER (host-variable)
2, 29COL LIKE UPPER (SQL-variable)
2, 29COL LIKE UPPER (global-variable)
2, 29COL LIKE UPPER (CAST ('pattern' AS data-type))
2, 29COL LIKE UPPER (CAST (host-variable AS data-type))
2, 29COL LIKE UPPER (CAST (SQL-variable AS data-type))
2, 29COL LIKE UPPER (CAST (global-variable AS data-type))
2, 292, 29
T1.COL = T2.COL
T1.COL op T2.COL
T1.COL = T2 col expr
6, 9, 11, 12, 14, 15, 25, 27, 29T1.COL op T2 col expr
6, 9, 11 , 12, 13, 14, 15, 29COL = (noncor subq)
COL op (noncor subq)
28COL = ANY (noncor subq)
22, 29(COL1,...COLn) IN (noncor subq)
29COL = ANY (cor subq)
19, 22, 29COL IS NOT DISTINCT FROM value
16COL IS NOT DISTINCT FROM noncol expr
9, 11, 12, 15, 29T1.COL1 IS NOT DISTINCT FROM T2.COL2
3, 4T1.COL1 IS NOT DISTINCT FROM T2 col expr
6, 9, 11, 12, 14, 15, 29COL IS NOT DISTINCT FROM (noncor subq)
SUBSTR(COL,1,n) = value
SUBSTR(COL,1,n) op value
DATE(COL) = value
33DATE(COL) op value33
YEAR(COL) = value
33YEAR(COL) op value
33
- Stage 1 not indexable predicates 31
- The following predicates might be evaluated during stage 1 processing, during index screening,
or after data page access.
COL <> value
8, 11COL <> noncol expr
8, 11, 29COL NOT BETWEEN value1 AND value2
COL NOT IN (list)
COL NOT LIKE ' char'
29COL LIKE '%char'
1, 29COL LIKE '_char'
1, 29T1.COL <> T2 col expr
8, 11, 27, 29COL op ANY (noncor subq)
22COL op ALL (noncor subq)
COL IS DISTINCT FROM value
8, 11COL IS DISTINCT FROM (noncor subq)
- Stage 2 predicates
- The following predicates must be processed during stage 2, after the data is returned.
COL BETWEEN COL1 AND COL2
10value NOT BETWEEN COL1 AND COL2
- value BETWEEN col expr and col expr32
T1.COL <> T2.COL
T1.COL1 = T1.COL2
3,25T1.COL1 op T1.COL2
3T1.COL1 <> T1.COL2
3COL = ALL (noncor subq)
COL <> (noncor subq)
22COL <> ALL (noncor subq)
COL NOT IN (noncor subq)
COL = (cor subq)
5COL = ALL (cor subq)
COL op (cor subq)
5COL op ANY (cor subq)
22COL op ALL (cor subq)
COL <> (cor subq)
5COL <> ANY (cor subq)
19(COL1,...COLn) IN (cor subq)
COL NOT IN (cor subq)
(COL1,...COLn) NOT IN (cor subq)
T1.COL1 IS DISTINCT FROM T2.COL2
3T1.COL1 IS DISTINCT FROM T2 col expr
8, 11COL IS NOT DISTINCT FROM (cor subq)
EXISTS (subq)
19expression = value
27, 32expression <> value
27expression op value
27, 32expression op (subq)
NOT XMLEXISTS
CASE expression WHEN expression ELSE expression END = value
32
- Indexable but not stage 1 predicates
- The following predicates can be processed during index access, but cannot be processed during
stage 1.
XMLEXISTS
26
- Indexable only if an ESCAPE character is specified and used in the LIKE predicate. For example, COL LIKE '+%char' ESCAPE '+' is indexable.
- Indexable only if the pattern in the variable is an indexable constant (for example, variable='char%').
- If both COL1 and COL2 are from the same table, access through an index on either one
is not considered for these predicates. However, the following query is an
exception:
By using correlation names, the query treats one table as if it were two separate tables. Therefore, indexes on columns C1 and C2 are considered for access.SELECT * FROM T1 A, T1 B WHERE A.C1 = B.C2;
- The predicate might be indexable and stage 1, if both sides contain the same data type. Otherwise, the predicate is stage 2.
- If the subquery has already been evaluated for a given correlation value, then the subquery might not have to be reevaluated.
- The column on the left side of the join sequence must be in a different table from any columns on the right side of the join sequence.
- The tables that contain the columns in expression1 or expression2 must already have been accessed.
- The processing for WHERE NOT COL = value is like that for WHERE COL <> value, and so on.
- If noncol expr, noncol expr1, or
noncol expr2 is a noncolumn expression of one of these forms, then the predicate
is not indexable:
- noncol expr + 0
- noncol expr - 0
- noncol expr * 1
- noncol expr / 1
- noncol expr CONCAT empty string
- COL, COL1, and COL2 can be the same column or different columns. The columns are in the same table.
- Any
of the following sets of conditions make the predicate stage 2:
- The first value obtained before the predicate is evaluated is BIGINT or DECIMAL(p,s), where p>15, and the second value obtained before the predicate is evaluated is REAL or FLOAT.
- The first value obtained before the predicate is evaluated is CHAR, VARCHAR, GRAPHIC, or VARGRAPHIC, and the second value obtained before the predicate is evaluated is DATE, TIME, or TIMESTAMP.
- The predicate is stage 1 but not indexable if the first value obtained before the predicate is evaluated is CHAR or VARCHAR, the second value obtained before the predicate is evaluated is GRAPHIC or VARGRAPHIC, and the first value obtained before the predicate is evaluated is not Unicode mixed.
- If
both sides of the comparison are strings, any of the following sets of conditions makes the
predicate stage 1 but not indexable:
- The first value obtained before the predicate is evaluated is CHAR or VARCHAR, and the second value obtained before the predicate is evaluated is GRAPHIC or VARGRAPHIC.
- Both of the following conditions are true:
- Both sides of the comparison are CHAR or VARCHAR, or both sides of the comparison are BINARY or VARBINARY
- The length the first value obtained before the predicate is evaluated is less than the length of the second value obtained before the predicate is evaluated.
- Both of the following conditions are true:
- Both sides of the comparison are GRAPHIC or VARGRAPHIC.
- The length of the first value obtained before the predicate is evaluated is less than the length of the second value obtained before the predicate is evaluated.
- Both of the following conditions are true:
- The first value obtained before the predicate is evaluated is GRAPHIC or VARGRAPHIC, and the second value obtained before the predicate is evaluated is CHAR or VARCHAR.
- The length of the first value obtained before the predicate is evaluated is less than the length of the second value obtained before the predicate is evaluated.
- If both sides of the comparison are strings, but the two sides have different CCSIDs, the predicate is stage 1 and indexable only if the first value obtained before the predicate is evaluated is Unicode and the comparison does not meet any of the conditions in note 13.
- If all of the following conditions are true, the predicate is
stage 2:
- col expr or noncol expr is an integer value that is the product or the quotient of two non-column expressions
- COL is a FLOAT or a DECIMAL column
- If COL has the ROWID data type, Db2 tries to use direct row access instead of index access or a table space scan.
- If COL has the ROWID data type, and an index is defined on COL, Db2 tries to use direct row access instead of index access.
- IN-list predicates are indexable and stage 1 if the following conditions
are true:
- The IN list contains only simple items. For example, constants, host variables, parameter markers, and special registers.
- The IN list does not contain any aggregate functions or scalar functions.
- The IN list is not contained in a trigger's WHEN clause.
- For numeric predicates where the left side column is DECIMAL with precision greater than 15, none of the items in the IN list are FLOAT.
- For string predicates, the coded character set identifier is the same as the identifier for the left side column.
- For DATE, TIME, and TIMESTAMP predicates, the left side column must be DATE, TIME, or TIMESTAMP.
- Certain predicates might become indexable and stage 1 depending on how they are transformed during processing.
- The predicate types COL IS NULL and COL IS NOT NULL are stage 2 predicates when they query a column that is defined as NOT NULL.
- If the predicate type is COL IS NULL and the column is defined as NOT NULL, the table is not accessed because C1 cannot be NULL.
- The ANY and SOME keywords behave similarly. If a predicate with the ANY keyword is not indexable and not stage 1, a similar predicate with the SOME keyword is not indexable and not stage 1.
- Under either of these circumstances, the predicate is stage 2:
- noncol expr is a case expression.
- noncol expr is the product or the quotient of two noncolumn expressions, that product or quotient is an integer value, and COL is a FLOAT or a DECIMAL column.
- COL IN (noncor subq) is stage 1 for type N access only. Otherwise, it is stage 2.
- If the inner table is an EBCDIC or ASCII column and the outer table is a Unicode column, the predicate is stage 1 and indexable.
- The XMLEXISTS is always stage 2. But the same predicate can be indexable and become the matching predicate if an XML index can be used to evaluate the XPath expression in the predicate. The XMLEXISTS predicate can never be a screening predicate.
- The predicate might be indexable by an expression-based index if it contains an expression that is a column reference, invokes a built-in function, or contains a general expression.
- This type of predicate is not stage 1 when a nullability mismatch is possible.
- If COL is defined with a field procedure, the predicate becomes stage 2.
- Join predicates of this type might be eligible for page range screening if the two columns have the same data type, length, and encoding scheme.
-
The following predicates might be indexable and stage 1 if only the right side
contains a DECFLOAT data
type:
-
COL = value
-
COL = noncol expr
-
COL op value
-
COL op noncol expr
-
- The
predicate might be indexable if expression contains one of the following scalar
functions:
- DATE
- YEAR
- SUBSTR (if the start value for the substring is 1.)
- COL is a DATE, TIME, or TIMESTAMP column.
The following abbreviations and variable values are used in the preceding in the sample predicates:
- char
- Any character string that does not include the special characters for percent (%) or underscore (_).
- COL
- A column name.
- col expr
- A column expression.
- cor subq
- A correlated subquery
- expression
- Any expression that contains arithmetic operators, scalar functions, aggregate functions, concatenation operators, columns, constants, host variables, special registers, or date or time expressions.
- noncol expr
- A non-column expression, which is any expression that does not contain a column. That expression
can contain arithmetic operators, scalar functions, concatenation operators, constants, host
variables, special registers, or date or time expressions.
An example of a non-column expression is
CURRENT DATE - 50 DAYS
- noncor subq
- A non-correlated subquery
- op
- any of the operators >, >=, <, <=, ¬>, ¬<
- predicate
- A predicate of any type.
- pattern
- Any character string that does not start with the special characters for percent (%) or underscore (_).
- subq
- A correlated or noncorrelated subquery
- Tn
- A table name.
- Tn col expr
- An expression that contains a column in table Tn. The expression might be only that column.
- value
- A constant, host variable, or special register.