ACC: How to Optimize Queries in MS Access v. 2.0, 95, and 97

ID: Q112112


The information in this article applies to:


SUMMARY

Moderate: Requires basic macro, coding, and interoperability skills.

This article discusses how you can optimize query performance in Microsoft Access 2.0, 7.0, and 97. The topics include the Microsoft Jet database engine's query Optimizer, query timing, analyzing performance, and design tips for improving query performance.

This article assumes that your database has local tables instead of linked (or attached) tables. If your tables are linked, this information still applies; however, there are additional issues that impact query performance on linked tables. For more information about improving performance on linked tables, please search on the following words in the Microsoft Knowledge Base:


   ODBC and Optimizing and Tables 


MORE INFORMATION

The Query Optimizer

The Microsoft Jet database engine contains several components, but the most important to queries (and the most complex) is the Optimizer. The Optimizer is "cost-based," meaning that it assigns a time cost to each query task and then chooses the least expensive list of tasks to perform that generates the desired result set. The longer a task takes to perform, the more costly or expensive it is considered to be.

To decide which query strategy to use, the Optimizer uses statistics. These statistics are based on the number of records in a table, the number of data pages in a table, the location of the table, whether or not indexes are present, how unique the indexes are, and so on. Based on these statistics, the Optimizer chooses the best internal query strategy for dealing with a particular query.

The statistics are updated whenever a query is compiled. A query is flagged as needing to be compiled when you save any changes to the query (or its underlying tables) and when the database is compacted. If a query is flagged to be compiled, the compiling and updating of statistics occurs the next time the query is run. Compiling usually takes from 1-4 seconds.

If you add a significant number of records to your database, you should open and save your queries to recompile them. For example, if you design and test a query using a small set of sample data, you should recompile the query after more records are added to the database. This ensures optimal query performance once your application is in use.

NOTE: You cannot view Jet database engine optimization schemes or specify how to optimize a query. However, you can use the Database Documenter to find out whether indexes are present and how unique an index is. For more information about the Database Documenter, search on "Database Documenter" using the Microsoft Access 97 Help Index.

NOTE: The Database Documenter is called the Database Documentor in Microsoft Access 2.0 and 95.

Query Timing

There are two significant time measurements for a Select query: time to display the first screenful of data and time to obtain the last record. If a query returns only one screenful of data, these two time measurements are the same. If a query returns many records, then these time measurements can be very different.

If the two measurements are the same when you view a Select query in Datasheet view, you see a screenful of data and a total number of records returned by the query such as, "Record 1 of N." If it is faster for the Jet database engine to display the first screenful of data then to complete the query and retrieve the last record, you see a screenful of data but no "N" in "Record 1 of N". The "N" value is blank until the query is complete or you scroll down to the last record.

This behavior is the result of the Jet database engine choosing one of two performance strategies: complete the query, and then display data; or display data, and then complete the query. You cannot control which strategy is used; however, the Jet database engine will choose whichever is most efficient.

Analyzing Performance

If you are using Microsoft Access 7.0 or Microsoft Access 97, you can use the Performance Analyzer to analyze queries in your database. Because the query performance analysis is closely tied to the Jet database engine, the Performance Analyzer will suggest adding indexes only when the indexes will actually be used by the Jet database engine to optimize the query. This means that the Performance Analyzer can provide performance tips which are more specific to your database than the general suggestions listed below in the "Tips to Improve Query Performance" section of this article.

To run the Performance Analyzer in Microsoft Access 7.0 or Microsoft Access 97, on the Tools menu click Analyze, and then click Performance.

Tips to Improve Query Performance

To improve query performance, try these tips:


REFERENCES

For more information about optimizing performance in Microsoft Access 97, search the Help Index for "optimization, system" or ask the Microsoft Access 97 Office Assistant.

For more information about query performance tips, see the "Querying Tips, Tricks, and Optimizations" document from Tech*Ed 95 (TE9511.EXE). For details about how to obtain the TE9516.EXE file, please see the following article in the Microsoft Knowledge Base:

Q129530 ACC2: Tech*Ed 95 Sessions on Query Tips and Tricks

For more information about optimizing database performance, see the "Database Optimization Techniques" document from Tech*Ed 95 (TE9516.EXE).

For details about how to obtain the TE9516.EXE file, please see the following article in the Microsoft Knowledge Base:

Q129612 ACC2: Tech*Ed 95 Sessions on Database Optimization Techniques

For more information about using indexes, please see the following article in the Microsoft Knowledge Base:

Q98793 ACC: Compound Indexes Must Restrict First Indexed Field

For more information about Rushmore technology, see the "Rushmore Query Optimization" document (RUSHMR.EXE). For details about how to obtain the RUSHR.ZIP file, please see the following article in the Microsoft Knowledge Base:

Q128388 ACC2: Rushmore Query Optimization Paper Available on MSL

Additional query words: speeding improving


Keywords          : kbusage GnlOthr 
Version           : 2.0 7.0 97
Platform          : WINDOWS 
Issue type        : kbhowto 

Last Reviewed: April 2, 1999