You are on page 1of 4

SQL Optimization:

 Two types of SQL Optimization Engines:


o Classic Query Engine – CSE (pre V5R2)
o SQL Query Engine – SQE - (post V5R2)
 The goal is to use SQE, the optimizer will select which engine to use according to how you write
your SQL.
 When you use the logical file in you SQL select statement it will always use the CSE.
 The V6R1 query dispatcher:
o Dispatched to CQE if
 Logical File reference
 Distributed queries via DB2 Multisystem
 Non SQL queries (QQQQRY API, QUERY/400, OPENQRYF)
o SQE now optimizes
 National Language sort sequences
 UPPER, LOWER, & translate functions
 UDTF’s
 Length, position and substr with UTF_8 and UTF_16 arguments
o Only SQE optimizes
 Values on FROM
 Grouping Sets
 Rollup & Cube
 Full Outer Join
 SQL Derived indexes
o Dispatcher 7.1
 CQE dispatched only if:
 Distributed queries via DB2 Multisystem
 Non_SQL queries (QQQQRY API, QUERY/400, OPNQRYF)
 SQE now optimizes Logical file references
 Optimizes:
 EVI Sum
 Adaptive Query Processing
 Brings resident Objects into memory
o Query Dispatcher
 Back up to CQE to complete optimization if it discovers something that it does
not support during optimization:
 Select/omit logical file
 Logical over multiple members
 Join logical files
 Derived keys
o Renaming fields, adding a translate or only selecting a subset of
the columns in LF
o Specifying an alternate collating sequence on a field
o Sort sequence specified for index or logical file
o SQL Query Processing – new Request
 Optimize
 Open
 Run
o The optimizer writes the best program to fulfill your request
 Provides the recipe
 Provides the methods
 Does no cooking
o Query processing can be divided into 4 phases:
 Query Validation
 Validate the query request
 Validate existing access plan
 Builds internal query structures
 Query Optimization
 Choose most efficient access method
 Builds access plan
 Query Execution
 Build the structures needed for query cursor
 Build the structures for any temporary indexes (if needed)
 Builds and activates query cursor (ODP)
 Monitors execution and Adapts if needed(7.1)
 Generate any information requested
o Sql Performance Monitor data
o Index Advised
o Etc
Data Access Methods
 Cost based optimization dictates that the fastest access method for a
given table will vary based upon selectivity of the query. The method
to use depends on the number of expected rows returned

 Statistics & Access Method Efficiency
 With the correct statistics, cost-based query optimizers more accurately estimate the
number of rows to process
o Better estimates = better performing plans
 All query optimizers rely on stats for plan decisions
o Most DBMS products rely on manual stats collection
o Pre-SQE DB2 for i only had indexes as a statistic source
o Post-SQE DB2 for I relies on index stats, automatic column statistics

See demo of Refresh SQL:

Optimization

Types of indexes.

Binary radix indexes

A radix index is a multilevel, hybrid tree structure that allows a large number of key values to be stored efficiently
while minimizing access times. A key compression algorithm assists in this process. The lowest level of the tree
contains the leaf nodes, which contain the address of the rows in the base table that are associated with the key
value. The key value is used to quickly navigator to the leaf node with a few simple binary search tests.

The binary radix tree structure is very good for finding a small number of rows because it is able to find a given
row with a minimal amount of processing. For example, using a binary radix index over a customer number column
for a typical OLTP request like "find the outstanding orders for a single customer: will result in fast performance. An
index created over the customer number column is considered to be the perfect index for this type of query
because it allows the database to zero in on the rows it needs and perform a minimal number of I/Os.

Encoded Vector Index (EVI) An encoded vector index is a permanent object that provides access to a table by
assigning codes to distinct key values and then representing those values in a vector.The size of the vector will
match the number of rows in the underlying table.

Each vector entry represents the table row number in the same position. The codes generated to represent the
distinct key values can be 1, 2 or 4 bytes in length, depending upon the number of distinct values that need to be
represented. Because of their compact size and relative simplicity, the EVI can be used to process large amounts of
data very efficiently.

Even though an encoded vector index is used to represent the values stored in a table, the index itself cannot be
used to directly gain access to the table. Instead, the encoded vector index can only be used to generate either a
temporary row number list or a temporary row number bitmap. These temporary objects can then be used in
conjunction with a Table Probe to specify the rows in the table that the query needs to process. The main
difference with the Table Probe associated with an encoded vector index (versus a radix index) is that the paging
associated with the table can be asynchronous. The I/O can now be scheduled more efficiently to take advantage
of groups of selected rows. Large portions of the table can be skipped over where no rows are selected.

Use the science and art of query optimization My recent documents

Example: of SQL written last year:

SELECT distinct
c.Exenme,d.cscnme,b.cmktid,b.mktdsc,b.cchnid,b.chndsc,b.ctype,
b.ctdsc,a.cmstor,a.ccust,a.cnme,a.cste,G.sspplc,G.sssys,
coalesce(G.ssefto,'0001-01-01') as effto,f.smid,f.smdesc,f.smftg
FROM RCMJ001 A {Rcm join to RCEP001)
JOIN SMART01/H_CUS00004 B ON A.CTYPE = B.CTYPE
JOIN SMART01/H_STA00003 C ON A.csal = c.accexe
join smart01/h_cus00011 d on a.cmcsc = d.cscid
join rssp001 G on a.cmstor = G.ssstor
join ismp001 f on G.sspplc = f.smplcd and G.sssys = f.smsys
WHERE a.CMSTTS= 0 and G.sspplc = 'EVDCTR' and
YEAR(ssefto) = 9999
and f.smid = 'SM' and f.smftg >= 2;

example 2:

SELECT distinct
c.Exenme,d.cscnme,b.cmktid,b.mktdsc,b.cchnid,b.chndsc,b.ctype,
b.ctdsc,h.cmstor,a.ccust,a.cnme,a.cste,G.sspplc,G.sssys,
coalesce(G.ssefto,'0001-01-01') as effto,f.smid,f.smdesc,f.smftg
FROM RCM A join rcep001 h on ccust = cmcust
JOIN SMART01/H_CUS00004 B ON A.CTYPE = B.CTYPE
JOIN SMART01/H_STA00003 C ON A.csal = c.accexe
join smart01/h_cus00011 d on H.cmcsc = d.cscid
join rssp001 G on h.cmstor = G.ssstor
join ismp001 f on G.sspplc = f.smplcd and G.sssys = f.smsys
WHERE H.CMSTTS= 0 and G.sspplc = 'EVDCTR' and
YEAR(ssefto) = 9999 and A.cmid = 'CM'
and f.smid = 'SM' and f.smftg >= 2;

Run index advisor

You might also like