Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use DB2 Explain?

Tags:

How do I use DB2's Explain function? -- both to run it, and to use it to optimize queries. Is there a better tool available for DB2?

I've built queries before, but the only way I've had to tell how long they'd take is to run them and time them -- which is hardly ideal.

Edit: The answer for me turned out to be "You can't. You don't have and cannot get the access." Don't you love bureaucracy?

like image 826
Trevel Avatar asked Jul 08 '09 15:07

Trevel


People also ask

What are explain tables in Db2?

EXPLAIN tables contain information about SQL statements and functions that run on Db2 for z/OS®. You can create and maintain a set of EXPLAIN tables to capture and analyze information about the performance of SQL statements and functions that run on Db2 for z/OS.

Where is the output of explain stored in Db2?

Output from EXPLAIN: Db2 inserts one or more rows of data into a plan table and other existing EXPLAIN tables . For a list of all EXPLAIN tables, see EXPLAIN tables. A plan table must exist before the operation that results in EXPLAIN output.

What are Db2 commands?

The Db2 command line processor is a program that runs under z/OS UNIX System Services. You can use the Db2 command line processor to execute SQL statements, bind DBRMs that are stored in HFS files into packages, call stored procedures, and perform XML schema repository operations.


2 Answers

What you're looking for is covered by two DB2 utilities:

  1. The explain utility, which shows the optimizer's access plan and estimated cost for a specific query (based on current RUNSTATS statistics)
  2. The design advisor, which recommends structural changes to improve the performance of one or more queries

Both utilities require specialized tables to be created in the database.

I tend to use the explain utility more than the advisor, especially if I am able to change the SQL for the query to influence a better access plan. I use the db2expln command-line utility to explain different versions of a query I'm tuning and compare their costs. What's most important is that your table and index statistics are up to date when running explain or the design advisor.

like image 71
Fred Sobotka Avatar answered Oct 31 '22 22:10

Fred Sobotka


IBM offers Data Studio as a free tool built on eclipse, which among other benefits includes a GUI for running visual explain, as well as providing tuning help through a query adviser. I highly recommend using Data Studio.

It is relatively easy to set up the correct resources (the explain tables that need to be built, and the bind that need to be done) by right clicking a connected data source, and choosing

analyze and tune > configure for tuning > guided configuration.

enter image description here

To generate the explain graph - simply highlight your query, right click, and choose "Open Visual Explain":

enter image description here

To use the query advisor, choose "start tuning" instead. It will take you through a process which will generate the explain, as well as recommend any tuning opportunities it can determine.

enter image description here

like image 33
Josh Hull Avatar answered Nov 01 '22 00:11

Josh Hull