03 - Indexing Strategy S3
03 - Indexing Strategy S3
Indexing Strategy
IBM Systems Lab Services – January 2019 Copyright 2019 IBM Corporation
Db2 for i
Indexing Concepts
IBM Systems Lab Services – January 2019 Copyright 2019 IBM Corporation
Db2 for i
Indexing Concepts
IBM Systems Lab Services – January 2019 Copyright 2019 IBM Corporation
Db2 for i
Indexes with Derived Keys
• Indexes built on values derived from the column values can help
with a variety of predicates:
IBM Systems Lab Services – January 2019 Copyright 2019 IBM Corporation
Db2 for i
Indexes with Derived Keys
IBM Systems Lab Services – January 2019 Copyright 2019 IBM Corporation
Db2 for i
Many Indexes Are Already Derived
• The same idea applies for indexes built to support different CCSID’s which may
have different representations based on their associated national language
IBM Systems Lab Services – January 2019 Copyright 2019 IBM Corporation
Db2 for i
Sparse Indexes
The SQE optimizer was enhanced in IBM i 7.1 to support sparse indexes
Even though they are supported, these sparse indexes are rarely useful
IBM Systems Lab Services – January 2019 Copyright 2019 IBM Corporation
Db2 for i
Indexing Concepts
IBM Systems Lab Services – January 2019 Copyright 2019 IBM Corporation
Db2 for i
Indexing Concepts
IBM Systems Lab Services – January 2019 Copyright 2019 IBM Corporation
Db2 for i
Indexing Concepts
IBM Systems Lab Services – January 2019 Copyright 2019 IBM Corporation
Db2 for i
Indexing Concepts
In general…
• The query optimizer will make the final decision based on cost
IBM Systems Lab Services – January 2019 Copyright 2019 IBM Corporation
Db2 for i
Indexing Concepts
IBM Systems Lab Services – January 2019 Copyright 2019 IBM Corporation
Db2 for i
Indexing Advice from the Optimizer
IBM Systems Lab Services – January 2019 Copyright 2019 IBM Corporation
Db2 for i
Indexing Advice from the SQE Optimizer
• No derived keys
• No sparse indexes
• No EVI aggregate (include) columns
• Check advised EVI’s for cardinality/selectivity
IBM Systems Lab Services – January 2019 Copyright 2019 IBM Corporation
Db2 for i
Index Advised – System wide
• System only adds (summary) rows, user must manage the data
– Options to clear or prune
IBM Systems Lab Services – January 2019 Copyright 2019 IBM Corporation
Db2 for i
Visual Explain
IBM Systems Lab Services – January 2019 Copyright 2019 IBM Corporation
Db2 for i
Indexing Concepts
IBM Systems Lab Services – January 2019 Copyright 2019 IBM Corporation
Db2 for i
Autonomic Index Creation
IBM Systems Lab Services – January 2019 Copyright 2019 IBM Corporation
Db2 for i
Autonomic Index Creation – SQE in Action
Estimated No Indexes
Run
Time
IX Create Time
Maintained Temp IX
Permanent IX
Run 1
IPL Run 100
Time
IBM Systems Lab Services – January 2019 Copyright 2019 IBM Corporation
Db2 for i
Indexing Concepts
IBM Systems Lab Services – January 2019 Copyright 2019 IBM Corporation
Db2 for i
Index Maintenance
IBM Systems Lab Services – January 2019 Copyright 2019 IBM Corporation
Db2 for i
Indexes Made Simple
NO / Not Sure
Is the index
Create the index
created as an MTI?
YES
NO
YES
More logic required
IBM Systems Lab Services – January 2019 Copyright 2019 IBM Corporation
Db2 for i
Will the Index Pay for Itself?
IBM Systems Lab Services – January 2019 Copyright 2019 IBM Corporation
Db2 for i
Is the Index Created as an MTI?
• If the MTI was created, remember that the cost of creating the
index was amortized against the performance of running
without it.
– Index maintenance costs were not considered.
– More predictable performance for queries that don’t run as often and
may only sometimes result in MTI’s
IBM Systems Lab Services – January 2019 Copyright 2019 IBM Corporation
Db2 for i
Are There Many Changes to the Table?
IBM Systems Lab Services – January 2019 Copyright 2019 IBM Corporation
Db2 for i
More Logic Required
IBM Systems Lab Services – January 2019 Copyright 2019 IBM Corporation
Db2 for i
Maximizing Index Value
IBM Systems Lab Services – January 2019 Copyright 2019 IBM Corporation
Db2 for i
Maximizing Index Value
• Check which indexes are being used and which ones are
redundant
IBM Systems Lab Services – January 2019 Copyright 2019 IBM Corporation
Db2 for i
Index Evaluator – Work with Indexes
IBM Systems Lab Services – January 2019 Copyright 2019 IBM Corporation
Db2 for i
Index Evaluator – Work with Indexes…
IBM Systems Lab Services – January 2019 Copyright 2019 IBM Corporation
Db2 for i
Don’t Forget Change Control
IBM Systems Lab Services – January 2019 Copyright 2019 IBM Corporation
Db2 for i
Customer Example
• What’s wrong?
IBM Systems Lab Services – January 2019 Copyright 2019 IBM Corporation
Db2 for i
Customer Example …
• The index was scanned because customer number was character in the
table but numeric in the front end user interface
• Implicit conversion works on some interfaces but the rules for blank padding
try to be tolerant
• The engine scanned for the customer number embedded within blanks
• Changing the front end to supply the value as character let the query run
many times faster
• The encoding of the key value in the index made a big difference to
performance!
IBM Systems Lab Services – January 2019 Copyright 2019 IBM Corporation
Db2 for i
Effective Index Usage
• Match the data types and attributes of the column value and
search values (either host variables or constants)
IBM Systems Lab Services – January 2019 Copyright 2019 IBM Corporation
Db2 for i
Effective Index Usage
Table scan
IBM Systems Lab Services – January 2019 Copyright 2019 IBM Corporation
Db2 for i
Indexing Concepts
IBM Systems Lab Services – January 2019 Copyright 2019 IBM Corporation
Db2 for i
Best Practices – Index Creation
– It allows for parallel index builds including prefetch of data from storage
– The Db2 SMP feature must be installed (Option 26)
– There must be sufficient partition resources to allow parallel processing –
processor, disk, and memory. More resources means faster index
creation.
– Use the settings described in the SMP section of the workshop:
*MAX should be used sparingly if at all
*OPTIMIZE is the usual choice
IBM Systems Lab Services – January 2019 Copyright 2019 IBM Corporation
Db2 for i
Index Strategy – Index Creation for EVI’s
IBM Systems Lab Services – January 2019 Copyright 2019 IBM Corporation
Db2 for i
Index Maintenance
Row data
Maintain indexes... ...then insert row data into table
MY_TABLE
IX1 IX2 IX3 IX4
Time
IBM Systems Lab Services – January 2019 Copyright 2019 IBM Corporation
Db2 for i
Parallel Index Maintenance with Db2 SMP
Row data
Maintain indexes in parallel...
...then insert row data into table
IX1
IX2
IX3 MY_TABLE
IX4
Time
IBM Systems Lab Services – January 2019 Copyright 2019 IBM Corporation
Db2 for i
Insert into a table with REUSEDLT(*YES)
– Insert requests will reuse deleted rows one row at a time (blocking turned off)
– Index maintenance takes place synchronously as part of the row operation
Maintain indexes...then insert single row data into table reusing deleted rows
Deleted rows
Time
IBM Systems Lab Services – January 2019 Copyright 2019 IBM Corporation
Db2 for i
Insert into a table with REUSEDLT(*NO)
– Insert requests are inserted at the end and benefit from Db2 level row blocking
– Index maintenance occurs in parallel when inserting blocked data
INSERT INTO MY_TABLE...
Maintain indexes in parallel...then insert blocked data into table at the end
Deleted rows
IX1
IX2 MY_TABLE
IX3
IX4
Time
IBM Systems Lab Services – January 2019 Copyright 2019 IBM Corporation
Db2 for i
Insert into a table with REUSEDLT(*NO)
IBM Systems Lab Services – January 2019 Copyright 2019 IBM Corporation
Db2 for i
Indexing Strategies
IBM Systems Lab Services – January 2019 Copyright 2019 IBM Corporation
Db2 for i
Indexing Concepts
IBM Systems Lab Services – January 2019 Copyright 2019 IBM Corporation
Db2 for i
Data Access Methods and Strategies
High
Response
Time Table Scan
Skip Seq
Low
Ix Probe
Few Many
Number of rows searched / accessed
IBM Systems Lab Services – January 2019 Copyright 2019 IBM Corporation
Db2 for i
The Process of Identifying Indexes
Proactive method
• Analyze the data model, application and SQL requests
• Database PK, UK, RI constraints are helpful
Reactive method
• Rely on optimizer feedback and actual implementation methods
• Rely on SQE’s ability to auto tune using temporary indexes
IBM Systems Lab Services – January 2019 Copyright 2019 IBM Corporation
Db2 for i
Indexing Strategy - Basic Approach
Radix Indexes
• Common local selection columns
Minimum
• Join columns
• Local selection columns + join columns
• Local selection columns + grouping columns
• Local selection columns + ordering columns
Advanced
Requires
knowledge of
Encoded Vector Indexes query
• Local selection column for index ANDing/ORing optimization
and data
• Join columns (star or snowflake schema) lifecycle
• Index only access
•DISTINCT, COUNT, COUNT DISTINCT, SUM()
Note: Columns used with equal conditions are first in key list
IBM Systems Lab Services – January 2019 Copyright 2019 IBM Corporation
Db2 for i
Indexing Strategy - Examples
CUSTOMERS
custkey
ORDERS
orderkey
custkey
DATES partkey
datekey orderdate PARTS
shipdate partkey
suppkey SUPPLIERS
suppkey
IBM Systems Lab Services – January 2019 Copyright 2019 IBM Corporation
Db2 for i
Art - The Perfect Index
• SELECTING
– Table scan
– Table scan or probe via bitmap or RRN list
– Index scan or probe
• JOINING
– Index scan or probe
Assume the query
– Hash table probe
is highly selective,
– Sorted list probe and using an index
• GROUPING is the best strategy
– Index scan or probe
– Hash table scan
• ORDERING
– Index scan or probe
– Sorted list scan
IBM Systems Lab Services – January 2019 Copyright 2019 IBM Corporation
Db2 for i
Indexing Strategy - Example
SELECT d.year, d.week, s.supplier, o.orderkey, o.quantity, o.revenue_wo_tax
FROM ORDERS o
INNER JOIN SUPPLIERS s ON (o.suppkey = s.suppkey)
INNER JOIN DATES d ON (o.shipdate = d.datekey)
WHERE s.country = 'UNITED STATES'
AND d.week = 52
ORDER BY s.supplier;
IBM Systems Lab Services – January 2019 Copyright 2019 IBM Corporation
Db2 for i
Indexing Strategy - Example
SELECT d.year, d.week, s.supplier, o.orderkey, o.quantity, o.revenue_wo_tax
FROM ORDERS o
INNER JOIN SUPPLIERS s ON (o.suppkey = s.suppkey)
INNER JOIN DATES d ON (o.shipdate = d.datekey)
WHERE s.country = 'UNITED STATES'
AND d.week = 52
ORDER BY s.supplier;
week = 52
supplier= UNITED STATES
IBM Systems Lab Services – January 2019 Copyright 2019 IBM Corporation
Db2 for i
Indexing Strategy - Example
SELECT d.year, d.week, s.supplier, o.orderkey, o.quantity, o.revenue_wo_tax
FROM ORDERS o
INNER JOIN SUPPLIERS s ON (o.suppkey = s.suppkey)
INNER JOIN DATES d ON (o.shipdate = d.datekey)
WHERE s.country = 'UNITED STATES'
AND d.week = 52
ORDER BY s.supplier;
IBM Systems Lab Services – January 2019 Copyright 2019 IBM Corporation
Db2 for i
Indexing Strategy - Example
SELECT d.year, d.week, s.supplier, o.orderkey, o.quantity, o.revenue_wo_tax
FROM ORDERS o
INNER JOIN SUPPLIERS s ON (o.suppkey = s.suppkey)
INNER JOIN DATES d ON (o.shipdate = d.datekey)
WHERE s.country = 'UNITED STATES'
AND d.week = 52 o.shipdate = d.datekey
ORDER BY s.supplier;
o.suppkey = s.suppkey
IBM Systems Lab Services – January 2019 Copyright 2019 IBM Corporation
Db2 for i
Indexing Strategy - Example
SELECT d.year, d.week, s.supplier, o.orderkey, o.quantity, o.revenue_wo_tax
FROM ORDERS o
INNER JOIN SUPPLIERS s ON (o.suppkey = s.suppkey)
INNER JOIN DATES d ON (o.shipdate = d.datekey)
WHERE s.country = 'UNITED STATES'
AND d.week = 52
ORDER BY s.supplier;
IBM Systems Lab Services – January 2019 Copyright 2019 IBM Corporation
Db2 for i
Indexing Strategy - Example
SELECT d.year, d.week, s.supplier, o.orderkey, o.quantity, o.revenue_wo_tax
FROM ORDERS o
INNER JOIN SUPPLIERS s ON (o.suppkey = s.suppkey)
INNER JOIN DATES d ON (o.shipdate = d.datekey)
WHERE s.country = 'UNITED STATES' s.supplier
AND d.week = 52
ORDER BY s.supplier;
o.shipdate=d.datekey
o.suppkey = s.suppkey
IBM Systems Lab Services – January 2019 Copyright 2019 IBM Corporation
Db2 for i
Indexing Strategy - Example
SELECT d.year, d.week, s.supplier, o.orderkey, o.quantity, o.revenue_wo_tax
FROM ORDERS o
INNER JOIN SUPPLIERS s ON (o.suppkey = s.suppkey)
INNER JOIN DATES d ON (o.shipdate = d.datekey)
WHERE s.country = 'UNITED STATES'
AND d.week = 52
ORDER BY s.supplier;
IBM Systems Lab Services – January 2019 Copyright 2019 IBM Corporation
Db2 for i
Indexing Strategy - Example
SELECT d.year, d.week, s.supplier, o.orderkey, o.quantity, o.revenue_wo_tax
FROM ORDERS o
INNER JOIN SUPPLIERS s ON (o.suppkey = s.suppkey)
INNER JOIN DATES d ON (o.shipdate = d.datekey)
WHERE s.country = 'UNITED STATES'
AND d.week = 52
ORDER BY s.supplier;
IBM Systems Lab Services – January 2019 Copyright 2019 IBM Corporation
Db2 for i
Indexing Strategy - Example
SELECT d.year, d.week, s.supplier, o.orderkey, o.quantity, o.revenue_wo_tax
FROM ORDERS o
INNER JOIN SUPPLIERS s ON (o.suppkey = s.suppkey)
INNER JOIN DATES d ON (o.shipdate = d.datekey)
WHERE s.country = 'UNITED STATES'
AND d.week = 52
ORDER BY s.supplier;
IBM Systems Lab Services – January 2019 Copyright 2019 IBM Corporation
Db2 for i
Indexing Strategy - Examples
-- Query 1
SELECT A.CUSTOMER_NO, A.ORDER_DATE, A.QUANTITY
FROM ORDERS A
WHERE A.CUSTOMER_NO = 0112358;
-- Query 2
SELECT A.CUSTOMER_NO, A.ORDER_DATE, A.QUANTITY
FROM ORDERS A
WHERE A.CUSTOMER_NO = 0112358
AND A.ITEM_ID = ‘ABC123’;
IBM Systems Lab Services – January 2019 Copyright 2019 IBM Corporation
Db2 for i
Indexing Strategy - Examples
-- Query 3
SELECT A.CUSTOMER_NO, A.CUSTOMER, A.ORDER_DATE
FROM ORDERS A
WHERE A.CUSTOMER_NO IN (0112358, 1321345, 5891442)
AND A.ORDER_DATE > ‘2014/06/30’
ORDER BY A.ORDER_DATE;
-- Query 4
SELECT A.CUSTOMER_NO, A.CUSTOMER, A.ORDER_DATE
FROM ORDERS A
WHERE A.CUSTOMER_NO = 0112358
OR A.ORDER_DATE = ‘2014/06/30’;
IBM Systems Lab Services – January 2019 Copyright 2019 IBM Corporation
Db2 for i
Indexing Strategy - Examples
-- Query 5
SELECT *
FROM ORDERS A
WHERE UPPER(A.LASTNAME) = ‘MCLOUTH’
ORDER BY A.ORDERDATE DESC
FETCH FIRST 10 ROWS ONLY;
IBM Systems Lab Services – January 2019 Copyright 2019 IBM Corporation
Db2 for i
Indexing Strategy - Examples
SELECT *
FROM ORDERS A
WHERE UPPER(A.LASTNAME) = ‘MCLOUTH’
ORDER BY A.ORDERDATE
FETCH FIRST 10 ROWS ONLY;
IBM Systems Lab Services – January 2019 Copyright 2019 IBM Corporation
Db2 for i
Indexing Strategy - Examples
-- Query 6
SELECT A.CUSTOMER_NO, B.CUSTOMER, A.ORDER_DATE, A.QUANTITY
FROM ORDERS A,
CUSTOMERS B,
ITEMS C
WHERE A.CUSTKEY = B.CUSTKEY
AND A.ITEMKEY = C.ITEMKEY
AND A.CUSTOMER_NO = 0112358;
IBM Systems Lab Services – January 2019 Copyright 2019 IBM Corporation
Db2 for i
Indexing Strategy - Examples
-- Query 7
SELECT A.CUSTOMER_NO, B.CUSTOMER, A.ORDER_DATE, A.QUANTITY
FROM ORDERS A
INNER JOIN CUSTOMERS B
ON A.CUSTKEY = B.CUSTKEY
INNER JOIN ITEMS C
ON A.ITEMKEY = C.ITEMKEY
WHERE B. CUSTOMER LIKE ‘CAIN%’;
IBM Systems Lab Services – January 2019 Copyright 2019 IBM Corporation
Db2 for i
Tuning Subqueries
IBM Systems Lab Services – January 2019 Copyright 2019 IBM Corporation
Db2 for i
Join via Multiple Key Column Probe
• Index with three keys: STORE, DATE, and EMPNUM (in any
order) can be used to satisfy both the join and non-join selection
predicates on the table SALES in one query step.
IBM Systems Lab Services – January 2019 Copyright 2019 IBM Corporation
Db2 for i
Indexing Strategy - Examples
IBM Systems Lab Services – January 2019 Copyright 2019 IBM Corporation
Db2 for i
Art - The Perfect Index
Remember: a given perfect index may only cover a small set of queries
Skip
sequential
with
RRN lists
Keyed access
with Temporary All
"perfect" data Queries
indexes structures
Temporary
indexes
IBM Systems Lab Services – January 2019 Copyright 2019 IBM Corporation
Db2 for i
LAB
Art of Perfect Indexes
IBM Systems Lab Services – January 2019 Copyright 2019 IBM Corporation