Escolar Documentos
Profissional Documentos
Cultura Documentos
By Joshua Steffan
Contents
Executive Summary................................................................................................................................. 3
Introduction: Why SQL Tuning?............................................................................................................. 3
Structured Query Language (SQL)......................................................................................................... 4
Overview of the SQL Compiler ......................................................................................................... 4
Dynamic and Static SQL ................................................................................................................... 5
Tuning SQL for Application Performance.............................................................................................. 5
SQL Statement ................................................................................................................................... 5
Objects ............................................................................................................................................... 6
Tuning Environment .......................................................................................................................... 6
Native DB2 UDB utilities ....................................................................................................................... 6
IBM Command Center....................................................................................................................... 7
IBM Control Center ........................................................................................................................... 7
SQL Tuning Component ......................................................................................................................... 7
Technical Details ............................................................................................................................... 7
SQL Statement Information ............................................................................................................... 8
Object Information............................................................................................................................. 9
Tuning Environment .......................................................................................................................... 9
Quest Central for DB2 ..................................................................................................................... 10
Summary ............................................................................................................................................... 10
Quest Software...................................................................................................................................... 11
About the Author................................................................................................................................... 11
2
Application Performance Tuning for DB2 UDB
by Joshua Steffan
Executive Summary
Today, databases are at the foundation of a business computing architecture that often includes
Web servers, application servers, client workstations, application logic, networks, and other
components. Since the database acts as the data server, it can quickly turn into an application
bottleneck if a problem occurs. In many cases, newly developed applications pass quality
assurance tests, but go on to fail in production because of scalability problems. Poorly written
Structured Query Language (SQL)—the language used by the applications to retrieve and store
data in a database—frequently causes this to happen.
Like other databases, IBM DB2 UDB is negatively affected by poor application SQL. Bad
SQL—like bad grammar—will still communicate the required message, but it does so much
less efficiently. Application SQL must be tuned in order to optimize performance and
maximize the return on an often-significant investment in business technology.
Effective SQL tuning requires users to collect and analyze specific information, including the
offending SQL code itself, SQL access plan, target object statistics, and dependent object
relationships. Using the native DB2 utilities to gather this information is time-consuming and
requires the use of several different tools, making iterative tuning a difficult process.
Quest Software’s SQL Tuning module, part of Quest Central for DB2, allows application
developers and DBA's to quickly gather information needed to make tuning decisions within an
iterative analysis session. The expert advice function assists users by recommending
corrections and rewriting a selected SQL statement based on the accepted recommendation.
SQL Tuning is one of the four components of Quest Central™ for DB2, which helps DBA's
improve availability and performance in DB2 installations. Quest’s integrated components
encourage multitasking by offering true functional integration. The improved application
performance and return on investment generated by Quest Central for DB2 directly benefits the
business and the information technology infrastructure.
5
Explain information is typically stored within the database in a dedicated set of tables. In
addition to the access plan, the explain tables are populated with other information, such as the
statement cost. The cost represents the resource expense of the request and is used to measure
tuning improvements.
Execution results are another required supporting detail, but these are much easier to collect
and interpret. Tuning a SQL statement often requires rewriting or restructuring the query. When
attempting to improve performance by restructuring, it is easy to accidentally impair the
statement’s result. The execution results of a modified statement should be compared against
the results of the original to ensure that the correct data is still being returned.
Objects
The objects involved with a SQL statement—either directly or indirectly—also weigh upon
tuning decisions, so information must be gathered about these as well. The DB2 compiler
reviews object statistics, gathered by the RUNSTATS command, to determine the best way to
optimize the request. Absent or outdated statistics can cause the SQL compiler to make poor
optimization decisions about index access, join method and data prefetching.
Object relationships and dependencies are also important to SQL tuning. For example, a trigger
that executes every time a row is inserted could cause insert statements that take an unusual
amount of time to process. Without a complete picture of the objects involved with a SQL
statement, tuning efforts may be dedicated to the wrong area.
Tuning Environment
SQL tuning is a process that unavoidably requires trial and error. Each application, SQL
statement and database is different, so what worked for one statement may not for the next.
Because of this, a comprehensive tuning environment is perhaps the most important component
required for efficient statement analysis.
This environment should correlate the SQL statement and object information for ease of access
and use. It should also support multiple scenarios, so the user can work with multiple versions
of the statement at the same time. A comparison engine is needed as well, so users can quickly
identify the most improved statement from the scenarios provided.
A tuning environment that provides the user with expert advice is especially helpful reducing
the time spent tuning each statement. This is possible because the DB2 SQL optimizer simply
operates based upon an extensive set of rules. Once the tuning environment is made "aware" of
these rules, it can then anticipate the impact of a statement and recommend corrections to
improve performance.
6
IBM Command Center
Command Center is a graphical
application that enables a user to
interactively submit SQL statements
to a selected DB2 database and
receive statement results. Command
Center also allows the user to
generate the corresponding graphical
access plan for a SQL statement
using the Visual Explain function.
While the information provided by
Command Center is useful for SQL
tuning, it is only designed for simple
query execution and does not allow
the user to work with multiple
concurrent scenarios. This makes it
Figure 1: IBM only addresses part of the SQL difficult to compare different
tuning need. versions of a SQL statement to
measure performance improvements.
IBM Control Center
Control Center is a graphical application that is used for various database administration tasks.
It can be used to collect object statistic and relationship data that complements the information
gathered with Command Center.
Unfortunately, the user is left to combine the information provided by these two independent
utilities, since they don’t provide it in a manner conducive to SQL tuning.
7
Tuning module uses the same DB2 explain facilities as the SQL compiler. Using this method
ensures that the plan details and associated costs are accurate. The component also retrieves
statistic and dependency data from the DB2 catalog views.
SQL Statement Information
The SQL Tuning component allows the user to work with SQL statements from a variety of
sources. The simplest method entails directly typing the statement into the component or
loading the contents of a SQL file; this scenario may prove most beneficial to application
developers looking to quickly tune SQL queries for use in developing applications.
Certain textual database objects like views, summary tables, triggers and packages also provide
a source of SQL for the tuner. It extracts embedded SQL statements from the text of these
object types and allows the user to tune the statement upon which the object is based.
Once a statement has been entered into the tuning session, the user is able to execute an explain
on the query to generate the statement’s access plan and other statistics. The explain statistics
provide information such as cost, I/O and CPU required to execute each step in the access plan.
The SQL Tuning component also displays optimizer arguments used for each step of the access
plan. These arguments provide insight about the optimizer’s access decisions that control
activities like direction of table access, table joins, locking and prefetch usage.
Figure 3: Quest Central's SQL Tuning module. Valuable SQL access plan and execution
details are logically presented to help speed tuning analysis.
• List View – details contents of the access path in a format similar to what is obtained by
executing the DB2EXFMT command
8
• Tree View – displays access path contents in a Windows Explorer-style tree — especially
useful for large and complicated statements
• Graph View – this access path display, which is similar to the DB2 Command Center
utility, is useful to visualize processing order
Statement execution results provide further feedback while tuning SQL statements. Verifying
query output is especially important since the goal is to tune the statement, but not at the
expense of correct results. It is best to confirm that any newly tuned statements still achieve the
same results as the original.
Object Information
When a SQL statement is explained within the SQL Tuning component, it automatically
retrieves statistics for the objects involved with the statement. The user can quickly identify
whether statistics exist for a given object, and if so, show the last time the statistics were
updated. The SQL Tuner also lets the user update statistics for objects, which is sometimes all
that is needed to improve statement performance.
Object dependencies and relationships are also presented, since identifying referential integrity
constraints and dependent objects involved with a SQL statement is particularly important.
Each relationship and dependency has an impact on the decisions made by the SQL optimizer.
Tuning Environment
The SQL Tuning component provides a robust tuning environment that lets the user work with
multiple scenarios of a base SQL statement. The scenario capability lets the user keep the target
statement in its original form with modifications placed in separate scenarios.
Figure 4: The compare engine uses CPU cost and elapsed time to identify and illustrate the
statement that will result in the most significant improvement.
Scenarios can be based on manual statement modifications or generated from the component's
tuning advice. The SQL Tuner examines every part of the statement in question and offers
9
informed tuning recommendations. With the click of a button, the component then rewrites the
SQL statement to implement the selected advice.
The SQL Tuning component maintains separate access plan and object statistics for each
scenario. The comparison engine uses cost, I/O and CPU statistics to identify and illustrate the
statement that will result in the most significant improvement.
Figure 5: Quest Central’s Performance Monitoring module complements the SQL Tuner
by identifying offensive queries.
The Database Administration module, Quest Central for DB2's object management component,
provides integration with the SQL Tuning component as well. When using Database
Administration to create or alter DB2 database objects such as views, triggers, and summary
tables, the user can invoke SQL Tuning to tune the statement that will be used by the object
under construction. The architecture of the Quest Central suite is robust enough to facilitate
integration and multitasking. Since all components are integrated with a common interface,
users can create a custom-tailored solution for their specific needs.
Summary
Quest Central for DB2's SQL Tuning component correlates all the necessary statement and
object information in a complete tuning environment that is specifically designed for analysis,
comparison and iterative tuning. SQL Tuning guides the user through the tuning process with
intelligent advice that suggests statement modifications to improve performance, and even
rewrites SQL statements to implement the selected advice.
SQL Tuning is an easy and efficient solution. Optimized application SQL creates higher-quality
applications, increasing return on investment and providing the user with improved application
response time.
10
Quest Software
Quest Software, Inc. is a leading provider of performance management solutions designed to
maintain the integrity of mission-critical business transactions and maximize the performance
of enterprise applications. Our solutions address the needs of today’s 24x7x365 businesses
where demands on the information technology infrastructure are high and tolerance for
downtime is low.
Founded in 1987, Quest Software now helps more than 100,000 users achieve the best possible
performance from their enterprise systems so the end user experience is a positive one. Based
in Irvine, California, Quest Software has offices worldwide and over 1,000 employees. For
more information, visit www.quest.com.
© 2001 Quest Software, Inc. All rights reserved. Quest Central is a trademark of Quest Software. All other brands or product names
are trademarks or registered trademarks of their respective owners.
11