Stored queries vs. Dynamic SQL directly in code 
Author Message
 Stored queries vs. Dynamic SQL directly in code

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?


Sat, 04 Aug 2001 03:00:00 GMT  
 Stored queries vs. Dynamic SQL directly in code
Well, this is another "it depends" situation.

A query you build "on the fly" in code must be compiled every time.  For
most queries, this doesn't take long, however.  In general, stored
queries -- especially ones that you've executed successfully before so that
Access has had a chance to compile and store a "plan" -- should run a tad
faster.  HOWEVER, if you have thousands of queries or other objects in your
database, it can take longer for Access to *find* the stored query than it
would to compile a new one!

--
John Viescas
author, "Running Microsoft Access 97"
http://www.amazon.com/exec/obidos/ISBN=1572313234/

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?



Sat, 04 Aug 2001 03:00:00 GMT  
 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?



Sat, 04 Aug 2001 03:00:00 GMT  
 Stored queries vs. Dynamic SQL directly in code
I am not an expert on the functions within Access, but I have noticed over
the years that something written once and accepted as a truism can change
very quickly and still be accepted as correct.

To my knowledge any SQL code in a form now is optimised to the same lavel as
a query (Access 8) but maybe Michael Kaplan or similar expert can tell the
real facts.

--
From Richard in Woy Woy, Australia
Specialising in POS applications and
program conversions to MS Access.

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?



Sun, 05 Aug 2001 03:00:00 GMT  
 Stored queries vs. Dynamic SQL directly in code
What you are referring to is the fact that Access 8 (97) creates
hidden saved queries when you use a SQL string for a form/report
recordsource or for a combo/list box rowsource. This does not,
however, apply to SQL strings created and used in code.

 -- Andy

Quote:

>I am not an expert on the functions within Access, but I have noticed over
>the years that something written once and accepted as a truism can change
>very quickly and still be accepted as correct.

>To my knowledge any SQL code in a form now is optimised to the same lavel as
>a query (Access 8) but maybe Michael Kaplan or similar expert can tell the
>real facts.

>--
>From Richard in Woy Woy, Australia
>Specialising in POS applications and
>program conversions to MS Access.


>>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?



Sun, 05 Aug 2001 03:00:00 GMT  
 Stored queries vs. Dynamic SQL directly in code
Not necessarily. A compiled query may run inefficiently if the data sources
have changed in size significantly. In other words, if I compile my query
using development test data on my development machine - the same query may
have a different execution plan once it actually gets out into the
production environment. The point is that you must be very careful with the
assumption which you make below.

Abbot Cooper

(remove "_NoSpam_" from address when sending)

Quote:

>The code may not run faster the first time from a stored query, as it has
not
>been compiled yet and is basically the same as dynamic SQL.  Having been
run,
>however, the stored query is compiled, and will always run faster than the
>dynamic SQL, which has to be compiled each time it runs.

>Bill Gibbs
>Systems Consultant
>MetaCorp Strategies



Tue, 28 Aug 2001 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. STORED QUERY Vs DYNAMIC SQL WARNING?????

2. VB code SQL vs DB Stored queries

3. Converting dynamic query by example SQL to RDO based SQL Stored Procedure

4. SQL query: from SELECT statement to stored procedure with dynamic PL/SQL

5. Queries : Performance of saved vs dynamic in code

6. Inline SQL vs. stored parameter query

7. Inline SQL vs. stored parameter query

8. sql vs. stored queries in jet

9. Inline SQL vs. stored parameter query

10. stored procedures vs. in-line SQL code

11. Dynamic SQL Queries, dynamic reports

12. VB Query vs SQL Query Analyzer Speed?

 

 
Powered by phpBB® Forum Software