
Stored queries vs. Dynamic SQL directly in code
If you use a SQL string, then the Jet database engine create a query
execution plan on the fly when the query is run. If you use a saved
query, then the query plan is generated and saved the first time the
query is run after saving it, or the first time the query is run after
compacting the database that contains the query.
Using saved queries (after that first use), you do save the very small
amount of time it takes to generate a query plan. On the other hand,
using a temporary query you get the advantage of knowing that your
query plan is based on the most up to date statistics.
With lots of data, and data that changes in size markedly, there are
going to be many times when the saved query would not run as fast as
the temp query because its query plan may not be as good as the one
generated at the moment.
I like using saved parameter queries because it is a more object
oriented approach -- you can make a change in one place to the query
rather than in many places in your code. And it results in somewhat
cleaner, more maintainable code. But there are many cases where the
flexibility of building a SQL string in code is required, and in those
cases I never worry about the tiny bit of extra time required to
generate a query plan.
This issue points out a seldom understood argument for frequently
compacting all databases -- both the front end and the back end. The
back end needs to be compacted to update the database statistics, and
the front end needs to be compacted to assure that fresh query plans
are created for all saved queries the next time they are run.
If you are interested in running query timings and tuning performance,
you should consider using the ISAMStats and QueryPlan functions, two
undocumented functions which are both discussed in the Microsoft Jet
Database Engine Programmer's Guide, from Microsoft Press.
-- Andy
-- Andy
Quote:
>All Access documentation I've ever read says that a query stored in the
>database will run faster than an SQL placed directly in code. I've tested
>this and tested this, but can find no examples of a stored query running
>faster than dynamic SQL. Matter of fact, a couple of my SQL strings ran
>faster from code than their stored counterparts. Any thoughts?