Tutorial: SQL Optimizer
Using Optimize SQL consists of two steps. In the first step, SQL Optimizer generates semantically equivalent alternatives with unique execution plans for your original SQL statement. In the second step, SQL Optimizer executes the alternatives to test runs each statement's performance. This provides execution times and run-time statistics that allow you to find the best SQL statement for your database environment.
Step 1: Optimize the SQL Statement
-
Select the SQL Details tab in the main window.
- Enter a SQL statement in the Alternative Details pane.
-
Click to optimize the SQL statement
-
Select a connection. Review the following for additional information:
Select Connection Description Connection
Click to select a previously created connection.
Tip: Click to open the Connection Manager to create a new connection. Connect to the Database
Select Schema and SQLID Description Schema
Click to select a schema for the connection. SQLID
Click to select your SQLID. -
Click after SQL Optimizer completes the SQL rewrite process to compare your original SQL statement with the alternatives generated.
Step 2: Test Run Alternative SQL Statements
The Test Run function provides an efficient way to benchmark alternatives generated by SQL Optimizer. You can test run selected alternatives to obtain actual execution statistics. This function does not affect network traffic since SQL Optimizer can provide these statistics without having to retrieve result sets from the database server. Additionally, data consistency is maintained when using SELECT, SELECT INTO, INSERT, DELETE, and UPDATE statements because these statements are run in a transaction that is rolled back after execution.
To test run a SQL statement alternative
-
Click .
-
Select Optimize SQL | Test Run | Test Run Settings.
-
Select Custom Settings or Predefined Settings.
-
Select Optimize SQL | Test Run | Execution Method.
-
Review the following for additional information:
Execution Method Description Maximum rows to be retrieved (records)
Select this checkbox and then enter the maximum number of rows to retrieve for a test run of all records. Run Time Retrieval Method Description Run SQL options
Select one of the following options:
- Run all SQL twice if original SQL runs faster than (seconds)—Combines the Original SQL twice and all others once and the All SQL twice options into one option and allows you to determine (by the number of seconds a SQL statement runs) which option to use. The original SQL statement always runs twice. The SQL alternatives run twice if the original SQL statement runs in less time than the value specified. Otherwise, the SQL alternatives all run once.
- Original SQL twice and all others once—Caches data from a table into memory the first time you access it. The next time you access that data, it is already in memory so the following SQL statements run faster. To provide an accurate comparison, the first SQL statement runs twice but only the time from the second run is compared to the times for the other statements.
- All SQL twice—Executes all SQL statements twice to eliminate factors that can affect the accuracy of the results. If you recently executed a SQL statement, the information for that statement may be cached and the statement may execute faster. This option eliminates time variation caused by caching since it runs all SQL statements twice but only uses the second run time for comparison.
- All SQL once—Executes all SQL statements once. For long running SQL, you do not need to run any statement twice since the effect from caching diminishes over time.
-
Select Optimize SQL | Test Run | Order and Termination.
-
Select the This percentage of the original SQL run time checkbox.
-
Click .
The execution statistics display in the Alternatives pane once the test run completes. Select an alternative to see more information in the Alternative Details and Execution Plan panes.