473,396 Members | 1,938 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,396 developers and data experts.

How to Optimize Queries and Recordsets

ADezii
8,834 Expert 8TB
  1. Create Indexes on all Columns used in 'ad hoc' Query Joins, restrictions, and sorts (Jet already creates Indexes for Enforced Relationships).
  2. Use Primary Keys instead of Unique Indexes wherever possible.
  3. Use Unique Indexes instead of Non-Unique Indexes whenever possible.
  4. Include as few Columns as possible in the result set.
  5. Refrain from using Expressions, such as IIf(), in Queries.
  6. Use Count(*) instead of Count([column]).
  7. Use the Between Operator in restriction clauses rather than open-ended >, >=, <, <= restrictions (Between 35 And 50 rather than >=35).
  8. Normalize your Tables.
  9. Avoid using Outer Joins if possible since a complete scan of the outer Table is required.
  10. For nontrivial Queries, use saved Queries instead of SQL since they are already optimized.
  11. If you create a Recordset based on an SQL string using ADO, specify the adOpenDynamic Cursor Type value. Because Jet doesn't support this Cursor, it will handle this specifically, thus providing the best performance for SQL strings.
  12. When you create an ADO Recordset retrieving Jet data, do not specify adUseClient for the Recordset's Cursor Location.
  13. Manually recompile Queries when the size of Tables, or the presence or type of Indexes, has changed.
  14. When possible, use Action Queries instead of looping through Recordsets in VBA to Update or Delete batches of data.
  15. When you need to use a DAO Snapshot Recordset, or a Static Recordset using ADO, use a Forward Scrolling Snapshot, or a Forward Only Recordset using ADO.
  16. When you only wish to add new Rows to a Recordset, open the Recordset using the dbAppendOnly option.
  17. When creating Queries using Client/Server sources, consider using Pass-Through Queries.
  18. When running very large Action Queries, set the Use Transaction property of the Query to False.
  19. If you are going to add many single Rows using DAO or ADO code, try and open the Recordset using dbOpenTable or its ADO equivalent, adCmdTableDirect.
  20. It is usually faster to Update data in a Non-Indexed Column. You may want to drop the Indexes, perform the Updates, and then re-add them.
  21. Don't over-Index, but Index Fields that will be used for sorting, searching, or joining.
  22. Do not Index Columns that contain highly duplicated information.
