Previous | Table of Contents | Next |
Although this part of the chapter is directed at tuning SQL statements associated with new applications, it may be appropriate for you to make these changes to existing applications if you have the flexibility to do so. The reason these guidelines are separate is because many of them involve not only tuning the SQL statements and application, but changing the database schema as well.
In the design stage, it is important to plan the application and the database design together. By properly designing the application to take advantage of the design and features of the database, you can take optimal advantage of both of them. At the same time, the database should be designed to function properly with the application that uses it. The design of the database should reflect the purpose of the application. The following sections look at some of the optimizations that are possible.
One of the most powerful performance enhancing features available in an RDBMS is the index. The index can greatly reduce the number of I/Os necessary to retrieve the requested data and thus greatly improve performance. It is necessary, however, to coordinate the use of the index between the database designer and the application designer. By not carefully coordinating, the two indexes may be bypassed when a small change in SQL coding may be able to take advantage of the index. Remember that the more indexes on a table, the more overhead is incurred during updates, inserts, and deletes. It is important to index selectively.
What Tables Should Be Indexed?
Use the following guidelines to decide which tables to index:
Once you decide to use an index, you must then decide on which columns to put the index. You may index one or more columns, depending on the table.
What Columns Should Be Indexed?
Use the following guidelines to decide which columns to index:
Note:
Remember that some penalty is associated with performing INSERT, UPDATE, and DELETE statements on columns that are indexed. If you have a lot of those statements, an index may hurt more than help. Use SQL Trace on the SQL statements that access tables both with and without an index. Compare the results.
Composite Indexes
Composite indexes may be more effective than individual indexes in situations such as the following:
If carefully designed, composite indexes can be quite useful. As with single-column indexes, they are most effective if applications are written with the indexes in mind.
Once you create the index, periodically use SQL Trace to determine whether your queries are taking advantage of the index. It may be worth the effort to try the query with and without indexes and compare the results to see whether the index is worth the space it is using.
How to Avoid an Index
If you have an application that can take advantage of an index, but contains a few SQL statements that result in poor performance when they use an index, you can tell the optimizer to bypass the index. There are several ways this can be done:
In this manner, you can design your database to effectively use indexes and have the flexibility to avoid the index when it is not optimal to do so.
Previous | Table of Contents | Next |