How To Influence The DB2 Query Optimizer Using Optimization Profiles
How To Influence The DB2 Query Optimizer Using Optimization Profiles
Tom Eliaz
teliaz@us.ibm.com
IBM
Monday, May 8, 2006 • 10:20 a.m. – 11:30 a.m.
1
How to Influence the DB2 Query Optimizer
Using Optimization Profiles
¾ How to influence the optimizer's choice of access method, join
method, and join order
Hi. Thank you for reading the presentation notes for my talk. The majority of this
talk will be presented as a demonstration, and the slides are available for your
reference during and after the talk. They cover the demonstration material in more
detail. I hope you enjoy the demonstration, the slides and the presentation notes.
These notes are informal. I recommend reading the optimization profile
documentation for more details about advanced syntax.
Have you ever wanted to tell DB2 exactly which index to use?
Optimization Profiles help you control the rewrites and optimizations for your
query.
I will describe the power of DB2’s optimization profile.
The different areas you can impact using profiles.
Targeting a profile for a specific query.
Targeting a profile to all queries executed while it is enabled.
Cost-based guidelines (access and join methods and plan topology)
Rewrite guidelines (subquery-to-join, inlist-to-join)
Global guidelines (optimization class, reopt, MQTs, query degree)
I will describe how to write, enable, manage, and debug optimization profiles.
Along the way, I will provide basic optimizer tuning tips which should help you
avoid the need for profiles in all but the most desperate circumstances.
2
Motivation
¾ The DB2 optimizer is responsible for choosing the best access
plan for your query
Optimization Profiles
Plus a little something extra, watch for the
3
The running analogy in the talk is the quiver and the arrow. You have an existing set
of performance tools to optimize your queries. These are arrows in your quiver. I
hope to add optimization profiles as another tool, to be used only as a last resort.
For those reading the notes, I can let you in on the secret. The diamond represents
XML. You’ll find out why soon!
3
The Big Picture – Optimization Profiles
Admissions:
I like to have fun
ILIA Founder and Member
(I Like Icons Association)
ILIA is the I Like Icons Association. As you can already see, I use icons everywhere
in my slides. I think they’re fun and I hope you get a kick out of them.
4
Optimization Profile Overview
A small rant: One of the main advances with SQL was the separation of the application from the
schema, declarative query language. We worked hard to keep profiles out of the application. Profiles
talk about your schema, your indexes, your data. Good application design means keeping these
profiles out of your queries.
5
Optimization Profile Overview
Profiles are powerful things, and should be used with caution. You want to make sure you understand
what’s wrong with your query before you twist the compiler’s arm.
6
Agenda
¾ Big Picture
¾ Agenda
¾ Optimization profiles: first look
¾ Types of optimization profiles
¾ Access (index…)
¾ Join
¾ Rewrites (subquery to join…)
¾ Global (opt class, reopt, MQTs)
¾ Writing optimization profiles
¾ Putting optimization profiles into effect
¾ Managing optimization profiles
¾ Diagnostics
Lots of fun stuff to cover, but hopefully you can get the high level view fairly
quickly.
7
Overview – How Can You Nudge The Optimizer
¾ Understand what’s wrong with your query
¾ Use today’s tools to tune DB2
¾ If those don’t work, use a profile as last resort
Margarita: When things are so bad that you need a profile, then only a profile will
do. And once you’ve overcome your major problem with a tiny bit of text, you’ll
have enough time to relax at the pub. Enjoy your Margarita, it’s on the profiles
team!
8
An Example Query
SELECT S.S_NAME, S.S_ADDRESS, S.S_PHONE, S.S_COMMENT
FROM TPCD.PARTS, TPCD.SUPPLIERS S, TPCD.PARTSUPP PS
WHERE P_PARTKEY = PS.PS_PARTKEY AND
S.S_SUPPKEY = PS.PS_SUPPKEY AND
P_SIZE IN (39,40,45,48) AND
P_TYPE = ’BRASS’ AND
S.S_NATION IN ('MOROCCO', 'SPAIN') AND
PS.PS_SUPPLYCOST = (SELECT MIN(PS1.PS_SUPPLYCOST)
FROM TPCD.PARTSUPP PS1, TPCD.SUPPLIERS S1
WHERE TPCD.PARTS.P_PARTKEY = PS1.PS_PARTKEY
AND S1.S_SUPPKEY = PS1.PS_SUPPKEY AND
S1.S_NATION = S.S_NATION)
ORDER BY S.S_NAME
Index isn’t used
We can fix that right up, and much more, using profiles.
9
An Example Query
SELECT S.S_NAME, S.S_ADDRESS, S.S_PHONE, S.S_COMMENT
FROM TPCD.PARTS, TPCD.SUPPLIERS S, TPCD.PARTSUPP PS
WHERE P_PARTKEY = PS.PS_PARTKEY AND S.S_SUPPKEY = PS.PS_SUPPKEY AND
P_SIZE IN (39,40,45,48) AND P_TYPE = ’BRASS’ AND
S.S_NATION IN ('MOROCCO', 'SPAIN') AND
PS.PS_SUPPLYCOST = (SELECT MIN(PS1.PS_SUPPLYCOST)
FROM TPCD.PARTSUPP PS1, TPCD.SUPPLIERS S1
WHERE TPCD.PARTS.P_PARTKEY = PS1.PS_PARTKEY AND
S1.S_SUPPKEY = PS1.PS_SUPPKEY AND
S1.S_NATION = S.S_NATION)
ORDER BY S.S_NAME
10
For now, the first bit of XML tells us all these things:
-For the table Suppliers
-Use the index S_NATIONREGION
-Make Suppliers the first table accessed in the query block
Uh oh, looking at the explain of the query after that first index shows that we don’t
have start-stop keys to use on the index.
We can fix that if we do an index oring, and we still want it accessed first of course.
We’ll subdivide the IN predicate, and each leg of the index or will use start stop
keys. Wow! All with one line:
Also, here is our diamond back again, letting us know about XML. XML is great for profiles because
you’ll be able to store and query your profiles using DB2.
10
Create The Profile XML File
<?xml version="1.0" encoding="UTF-8"?>
<OPTPROFILE VERSION=‘9.0’>
Which query should
we change
<STMTPROFILE ID=‘TPCD Q1’>
<STMTKEY>SELECT S. _NAME, S.S_ADDRESS, S.S_PHONE,
S.S_COMMENT…</STMTKEY>
What should we do
to it?
<OPTGUIDELINES>
<IXSCAN TABLE='S' INDEX='S_NATIONREGION‘ FIRST='TRUE‘/>
</OPTGUIDELINES>
</STMTPROFILE>
</OPTPROFILE>
11
This is what a complete optimization profile looks like when it modifies a single
statement.
There is great power stored in the extra XML, and I’ll describe more of it in
presentation. For now lets start off with the simple common uses.
11
Insert and Use the Profile
¾ IMPORT FROM profile_file OF DEL MODIFIED BY LOBSINFILE INSERT
INTO SYSTOOLS.OPT_PROFILE
12
After you write a profile, you load it into a user-managed table in your database, and
enable the profile.
You can enable the profile for a particular bind, on the clp, in cli, sqlj, all of our
DB2 interfaces.
Once you’ve enabled the profile, re-execute the query and make sure everything
worked as you wanted. If you get a Warning 437 RC 13, that means something went
wrong. I will describe diagnostics near the end of this talk.
12
Recap: Why Are Profiles Great?
¾ Get the plan you know you need
¾ Use them without changing the application!
¾ In database
¾ Centrally managed, queriable, adapted as the database
changes
¾ Easy to enable and disable as needed
¾ Flexible
¾ Target specific queries or all queries running
¾ Change access and join, change rewrites
¾ Margarita!
13
13
Anatomy of an Optimization Profile
<?xml version="1.0" encoding="UTF-8"?>
<OPTPROFILE VERSION=‘9.0’>
Global Guidelines
<OPTGUIDELINES><QRYOPT VALUE=‘3’/></OPTGUIDELINES>
Global Guides
14
This is an animated slide that shows that an optimization profile is composed of two
major sections.
First there is an optional Global Guidelines section, where you put guidelines that
impact all queries that are run while the profile is enabled.
Next, there are zero or more statement guidelines, each of which modify a single
statement.
DB2 matches a statement key you provide in the statement guideline with incoming
queries in much the same way that the package cache maps compiled queries to
incoming statements.
14
Types of Optimization Guidelines
¾ Cost-based Guidelines (zero or more per query)
¾ Access methods
¾ Use this index for this table, index-anding, access this table first, etc.
¾ Join methods
¾ Join topology
15
15
First The Pieces, Then The Whole
16
In the next slides I will walk you through all the sections of the optimization
profiles. On each slide I will show just the small piece of XML that corresponds to
that topic. We will bring everything together near the end, but since each part of the
profile is optional, your profile doesn’t need to be much more than any one of these
fragments.
16
Cost-based Optimization Guidelines
Access Requests
¾ Applied for a specific query
¾ Specify how to access a table
¾ Correspond to DB2 access methods
¾ Table reference identified using either exposed names in the
original, or the optimized statement
17
Access guidelines are probably the most useful profiles. We have an access
guideline for each of our main access methods in DB2.
17
Index Scan Guidelines
SELECT S.S_NAME, S.S_ADDRESS, S.S_PHONE, S.S_COMMENT
Yes, you can tell us which index to use
FROM TPCD.PARTS, TPCD.SUPPLIERS S, TPCD.PARTSUPP PS
WHERE P_PARTKEY = PS.PS_PARTKEY AND S.S_SUPPKEY = PS.PS_SUPPKEY AND
(in a crunch)!
P_SIZE = 39 AND P_TYPE = ’BRASS’ AND S.S_NATION IN ('MOROCCO', 'SPAIN')
AND PS.PS_SUPPLYCOST =
Take a break… relax… when the sky is falling
(SELECT MIN(PS1.PS_SUPPLYCOST)
FROM TPCD.PARTSUPP PS1, TPCD.SUPPLIERS S1
Profiles Come Calling!
WHERE TPCD.PARTS.P_PARTKEY = PS1.PS_PARTKEY AND
S1.S_SUPPKEY = PS1.PS_SUPPKEY AND
S1.S_NATION = S.S_NATION)
ORDER BY S.S_NAME
¾ Access the PARTS table in the main subselect using the IPTKY index
<IXSCAN TABLE=’TPCD.PARTS’ INDEX=‘IPTKY’/>
And here’s the big party! It’s very easy to tell us to use a specific index, any index,
and even tell us to access a table first in the query block.
If you leave something out like an index name, DB2 will pick the best.
Your profile need not talk about the whole query. In fact we suggest you give very
few guidelines, as few as are required to nudge DB2 into the right direction.
Anything you do not talk about will be optimized as normal.
18
Index Anding Guidelines
SELECT S.S_NAME, S.S_ADDRESS, S.S_PHONE, S.S_COMMENT
FROM TPCD.PARTS, TPCD.SUPPLIERS S, TPCD.PARTSUPP PS
WHERE P_PARTKEY = PS.PS_PARTKEY AND S.S_SUPPKEY = PS.PS_SUPPKEY AND
P_SIZE = 39 AND P_TYPE = ’BRASS’ AND S.S_NATION IN ('MOROCCO', 'SPAIN')
AND PS.PS_SUPPLYCOST =
(SELECT MIN(PS1.PS_SUPPLYCOST)
FROM TPCD.PARTSUPP PS1, TPCD.SUPPLIERS S1
WHERE TPCD.PARTS.P_PARTKEY = PS1.PS_PARTKEY AND
S1.S_SUPPKEY = PS1.PS_SUPPKEY AND
S1.S_NATION = S.S_NATION)
ORDER BY S.S_NAME
We need to use the table name PS1 here because it is the name you’ve exposed for
that table. The last slides describe the advanced topic of creating table references in
complex situations such as views.
In general, use the exposed name if you’ve created one, else use the table name.
19
List Prefetch Guidelines
¾ Access the PARTS table in the main subselect using a list prefetch on
the IPTKY index
The generic access guideline is very useful when building join queries.
20
Cost-based Optimization Guidelines
Join Requests
¾ Specify a join method and join order
¾ Correspond to DB2 join methods
¾ Composed of other access or join requests
21
Like access requests, we have a join guideline for each of our main join methods in
DB2.
21
Nested Loop Join Guidelines
SELECT S.S_NAME, S.S_ADDRESS, S.S_PHONE, S.S_COMMENT
FROM TPCD.PARTS, TPCD.SUPPLIERS S, TPCD.PARTSUPP PS
WHERE P_PARTKEY = PS.PS_PARTKEY AND S.S_SUPPKEY = PS.PS_SUPPKEY AND
P_SIZE = 39 AND P_TYPE = ’BRASS’ AND
S.S_NATION IN ('MOROCCO', 'SPAIN') AND
PS.PS_SUPPLYCOST = (SELECT MIN(PS1.PS_SUPPLYCOST)
FROM TPCD.PARTSUPP PS1, TPCD.SUPPLIERS S1
WHERE TPCD.PARTS.P_PARTKEY = PS1.PS_PARTKEY AND
S1.S_SUPPKEY = PS1.PS_SUPPKEY AND
S1.S_NATION = S.S_NATION)
ORDER BY S.S_NAME
¾ Join the PARTS and PARTSUPP tables in the main subselect using a
nested loop join
¾ Access the tables using the best index
22
Reversing the join order is great. In this case we specified that index accesses be
used for the legs of the join, but we could have used an ACCESS guideline to give
DB2 full freedom.
22
Hash Join Guidelines
SELECT S.S_NAME, S.S_ADDRESS, S.S_PHONE, S.S_COMMENT
FROM TPCD.PARTS, TPCD.SUPPLIERS S, TPCD.PARTSUPP PS
WHERE P_PARTKEY = PS.PS_PARTKEY AND S.S_SUPPKEY = PS.PS_SUPPKEY AND
P_SIZE = 39 AND P_TYPE = ’BRASS’ AND
S.S_NATION IN ('MOROCCO', 'SPAIN') AND
PS.PS_SUPPLYCOST = (SELECT MIN(PS1.PS_SUPPLYCOST)
FROM TPCD.PARTSUPP PS1, TPCD.SUPPLIERS S1
WHERE TPCD.PARTS.P_PARTKEY = PS1.PS_PARTKEY AND
S1.S_SUPPKEY = PS1.PS_SUPPKEY AND
S1.S_NATION = S.S_NATION)
ORDER BY S.S_NAME
23
You may not always be able to get a hash join. For example, your query
optimization class may disallow has joins, you may not have identical types on the
sides of your predicate, etc. You can read more about hash join in the DB2
performance guides.
23
Merge Join Guidelines
SELECT S.S_NAME, S.S_ADDRESS, S.S_PHONE, S.S_COMMENT
FROM TPCD.PARTS, TPCD.SUPPLIERS S, TPCD.PARTSUPP PS
WHERE P_PARTKEY = PS.PS_PARTKEY AND S.S_SUPPKEY = PS.PS_SUPPKEY AND
P_SIZE = 39 AND P_TYPE = ’BRASS’ AND
S.S_NATION IN ('MOROCCO', 'SPAIN') AND
PS.PS_SUPPLYCOST = (SELECT MIN(PS1.PS_SUPPLYCOST)
FROM TPCD.PARTSUPP PS1, TPCD.SUPPLIERS S1
WHERE TPCD.PARTS.P_PARTKEY = PS1.PS_PARTKEY AND
S1.S_SUPPKEY = PS1.PS_SUPPKEY AND
S1.S_NATION = S.S_NATION)
ORDER BY S.S_NAME
¾ Join the PARTS and PARTSUPP tables in the main subselect using a
nested loop join
¾ Join that result with the SUPPLIERS table using a merge join
¾ Choose the best index for all table accesses
LOOKIN’ GOOD!
<MSJOIN>
<NLJOIN><IXSCAN TABLE=‘TPCD.PARTS’/><IXSCAN TABLE=‘PS’/></NLJOIN>
<IXSCAN TABLE=‘S’/>
</MSJOIN>
24
Even complex graphs can be expressed very naturally in our XML syntax.
Joins contain other accesses and joins.
24
Types of Optimization Guidelines
¾ Cost-based Guidelines
¾ Access methods
¾ Use this index for this table, index-anding, access this table first, etc.
¾ Join methods
¾ Join topology
¾ Global Guidelines
¾ Used to set Global optimization parameters
¾ Enable/disable query reoptimization, query opt level, degree
25
25
Query Rewrite Guidelines
¾ Enable or disable rewrite rules applied to a specific query
26
Query rewrite guidelines help you alter the rewrite rules that transform your query.
These can have a tremendous impact on your query, sometimes making other
profiles inapplicable.
26
In-List to Join
SELECT S.S_NAME, S.S_ADDRESS, S.S_PHONE, S.S_COMMENT
FROM TPCD.PARTS, TPCD.SUPPLIERS S, TPCD.PARTSUPP PS
WHERE P_PARTKEY = PS.PS_PARTKEY AND S.S_SUPPKEY = PS.PS_SUPPKEY AND
P_SIZE IN (39,40,45,48) AND P_TYPE = ’BRASS’ AND
S.S_NATION IN ('MOROCCO', 'SPAIN') AND
PS.PS_SUPPLYCOST = (SELECT MIN(PS1.PS_SUPPLYCOST)
FROM TPCD.PARTSUPP PS1, TPCD.SUPPLIERS S1
WHERE TPCD.PARTS.P_PARTKEY = PS1.PS_PARTKEY AND
S1.S_SUPPKEY = PS1.PS_SUPPKEY AND
S1.S_NATION = S.S_NATION)
ORDER BY S.S_NAME
¾ Transform the list of constants in the predicate P_SIZE (39, 40, 45, 48)
to a table expression
<INLIST2JOIN TABLE=‘P’/>
In-list to join leaves the in-list as a predicate or transforms it to a join with a table
expression. It can take a COLUMN attribute to target it to a specific predicate.
27
In-List to Join - Disable
SELECT S.S_NAME, S.S_ADDRESS, S.S_PHONE, S.S_COMMENT
FROM TPCD.PARTS, TPCD.SUPPLIERS S, TPCD.PARTSUPP PS
WHERE P_PARTKEY = PS.PS_PARTKEY AND S.S_SUPPKEY = PS.PS_SUPPKEY AND
P_SIZE IN (39,40,45,48) AND P_TYPE = ’BRASS’ AND
S.S_NATION IN ('MOROCCO', 'SPAIN') AND
PS.PS_SUPPLYCOST = (SELECT MIN(PS1.PS_SUPPLYCOST)
FROM TPCD.PARTSUPP PS1, TPCD.SUPPLIERS S1
WHERE TPCD.PARTS.P_PARTKEY = PS1.PS_PARTKEY AND
S1.S_SUPPKEY = PS1.PS_SUPPKEY AND
S1.S_NATION = S.S_NATION)
ORDER BY S.S_NAME
28
Each of the rewrite guidelines can take an optional OPTION attribute, which allows
you to DISABLE that transformation. ENABLE is the default if no OPTION is
specified.
28
Subquery to Join
<SUBQ2JOIN />
<SUBQ2JOIN OPTION=‘DISABLE’/>
29
Due to space limitations, I don’t give the details for Not-Exists to anti-join and Not-
In to anti-join. They are similar to subquery to join and can be found in our
documentation.
29
Rewrite and Global Guidelines
¾ When found at the statement level, applied only to the
matched statement
¾ Overrides the values at the Global level, special register,
bind option, etc. Global Guidelines
So far I’ve shown you guidelines that only apply to individual statements, in the
statement guidelines.
These next few can be placed in a statement specific guideline, OR in the Global
guidelines section to apply to all queries that are executed while the profile is active
(all queries in a package if you bind with a profile, for example). I describe
precedence rules later in the talk.
30
REOPT
¾ Overrides the REOPT bind
¾ REOPT NONE
¾ No reoptimization of the query is done, optimizer chooses default
values.
<REOPT VALUE=‘NONE’ />
¾ REOPT ONCE
¾ The execution plan is picked at the first OPEN for a query. Useful if
initial values are representative of following executions of the query.
<REOPT VALUE=‘ONCE’ />
¾ REOPT ALWAYS
¾ Reoptimize the query on every execution. Chosen plans are optimal,
at cost of compilation time and package cache activity.
<REOPT VALUE=‘ALWAYS’ />
31
REOPT is great for queries with parameter markers/host variables. Look into our
documentation to find our more.
31
Query Optimization
¾ Overrides the Query Optimization class (dft_queryopt)
¾ You can set the query optimization class PER QUERY using profiles
¾ QRYOPT 0
¾ Minimal query optimization
¾ QRYOPT 1
¾ QRYOPT 3
¾ QRYOPT 5
¾ Default. Significant optimization with limiting heuristics
¾ QRYOPT 7
¾ QRYOPT 9
¾ Maximal query optimization
<QRYOPT VALUE=’5’/>
32
32
Default Degree
¾ Overrides the Current Degree (dft_degree)
33
Please refer to the infocenter documentation to learn more about the degree
parameter.
33
Types of Optimization Guidelines
¾ Cost-based Guidelines
¾ Access methods
¾ Use this index for this table, index-anding, access this table first, etc.
¾ Join methods
¾ Join topology
¾ Global Guidelines
¾ Used to set Global optimization parameters
¾ Enable/disable query reoptimization, query opt level, degree
34
We will now look into global guidelines that impact all queries that are executed
while the profile is enabled.
34
Global Guidelines
¾ Up to now, all guidelines impacted a specific statement
¾ Global guidelines are applied to all queries issued while
profile is in effect Global Guidelines
35
MQT Optimization Choices Guideline
¾ Enable or disable the use of MQTs in your query
¾ Provide a list of MQTs considered for the query
¾ Useful if you experience compile time problems with MQTs
¾ Using the MQT is still a cost based decision, this adds/removes them
from the pool
36
MQT Optimization choices can conflict (you can disable and then also list some
MQTs). In these situations we try to take the most restrictive directive (disable) but
in general this is not supported and you should not write such guidelines.
36
Statement Key, Statement What?
<?xml version="1.0" encoding="UTF-8"?>
<OPTPROFILE VERSION=‘9.0’>
<OPTGUIDELINES><QRYOPT VALUE=‘3’/></OPTGUIDELINES>
<OPTGUIDELINES>
<IXSCAN TABLE=’TPCD.PARTS’ INDEX=‘IPTKY’/>
<INLIST2JOIN TABLE=‘TPCD.PARTS’ COLUMN=‘P_SIZE’ OPTION=‘DISABLE’/>
</OPTGUIDELINES>
</STMTPROFILE>
</OPTPROFILE>
37
37
Statement Key
¾ Each statement level profile must identify a corresponding query
using a statement key
38
Analogy is to the plan cache, we make sure we get the right statement matched up.
If you don’t provide a value, then we do not use it as a criteria for matching. If you
provide a value for schema or function path, then it must match the incoming
statement’s schema or function path exactly.
38
Statement Key
<STMTPROFILE ID=‘TPCD SIMPLE QUERY’>
<STMTKEY SCHEMA=‘TPCD’
FUNCTIONPATH=‘SYSIBM,SYSFUN,SYSPROC,SYSIBMADM,TE’>
<![CDATA[SELECT C_NAME FROM CUSTOMERS]]></STMTKEY>
<OPTGUIDELINES>
<IXSCAN TABLE=’TPCD.PARTS’ INDEX=‘IPTKY’/>
<INLIST2JOIN TABLE=‘TPCD.PARTS’ COLUMN=‘P_SIZE’ OPTION=‘DISABLE’/>
</OPTGUIDELINES>
</STMTPROFILE>
39
39
Create the Profile Tables
¾ Profile Table DDL
CREATE TABLE SYSTOOLS.OPT_PROFILE (
SCHEMA VARCHAR(128) NOT NULL,
NAME VARCHAR(128) NOT NULL,
PROFILE BLOB (2M) NOT NULL,
PRIMARY KEY ( SCHEMA, NAME ) );
40
DB2 documentation contains provides all DDL I use in the presentation. This is
table is not created automatically, as most users do not use profiles.
40
Insert Into the Profile Table
¾ Any way you want to load it is fine!
¾ I will show you how to use the IMPORT utility
There are so many ways of inserting a LOB. My favorite is with a table function, but
I chose to describe IMPORT here because it works from client to server.
41
Enabling DB2 for Profiles
¾ Enable the DB2_OPTPROFILE registry variable
¾ db2set DB2_OPTPROFILE=YES
¾ db2set DB2_OPTPROFILE=NO
42
Often times this step is forgotten with profiles. DB2 will complain about the syntax
of your other profile commands unless you tell it that profiles are enabled.
42
Enabling a Specific Profile
¾ You may have many profiles in your systools.opt_profile table
¾ One profile enabled at a time
¾ Special register
¾ SET CURRENT OPTIMIZATION PROFILE SCHEMA.NAME
¾ Sets the profile for dynamic queries following it
¾ “String”, Host Variable, or NULL (no profile in use, overrides bind
option)
When you bind an application with a profile, it is the profile for all static queries.
Also, if you have dynamic queries in your application, it is the default for those.
You can specify a profile in the application using the registry variable, and that will
change the default for following dynamic queries.
All DB2 interfaces can either explicitly support profile options, such as in the cli.ini
file, or will pass through profile settings as needed.
43
How Can Things Go Wrong?
¾ Reference a nonexistent table or index
¾ Syntax error in your guideline
44
44
W 437 RC 13 example
45
This is an example of a profile gone wrong on the DB2 CLP. Note the 437 warning
still allows results to be returned. Profiles will always be associated with reason
code 13. Please check the infocenter for other reason codes associated with
SQL0437.
45
Warning, Now What?
¾ Explain the query with the warning
¾ Human readable details in a new section!
46
46
Flushing?
¾ Once used, profiles are stored in an internal cache
¾ When updating or deleting a profile, the profile must be flushed
from the cache
47
Quick tip for note readers, don’t worry about flushing! I have a trigger that does it
all for you in the next slide. It’s also in our documentation.
47
Flushing the Easy Way
CREATE PROCEDURE SYSTOOLS.OPT_FLUSH_CACHE( IN SCHEMA VARCHAR(128),
IN NAME VARCHAR(128) )
LANGUAGE SQL
MODIFIES SQL DATA
BEGIN ATOMIC
-- FLUSH stmt (33) + quoted schema (130) + dot (1) + quoted name (130) = 294
DECLARE FSTMT VARCHAR(294) DEFAULT 'FLUSH OPTIMIZATION PROFILE CACHE '; --
-- Setup error handler to ignore error in case DB2_OPTPROFILE is not set
DECLARE CONTINUE HANDLER FOR SQLSTATE VALUE '42601' BEGIN END; --
IF NAME IS NOT NULL THEN
IF SCHEMA IS NOT NULL THEN
SET FSTMT = FSTMT || '"' || SCHEMA || '".'; --
END IF; --
48
Here it is! Using this trigger you can forget there is any such thing as flushing the
profile cache.
48
¾ Opt guidelines can refer to tables, views, alias’s, table expressions
using either
¾ their exposed names in the original statement (TABLE attribute)
¾ their corresponding unique correlation names in the optimized
statement (TABID attribute)
Optimized statement:
49
References Through Views
¾ Can use a sequence of exposed names to qualify references to
tables in views
CREATE VIEW “Hamid".V1 as (SELECT * FROM EMPLOYEE WHERE SALARY > 50,000)
CREATE VIEW “Laura".V2 AS (SELECT * FROM "Rick".V1 WHERE DEPTNO IN (‘52’, ‘53’,’54’)
SELECT *
FROM “Laura".V2 A
WHERE SALARY > (SELECT AVG(SALARY) FROM EMPLOYEE)
<OPTGUIDELINES><IXSCAN TABLE='A/“Hamid".V1/EMPLOYEE'/></OPTGUIDELINES>
CREATE VIEW "Gustavo".V2 AS (SELECT * FROM "Rick".V1 WHERE DEPTNO IN (‘52’, ‘53’,’54’)
SELECT *
FROM "Gustavo".V2 A
WHERE SALARY > (SELECT AVG(SALARY) FROM EMPLOYEE)
50
50
Summary
51
I hope you enjoyed this presentation. For advanced topics I urge you to read our
great documentation.
Profiles are a simple way to impact your query plans. We’ve worked hard to let you
alter applications with modifying the application. Also, we’ve tried to support the
best application design by separating the profiles from the queries themselves, and
letting you manage and query the profiles as the extremely important objects they
are.
51
Session: A1
How to Influence the DB2 Query Optimizer Using Optimization Profiles
Tom Eliaz
IBM
teliaz@us.ibm.com
52
Once again I hope you enjoyed this presentation and I look forward to hearing your
feedback. Check them out in DB2 Viper for Linux, Unix, and Windows!
52