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.
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.
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.
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:
- Total estimated runtime
- Index creation advisor
- 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.
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:
- Test your statements, try different techniques, and evaluate the runtimes.
- Build indexes and statistics as needed and then retest and evaluate runtimes.
- 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.
LATEST COMMENTS
MC Press Online