Writing and tuning queries for optimal performance

There are several ways in which you can minimize the impact of SQL statements on Db2® database performance.

You can minimize this impact by:
  • Writing SQL statements that the Db2 optimizer can more easily optimize. The Db2 optimizer might not be able to efficiently run SQL statements that contain non-equality join predicates, data type mismatches on join columns, unnecessary outer joins, and other complex search conditions.
  • Correctly configuring the Db2 database to take advantage of Db2 optimization functionality. The Db2 optimizer can select the optimal query access plan if you have accurate catalog statistics and choose the best optimization class for your workload.
  • Using the Db2 explain functionality to review potential query access plans and determine how to tune queries for best performance.
Best practices apply to general workloads, warehouse workloads, and SAP workloads.

Although there are a number of ways to deal with specific query performance issues after an application is written, good fundamental writing and tuning practices can be widely applied early on to help improve Db2 database performance.

Query performance is not a one-time consideration. You should consider it throughout the design, development, and production phases of the application development life cycle.

SQL is a very flexible language, which means that there are many ways to get the same correct result. This flexibility also means that some queries are better than others in taking advantage of the Db2 optimizer's strengths.

During query execution, the Db2 optimizer chooses a query access plan for each SQL statement. The optimizer models the execution cost of many alternative access plans and chooses the one with the minimum estimated cost. If a query contains many complex search conditions, the Db2 optimizer can rewrite the predicate in some cases, but there are some cases where it cannot.

The time to prepare or compile an SQL statement can be long for complex queries, such as those used in business intelligence (BI) applications. You can help minimize statement compilation time by correctly designing and configuring your database. This includes choosing the correct optimization class and setting other registry variables correctly.

The optimizer also requires accurate inputs to make accurate access plan decisions. This means that you need to gather accurate statistics, and potentially use advanced statistical features, such as statistical views and column group statistics.

You can use the Db2 tools, especially the Db2 explain facility, to tune queries. The Db2 compiler can capture information about the access plans and environments of static or dynamic queries. Use this captured information to understand how individual statements are run so that you can tune them and your database manager configuration to improve performance.