Previous | Table of Contents | Next |
WHERE Clause Tips
While most of these tips are generalizations , each of them has been proven to be effective much of time. In general, it s best to write your code according to these tips (making necessary allowances to meet your required functionality) and then differentiate from the tips as necessary to improve performance. These tips all apply to the WHERE clause for your SELECT , DELETE , and UPDATE statements:
Remember, each of these tips is a generalization based on many individual statements. Successful performance tuning is the result of many hours of tedious work to wring out every bit of performance; these tips will only start you along that road.
There are some additional tuning tips that apply when you re using Oracle s rule-based optimizer, which attempts to execute every SQL statement using the same method.
Rule-Based Optimizer
The tips outlined in this section are relevant only when using the rule-based optimizer. Most Oracle installations now predominantly use the cost-based optimizer, but use of the rule-based optimizer is still far from uncommon.
There are two primary conditions that you should be aware of when using the rule-based optimizer in your queries.
If it seems like using the rule-based optimizer requires more work, that s because it does. Using the rule-based optimizer requires you to be much more conscious of the conditions that exist in your data and the indexes in place on your tables.
While tuning SQL statements is the most common type of performance tuning, there are some general guidelines that can significantly improve the performance of your PL/SQL code as well.
There s very little call for tuning a properly designed block of PL/SQL. When performance tuning is necessary, most (if not all) of the work goes into tweaking performance improvements out of individual SQL statements. Still, there are several important design considerations for PL/SQL blocks that can have a significant impact on performance, most notably in the areas of using cursors and exception handling. The use of cursors allows you to significantly reduce the overhead required by your SELECT statements.
In PL/SQL terms, a cursor is best described as a defined SELECT statement that can be referenced in your code as a variable. Most PL/SQL blocks contain at least one SELECT statement. This statement is often included inside the body of the PL/SQL block, as shown in Listing 10.3.
Listing 10.3 A SELECT statement inside the body of a PL/SQL block.
DECLARE vLastName varchar2 (20); BEGIN SELECT last_name INTO vLastName FROM STUDENTS WHERE ssn = '999999999'; END;
This query returns a single row (at least, we re assuming that there is one distinct social security number per student). While there s nothing wrong with the SELECT statement itself, there is a performance problem associated with its use inside the PL/SQL block.
Oracle executes two fetches to return this single row of data. The first fetch returns the row of data returned by the query. The second fetch is performed to make sure that there are no more rows that satisfy the conditions of the query. Any SELECT statement inside a PL/SQL block will always perform an extra fetch for this purpose.
This extra fetch can be avoided if the SELECT statement is implemented by using a cursor, as shown in Listing 10.4.
Previous | Table of Contents | Next |