The DB2 Predicates affect how DB2 selects the access path for the statement. Predicates are found in the WHERE, HAVING, or ON clauses of SQL statements; they describe attributes of data. Most DB2 predicates are based on the columns of a table. They either qualify rows (through an index) or reject rows (returned by a scan) when the table is accessed. The resulting qualified or rejected rows are independent of the access path that is chosen for that table.
DB2 Predicates in SQL statements are classified. These classifications dictate how DB2 processes the predicates and how much data is filtered during the process. These classifications are as follows:
An indexable predicate can match index entries; predicates that cannot match index entries are said to be non-indexable.
The DB2 Data Manager understands your indexes and tables and can use an index for efficient access to your data. Only a stage 1 predicate can limit the range of data accessed on a disk. Stage 1 is responsible for translating the data stored on pages into a result set of rows and columns.
Stage 1 predicates are better than stage 2 because they qualify rows earlier and reduce the amount of processing needed at stage 2.
If the predicate, which is classified as a stage 1, is evaluated after a join operation, then it is a stage 2 predicate. So all indexable predicates are stage 1, but not all stage 1 predicates are indexable.
Syntax – col op value
col – Indicates a column of a table.
Op – Indicates an operator such as =, >, <, >=, <=, and so on.
Value – Indicates an expression that does not contain a column from the table (a non-column expression).
Let’s consider index exists on the EMPNO column of the EMP table.
Example of indexable stage 1 predicate:
SELECT LASTNAME, FIRSTNME FROM EMP WHERE EMPNO = '000010'
Predicates containing NOT BETWEEN, NOT IN (for a list of values), NOT LIKE (without a leading search character), or LIKE (with a leading search character) can also be stage 1 indexable.
Example of non-indexable stage 1 predicate:
SELECT LASTNAME, FIRSTNME FROM EMP WHERE EMPNO <> '000010'
The stage 2 engine processes functions and expressions. But, it is unable to access data in indexes and tables directly. Data from stage 1 is passed to stage 2 for further processing. So, stage 1 predicates are more efficient than stage 2 predicates. Stage 2 predicates cannot use an index, and thus cannot limit the range of data retrieved from a disk.
Stage 2 (Relational Data Services) handles more complex predicates, data transformations, and computations. These Stage 2 predicates are much more expensive for DB2 to resolve than Stage 1 due to additional processing and additional code path. Additionally, RDS cannot make effective use of indexes.
Generally, stage 2 occurs after data accesses and performs such actions as sorting and evaluation of functions and expressions. Stage 2 predicates generally contain column expressions, correlated subqueries, CASE expressions, and so on.
Let’s consider EMPNO is a character column of fixed length 6. Below example is a range predicate comparing a character column to a character value that exceeds the length of the column which makes it a stage 2 predicate.
SELECT LASTNAME, FIRSTNME FROM EMP WHERE EMPNO > '00000010' SELECT LASTNAME, FIRSTNME FROM EMP WHERE SUBSTR(LASTNAME,1,1) = 'B'
The first set of rules:
The second set of rules describes the order of predicate evaluation within each of the above stages:
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.
The type of a predicate depends on its operator or syntax, as listed below. The type determines what precessing and filtering occurs when the predicate is evaluated.
When simple predicates are connected by an OR condition, the resulting compound predicate is evaluated at the higher stage of the two simple predicates. The following example contains two simple predicates that are combined by an OR. The first predicate is stage 1 indexable, and the second is non-indexable stage 1. The result is that the entire compound predicate is stage 1 and not indexable:
SELECT EMPNO FROM EMP WHERE WORKDEPT = 'C01' OR SEX <> 'M'
In the next example, the first simple predicate is stage 1 indexable, but the second (connected again by an OR) is stage 2. Thus, the entire compound predicate is stage 2:
SELECT EMPNO FROM EMP WHERE WORKDEPT = 'C01' OR SUBSTR(LASTNAME,1,1) = 'L'
BETWEEN is usually more efficient than <= predicate and the >= predicate except when comparing a host variable to 2 columns.
Stage 1: WHERE Col1 <= :hostvar AND col2 >= :hostvar Stage 2 : WHERE :hostvar BETWEEN col1 and col2
A well-maintained product backlog is crucial for successful product development. It serves as a single…
Incremental value to the customer refers to the gradual delivery of small, functional parts of…
A Product Market refers to the group of potential customers who might be interested in…
The Professional Agile Leadership - Evidence-Based Management (PAL-EBM) certification offered by Scrum.org is designed for…
The Professional Agile Leadership (PAL I) certification, offered by Scrum.org, is designed to equip leaders…
Choosing the right Scrum Master Certification depends on your current experience and career goals. If…