Apr 7 '07 #1
2 17902
Denburt
1,356 Expert 1GB
Very interesting article, I do have a question or two for clarification and I would also like to add a thing or two (Hope you don't mind).

5. Refrain from using Expressions, such as IIf(), in Queries.

I do understand that using Functions and conditional statements do not process well in an underlying query or even sub queries however if it is in the direct query that is used as a recordset or recordsource would there be much of a time difference or other effect verses using it in the form or report itself?

6. Use Count(*) instead of Count([column]).

Interesting point I thought should be expanded on. :

Originally Posted by
• Count(*) counts all rows returned.
• Count([Column Name]) counts all rows where [Column Name] is not NULL.
10. For nontrivial Queries, use saved Queries instead of SQL since they are already optimized.

By this I am sure that you are referring to using Saved Queries versus SQL strings in combo Boxes, Form and Report Recordsources etc.

13. Manually recompile Queries when the size of Tables, or the presence or type of Indexes, has changed.

Just wow (missed this one somewhere along the way) I will be adding a subroutine to do this right away. If all goes well i may post it in the articles section shortly.

Information about query performance in an Access database
For 2000 - 2002 - 2003 A short snippet of the article:

After you compact your database, run each query to compile the query so that each query will now have the updated table statistics.
The article also stated that there are many reason to recompile the queries such as adding a number of records etc. a very interesting read.


Found this in a MS Access 97 article for Optimization.
A short snippet of the article:
Compile your saved queries before delivering your application. Do this by opening them in datasheet view and immediately closing them. Doing so will save the query plan with the query. This plan tells Jet how to process the query in the most efficient way. If you can, avoid using SQL strings in module code that are constructed and run on the fly. When these are run, they have to be compiled. Also be aware that, over time, the plan saved with the query may become obsolete as the data in the MDB file changes. You may want to recompile queries after compacting your database.
Apr 9 '07 #2
ADezii
8,834 Expert 8TB
Very interesting article, I do have a question or two for clarification and I would also like to add a thing or two (Hope you don't mind).

5. Refrain from using Expressions, such as IIf(), in Queries.

I do understand that using Functions and conditional statements do not process well in an underlying query or even sub queries however if it is in the direct query that is used as a recordset or recordsource would there be much of a time difference or other effect verses using it in the form or report itself?

6. Use Count(*) instead of Count([column]).

Interesting point I thought should be expanded on. :



10. For nontrivial Queries, use saved Queries instead of SQL since they are already optimized.

By this I am sure that you are referring to using Saved Queries versus SQL strings in combo Boxes, Form and Report Recordsources etc.

13. Manually recompile Queries when the size of Tables, or the presence or type of Indexes, has changed.

Just wow (missed this one somewhere along the way) I will be adding a subroutine to do this right away. If all goes well i may post it in the articles section shortly.

Information about query performance in an Access database
For 2000 - 2002 - 2003 A short snippet of the article:


The article also stated that there are many reason to recompile the queries such as adding a number of records etc. a very interesting read.


Found this in a MS Access 97 article for Optimization.
A short snippet of the article:
Don't mind the questions at all:

05. Using Expressions in Queries may be detrimental to the speed of your Queries. Because Jet cannot use an Index in any way with a Calculated Column, you may be causing sequential scans of your data by including an Expression in a Query. Even if a Row is excluded from output because of a restriction you placed in the Query, all Expressions for that Row are still evaluated. If you have a Table containing 1,000 Rows and you've only requested 1 Row back, if your Query includes an Expression and you're filtering based on that Expression, Jet must calculate the Expression for all 1,000 Rows.

06. Jet has built in optimizations that make Count(*) much faster than column-based counts.

10. Access creates hidden QueryDefs for SQL statements it finds in RecordSource and RowSource properties of Forms and Reports, but it won't create QueryDefs for SQL statements that are embedded in VBA code. Here is where the problem lies.

13. Manually recompile Queries when the size of Tables, or the presence or type of Indexes, has changed. The reasoning behind this should be fairly obvious.

I hope all your questions have been answered, if not let me know and I'll do my best to attempt a better response.
Apr 9 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Gary | last post by:
I'm using ASP (VB Script) to generate some reports from a SQL Server database. These queries often take a significant amount of time to complete, and many of these reports consist of multiple...
6
by: Andreas Lauffer | last post by:
I changed from Access97 to AccessXP and I have immense performance problems. Details: - Access XP MDB with Jet 4.0 ( no ADP-Project ) - Linked Tables to SQL-Server 2000 over ODBC I used...
9
by: Dom Boyce | last post by:
Hi First up, I am using MS Access 2002. I have a database which records analyst rating changes for a list of companies on a daily basis. Unfortunately, the database has been set up (by my...
2
by: vulcaned | last post by:
I'm thinking I might want to move the back-end to one of my Access97 applications to SQLServer instead of continuing to use Access jet but before I start/do that I have several questions I'm hoping...
7
by: Zlatko Matić | last post by:
Let's assume that we have a database on some SQL server (let it be MS SQL Server) and that we want to execute some parameterized query as a pass.through query. How can we pass parameters to the...
20
by: Darryl Kerkeslager | last post by:
I use the following code to retrieve to recordsets into two local tables, from an mdb file over a WAN. Works fine, but I'm trying to tweak the speed, and I have some questions: 1. I tried both...
14
by: google | last post by:
I am creating a new database for use within our company, that I'd like to make reasonably secure (short of a true server based solution). The back-end of a non-server based database seems to be...
13
by: Frank Swarbrick | last post by:
IBM has a product for the VSE operating system called the VSAM Redirector. It allows you to use VSAM to access RDBMS tables/views as if they were actual VSAM files. We're doing a comparison right...
2
by: zwasdl | last post by:
Some of my queries take 1 hour to run, can I run other queries in access while waiting on the first query? If so, how can I do it? Thanks a lot, Wei
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.