Optimize Your SQL Statements with Visual Explain PDF Print E-mail
Programming - SQL
Written by Kevin Forsythe   
Wednesday, 05 August 2009 01:00

Learn how to use this GUI tool to improve your SQL statements.

 

Writing SQL statements that function correctly and deliver the desired results is only half the battle. We also need to manage the performance of those SQL statements. This might involve things such as how we write the statements, what indexes are in use, and what SQL engine is processing the statement. The Visual Explain tool within System i Navigator helps us manage all that and more.

 

080509ForsytheFigure_1

Figure 1: Find the Run SQL Scripts tool in System i Navigator. (Click images to enlarge.) 

 

Figure 1 shows where to find the Run SQL Scripts tool within System i Navigator. It provides the easiest way to interact with Visual Explain. Launch that tool by right-clicking on the database name listed under Databases in System i Navigator, such as Myi. From the pop-up menu, select Run SQL Scripts. This tool provides an excellent interface for testing, running, and saving your SQL statements.

 

080509ForsytheFigure_2

Figure 2: Run SQL Scripts is the easy way to use Visual Explain. (Click image to enlarge.)

 

Enter SQL statements into the tool as shown in Figure 2. Remember that by default this tool uses periods (.) instead of slashes (/) between library and file names. For any statement entered in this tool, you can launch the Visual Explain shown in Figure 3 by placing the cursor on the statement, and then from the Visual Explain pull-down menu at the top of the form, select "Run and Explain." For long-running statements, you might prefer to use the "Explain" option, which skips executing the statement. However, not all statements can be explained without being run.

 

080509ForsytheFigure_3 

Figure 3: Note the two sections of Visual Explain. (Click image to enlarge.)

 

The Visual Explain tool has two sections. On the left is the graph that shows the visual representation of the execution plan for the selected SQL statement. On the right, statistical information is provided for each step. These are some of the key statistics to look for:

 

  1. Total estimated runtime
  2. Index creation advisor
  3. SQL engine

 

Obviously, the lower the runtime, the better. Consider running a statement using different strategies to see how they affect the overall performance of the statement. Indexes often provide significant improvements in performance. Click on each step in the visual graph and check for index-creation advice in the statistics, or even better, you can you use the Index and Statistics Advisor discussed below. The third and perhaps most important thing to look at is which SQL engine is processing the statement. This information is located at the bottom of the statistics for the final select.

 

There are two SQL engines on the System i. The CQE is the "Classic Engine" that has been around for many years and understands all the unique database objects on the System i, such as Select/Omit logical files. The SQE is the "SQL Engine," and it is a more-modern and faster SQL engine, but there are some things it doesn't support yet, such as Select/Omit logical files.

 

As the database processes each SQL statement, it decides which engine should be used. If the files involved in the statement have any Select/Omit logicals built over them, the database uses the CQE engine. This behavior can be altered.  Use the Options pull-down menu and select Change Query Attributes. This opens a dialog window that allows you to pick which library the QAQQINI file is read from. This file contains a variety of settings that control the behavior of the SQL engine. Set the option IGNORE_DERIVED_INDEX to *YES to indicate that Select/Omit logicals should be ignored. This allows more statements to be routed to the SQE engine, which is dramatically faster that the CQE engine in most cases.

 

Note that QUSRSYS is the default library to find the QAQQINI file for all SQL statements. Use either the dialog window mentioned above or the CHGQRYA command to temporarily override that for a given job. Test thoroughly before changing anything in QAQQINI in QUSRSYS.

 

Figure 4 shows the Index and Statistics Advisor. This tool shows what indexes the database engine recommends building to improve the performance of the given SQL statement, and it also allows you to manage collecting statistics for the database objects involved. Even if the advisor recommends creating an index, you should still weigh the value of creating extra overhead in maintaining the index versus the performance gained by SQL statements that might use it.

 

080509ForsytheFigure_4

Figure 4: The Index and Statistics Advisor recommends what indexes to build to improve performance. (Click image to enlarge.)

 

The advisor not only tells you what indexes it recommends, but if you use the Create button, it will launch a graphical wizard that assists in creating the selected index. Statistics about the database can also be collected. These statistics assist the engine in making smarter decisions about how to perform SQL statements against the related database objects.

 

There are some key things to remember:

 

  1. Test your statements, try different techniques, and evaluate the runtimes.
  2. Build indexes and statistics as needed and then retest and evaluate runtimes.
  3. Try to get all statements to run through the SQE engine if possible.

 

If you are able to do these three things, you should find significant improvement in the performance of your SQL statements.

 

 


Kevin Forsythe
About the Author:

Kevin Forsythe has worked on IBM midrange systems for 25 years. With many years of experience in programming, analysis, consulting, and instruction, he is uniquely skilled at making difficult material more easily understood. His comprehensive background includes practical application, education, and research and provides a perspective that allows him to help others steer their way through the technical maze that surrounds them. Kevin is a member of COMMON's Speaker Hall of Fame, and he currently develops applications for DriveTime. Kevin has written numerous articles on a variety of AS/400/iSeries/System i/IBM i topics.

 

MC Press books written by Kevin Forsythe available now on the MC Press Bookstore.

 

HTML for the Business Developer HTML for the Business Developer

The fastest way to develop and deploy robust, data-driven, Web-based business applications.

List Price $49.95
Now On Sale
 
IBM i5/iSeries Primer IBM i5/iSeries Primer
This comprehensive, 35-chapter book is the ultimate resource and a "must-have" for every professional working with the i5/iSeries.

List Price $99.95
Now On Sale
 
SQL for eServer i5 and iSeries

SQL for eServer i5 and iSeries


This book introduces the basics essential for any SQL developer to build a solid foundation and examines the many ways in which SQL functions on the iSeries.

List Price $64.95

Now On Sale
 
Read More >>
Last Updated on Wednesday, 05 August 2009 01:00
 
Please login to make comments.
User Rating: / 3
PoorBest 
   MC-STORE.COM