Tuning SQL statements using the explain facility
The explain facility is used to display the query access plan that was chosen by the query optimizer to run an SQL statement.
It contains extensive details about the relational operations used to run the SQL statement, such as the plan operators, their arguments, order of execution, and costs. Because the query access plan is one of the most critical factors in query performance, it is important to understand explain facility output when diagnosing query performance problems.
- Understand why application performance has changed
- Evaluate performance tuning efforts
Analyzing performance changes
before and afterexplain information:
- Capture explain information for the query before you make any changes, and save the resulting explain tables. Alternatively, you can save output from the db2exfmt utility. However, having explain information in the explain tables makes it easy to query them with SQL, and facilitates more sophisticated analysis. As well, it provides all of the obvious maintenance benefits of having data in a relational DBMS. The db2exfmt tool can be run at any time.
- Save or print the current catalog statistics. You can also use the db2look command to help perform this task. In Db2® Version 9.7, you can collect an explain snapshot when the explain tables are populated. The explain snapshot contains all of the relevant statistics at the time that the statement is explained. The db2exfmt utility will automatically format the statistics that are contained in the snapshot. This is especially important when using automatic or real-time statistics collection, because the statistics used for query optimization might not yet be in the system catalog tables, or they might have changed between the time that the statement was explained and when the statistics were retrieved from the system catalog.
- Save or print the data definition language (DDL) statements, including those for CREATE TABLE, CREATE VIEW, CREATE INDEX, and CREATE TABLESPACE. The db2look command will also perform this task.
- Reorganize the index
- Collect new statistics for your table and indexes
- Collect explain information when rebinding your query
Evaluating performance tuning efforts
You can take a number of actions to help improve query performance, such as adjusting configuration parameters, adding containers, or collecting fresh catalog statistics.
After you make a change in any of these areas, you can use the explain facility to determine the affect, if any, that the change has had on the chosen access plan. For example, if you add an index or materialized query table (MQT) based on index guidelines, the explain data can help you to determine whether the index or materialized query table is actually being used as expected.
Although the explain output provides information that allows you to determine the access plan that was chosen and its relative cost, the only way to accurately measure the performance improvement for a query is to use benchmark testing techniques.