Db2 SQL Tuning
Db2 SQL Tuning
Developers
Webinar
Tony Andrews
tandrews@themisinc.com
Twitter
Questions?
I will try my best to get to some questions towards the end of
the webinar.
2
Webinar Objectives
• Learn what makes queries, programs, and applications perform
poorly
• Learn what you can do as a developer to improve performance
• Better understand what SQL optimization is
• What to do when you see table scans in a query
• Teach developers the different types of predicates
• Learn the difference between indexable and non- indexable
predicates
• Learn why data statistics and ‘Knowing Your Data’ is so
important
• Learn the top steps to tuning a query or program
• Leave with many SQL standards and guidelines for development
3
What are some of the key areas that can cause performance
issues within applications, programs, and queries?
4
What are some of the key areas that can cause performance
issues within applications, programs, and queries?
5
Bad Coding Practice
SQL Tip
1). Take out any / all Scalar functions coded on columns in predicates.
10
What Causes a Table Scan?
• The predicate(s) may be poorly coded in a non-indexable way.
• The predicates in the query do not match any available indexes on
table.
• The table could be small, and DB2 decides a tablespace scan may
be faster than index processing.
• The catalog statistics say the table is small, or maybe there are no
statistics on the table.
• The predicates are such that DB2 thinks the query is going to
retrieve a large enough amount of rows that would require a
tablespace scan. Check the Filter Factor!
• The predicates are such that DB2 picks a non-clustered index, and
the number of pages to retrieve is high enough based on total
number of pages in the table to require a tablespace scan.
• The tablespace file or index files could physically be out of shape
and need a REORG.
11
Tuning Approach: Change the SQL
and/or Change the program Design
12
Change the SQL Example
Each of these will produce the same results, but operate very differently. Typically one
will perform better than the other depending on data distributions. For Example:
Or a 2 table join, but watch out for possible duplicates (if 1 to many relationship)
14
Tuning Approach: Explain the Query
• Any Table Scans? What’s causing it?
• Any Index Scans? What’s causing it?
• Any Partition Scans? What’s causing it?
• Which Index? Matching columns? Screening?
• Any Sorts? What’s causing it? How big is the sort?
• Any Join sorts? What other queries join to that table?
• Any subqueries? Can they be rewritten?
• Any materialization from NTE and CTE’s? Can they be
rewritten? (Not saying these are always bad…)
• Check the predicates? Stage 2 or Residual? Filter factor?
15
Update / Improve Data Statistics
16
Update / Improve Data
Statistics
Statistics in Test vs Production.
18
Physical Design
Make sure of the clustering order of data in your
tablespaces.
Tables should be physically clustered in the order that they are
typically processed by queries processing the most data.
This ensures the least amount of ‘Getpages’ when
processing.
Long running queries with ‘List Prefetch’ and ‘Sorts’ in many
join processes are good indicators that maybe a table is not
in the correct physical order.
Application queries that join to a table via the foreign key vs
the primary key is a good indicator.
Too many ‘Getpages’ vs rows returned
19
Change the Physical Design ?
EMP table clustered by EMPNO
000010 HAAS …… A00 000100 SPENSER …… E21
000020 THOMPSON …… B01 000110 LUCHESI …… A00
000030 KWAN ………. C01 000120 O’CONNELL ..…. A00
000050 GEYER …… E01 000130 QUINTANA …… C01
000060 STERN …… D11 000140 NICHOLLS …… C01
000070 PULASKI …… D21 000150 ADAMSON …… D11
000090 HENDERSON ……. E11 000160 PIANKA …….. D11
It Depends…..
1-20
Change the Physical Design ?
EMP table clustered by EMPNO
000010 HAAS …… A00 000100 SPENSER …… E21
000020 THOMPSON …… B01 000110 LUCHESI …… A00
000030 KWAN ………. C01 000120 O’CONNELL ..…. A00
000050 GEYER …… E01 000130 QUINTANA …… C01
000060 STERN …… D11 000140 NICHOLLS …… C01
000070 PULASKI …… D21 000150 ADAMSON …… D11
000090 HENDERSON ……. E11 000160 PIANKA …….. D11
1-21
Thank you for allowing me to share some of my
experience and knowledge today!
Tony Andrews
tandrews@themisinc.com
22
The material in this presentation is further developed
in the following Themis courses:
24
Education. Check out
www.db2sqltuningtips.com
www.ibmpress.com
www.amazon.com
Finally! A book of DB2 SQL tuning tips for
developers, specifically designed to improve
performance.
As of DB2 V10.
Education. Check Out
www.themisinc.com