473,545 Members | 937 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Strange way of optimizing a query

I came across a situation that I've been unable to explain and was
hoping somebody had an answer:

I had written an update query which was taking about 8 seconds to run
and considered it too slow. I copied the SQL statement from the query
and tried executing it from code which then ran in 1 second. To make
sure that I didn't miss anything, I copied the SQL statement back into
a query and tried running it again. It now also only took 1 second
and looked exactly the same as before. I later realized that if I
take the original query, change it to SQL view and run it, then change
it back to design view and save it, it acomplishes the same thing.
The query looks exactly the same as before, but appears to have been
optimized somehow by running as a SQL statement first (either from
code or simply from the SQL view of the query).

Just to clear this from the discussion, I understand that a query can
run faster the second time around (I believe do to optimization), so I
made sure that when I compared them, I always ran them twice to
eliminate this from being the answer. Even after running the query
several times in its original state, it took 8 seconds. After simply
changing the view to SQL, running it, and changing it back, it always
takes 1 second thereafter.

If anybody has an answer to this, I would appreciate it.

Ryan
Nov 12 '05 #1
6 4534
When you save a query and run it, JET performs and saves an execution plan.
The execution plan is not recalculated unless the query is altered.

If the data has changed radically since the query was created, the saved
execution plan may be completely inappropriate. For example, if you create
the query with a few dozen records and later execute it with many thousands
of records, the saved plan may be completely wrong. If you then change the
SQL statement (or generate one dynamically in code), Access recalculates the
execution plan and so the query runs in a way that is much more appropriate
to the new data.

The issues regarding execution plan are quite different from the caching of
the data which speeds up the retrieval of the information from the disk on
subsequent reads.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html

"Ryan" <rp@rdoffice.ne t> wrote in message
news:cf******** *************** **@posting.goog le.com...
I came across a situation that I've been unable to explain and was
hoping somebody had an answer:

I had written an update query which was taking about 8 seconds to run
and considered it too slow. I copied the SQL statement from the query
and tried executing it from code which then ran in 1 second. To make
sure that I didn't miss anything, I copied the SQL statement back into
a query and tried running it again. It now also only took 1 second
and looked exactly the same as before. I later realized that if I
take the original query, change it to SQL view and run it, then change
it back to design view and save it, it acomplishes the same thing.
The query looks exactly the same as before, but appears to have been
optimized somehow by running as a SQL statement first (either from
code or simply from the SQL view of the query).

Just to clear this from the discussion, I understand that a query can
run faster the second time around (I believe do to optimization), so I
made sure that when I compared them, I always ran them twice to
eliminate this from being the answer. Even after running the query
several times in its original state, it took 8 seconds. After simply
changing the view to SQL, running it, and changing it back, it always
takes 1 second thereafter.

If anybody has an answer to this, I would appreciate it.

Ryan

Nov 12 '05 #2
"Allen Browne" <ab************ ***@bigpond.net .au> wrote in message news:<7P******* *************@n ews-server.bigpond. net.au>...
When you save a query and run it, JET performs and saves an execution plan.
The execution plan is not recalculated unless the query is altered.

If the data has changed radically since the query was created, the saved
execution plan may be completely inappropriate. For example, if you create
the query with a few dozen records and later execute it with many thousands
of records, the saved plan may be completely wrong. If you then change the
SQL statement (or generate one dynamically in code), Access recalculates the
execution plan and so the query runs in a way that is much more appropriate
to the new data.

The issues regarding execution plan are quite different from the caching of
the data which speeds up the retrieval of the information from the disk on
subsequent reads.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html

"Ryan" <rp@rdoffice.ne t> wrote in message
news:cf******** *************** **@posting.goog le.com...
I came across a situation that I've been unable to explain and was
hoping somebody had an answer:

I had written an update query which was taking about 8 seconds to run
and considered it too slow. I copied the SQL statement from the query
and tried executing it from code which then ran in 1 second. To make
sure that I didn't miss anything, I copied the SQL statement back into
a query and tried running it again. It now also only took 1 second
and looked exactly the same as before. I later realized that if I
take the original query, change it to SQL view and run it, then change
it back to design view and save it, it acomplishes the same thing.
The query looks exactly the same as before, but appears to have been
optimized somehow by running as a SQL statement first (either from
code or simply from the SQL view of the query).

Just to clear this from the discussion, I understand that a query can
run faster the second time around (I believe do to optimization), so I
made sure that when I compared them, I always ran them twice to
eliminate this from being the answer. Even after running the query
several times in its original state, it took 8 seconds. After simply
changing the view to SQL, running it, and changing it back, it always
takes 1 second thereafter.

If anybody has an answer to this, I would appreciate it.

Ryan


Thank you for the response, Allen. That was very helpful. Is there
another way of recalculating the execution plans on a query or all
queries other than the way I described?

Ryan
Nov 12 '05 #3
I think you will find that compacting the database causes it to recalculate
the query plan. That would be the simplest approach for a database in
operation.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html

"Ryan" <rp@rdoffice.ne t> wrote in message
news:cf******** *************** ***@posting.goo gle.com...
"Allen Browne" <ab************ ***@bigpond.net .au> wrote in message

news:<7P******* *************@n ews-server.bigpond. net.au>...
When you save a query and run it, JET performs and saves an execution plan. The execution plan is not recalculated unless the query is altered.

If the data has changed radically since the query was created, the saved
execution plan may be completely inappropriate. For example, if you create the query with a few dozen records and later execute it with many thousands of records, the saved plan may be completely wrong. If you then change the SQL statement (or generate one dynamically in code), Access recalculates the execution plan and so the query runs in a way that is much more appropriate to the new data.

The issues regarding execution plan are quite different from the caching of the data which speeds up the retrieval of the information from the disk on subsequent reads.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html

"Ryan" <rp@rdoffice.ne t> wrote in message
news:cf******** *************** **@posting.goog le.com...
I came across a situation that I've been unable to explain and was
hoping somebody had an answer:

I had written an update query which was taking about 8 seconds to run
and considered it too slow. I copied the SQL statement from the query
and tried executing it from code which then ran in 1 second. To make
sure that I didn't miss anything, I copied the SQL statement back into
a query and tried running it again. It now also only took 1 second
and looked exactly the same as before. I later realized that if I
take the original query, change it to SQL view and run it, then change
it back to design view and save it, it acomplishes the same thing.
The query looks exactly the same as before, but appears to have been
optimized somehow by running as a SQL statement first (either from
code or simply from the SQL view of the query).

Just to clear this from the discussion, I understand that a query can
run faster the second time around (I believe do to optimization), so I
made sure that when I compared them, I always ran them twice to
eliminate this from being the answer. Even after running the query
several times in its original state, it took 8 seconds. After simply
changing the view to SQL, running it, and changing it back, it always
takes 1 second thereafter.

If anybody has an answer to this, I would appreciate it.

Ryan


Thank you for the response, Allen. That was very helpful. Is there
another way of recalculating the execution plans on a query or all
queries other than the way I described?

Ryan

Nov 12 '05 #4
>
Hmm... interesting. So, how do you force Access to reoptimize your
queries Loop through them in VB and do some minor thing then undo it
to force the compiler to see the query as changed? (Okay, I'll go
back to smoking crack now...)


My question exactly. By the way, I work for a drug testing company
(i.e. smoking crack) (<:

Allen, I tried compacting the database and even repairing it. Neither
one seemed to effect anything. I even tried making a small change to
the query, saving it, and running it again and still no change. The
only time it makes a major difference (from over 30 seconds to less
than 5) is when I change the query view to SQL, run it, then change it
back to design view and run it again. When the query is in SQL view,
it is still slow and sometimes I even need to break it because it
takes so long. However, as soon as I switch back to design view, it
is lightning fast compared to what it was. Also, if I copy the SQL
statement out of the query and run it in code, it is very fast.

I understand what you are saying about the "execution plan" and it
makes sense, because the tables did indeed grow to thousands of
records since it was written. I would just like to know how I could
re-calculate all of the execution plans. Although, I've noticed that
this set of events only seems to help on my update queries, not
others.

Ryan
Nov 12 '05 #5
Interesting, Ryan. I've never had reason to test MS's claim that compacting
causes the query plan to be recalc'd.

You could try reassigning the SQL property of the QueryDef - even assigning
it to itself:

For Each qdf in db.QueryDefs
qdf.SQL = qdf.SQL
Next

Over time this approach may cause the database to bloat.
Interestingly, I think there is a training CrLf at the end of the SQL
statement built through the query design grid.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html

"Ryan" <rp@rdoffice.ne t> wrote in message
news:cf******** *************** ***@posting.goo gle.com...

Hmm... interesting. So, how do you force Access to reoptimize your
queries Loop through them in VB and do some minor thing then undo it
to force the compiler to see the query as changed? (Okay, I'll go
back to smoking crack now...)


My question exactly. By the way, I work for a drug testing company
(i.e. smoking crack) (<:

Allen, I tried compacting the database and even repairing it. Neither
one seemed to effect anything. I even tried making a small change to
the query, saving it, and running it again and still no change. The
only time it makes a major difference (from over 30 seconds to less
than 5) is when I change the query view to SQL, run it, then change it
back to design view and run it again. When the query is in SQL view,
it is still slow and sometimes I even need to break it because it
takes so long. However, as soon as I switch back to design view, it
is lightning fast compared to what it was. Also, if I copy the SQL
statement out of the query and run it in code, it is very fast.

I understand what you are saying about the "execution plan" and it
makes sense, because the tables did indeed grow to thousands of
records since it was written. I would just like to know how I could
re-calculate all of the execution plans. Although, I've noticed that
this set of events only seems to help on my update queries, not
others.

Ryan

Nov 12 '05 #6
"Allen Browne" <ab************ ***@bigpond.net .au> wrote in message news:<sO******* *************@n ews-server.bigpond. net.au>...
Interesting, Ryan. I've never had reason to test MS's claim that compacting
causes the query plan to be recalc'd.

You could try reassigning the SQL property of the QueryDef - even assigning
it to itself:

For Each qdf in db.QueryDefs
qdf.SQL = qdf.SQL
Next

Over time this approach may cause the database to bloat.
Interestingly, I think there is a training CrLf at the end of the SQL
statement built through the query design grid.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html


Hey Allen,

I tried the above and it did work. Thank you very much. After I
optimized the query, I tried making change to it to see if it remains
fast and it did. I suppose I'll just have to re-set the SQL statement
with the above method every so often to keep things optimized in the
event of major data increases.

Thanks again for the assistance.
Ryan
Nov 12 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2860
by: A Future Computer Scientist | last post by:
A question: Is it really important to think about optimizing the native code or optimizing it for P Code? Or does the code you write make a difference?
0
2349
by: Soefara | last post by:
Dear Sirs, I am experiencing strange results when trying to optimize a LEFT JOIN on 3 tables using MySQL. Given 3 tables A, B, C such as the following: create table A ( uniqueId int not null default 0 auto_increment, a1 varchar(64) not null default '',
4
9735
by: Stefano | last post by:
Hi everibody, it's the first time i post on this newsgroup. I'm Stefano from Milano, italy. I'm a beginners with Sql2000. My problem is this. I run a View using enterprise manager and after less then 20 second it goes in error time out. I run this view using a VB application and the error comes again .When i run it with Query Analyzer after...
6
1988
by: Uros | last post by:
Hello! I have some trouble getting good results from my query. here is structure stat_views id | integer id_zone | integer created | timestamp
10
4394
by: Henk Ernst Blok | last post by:
Hi Posgres users/developers, Can anyone explain why PosgreSQL (version 7.4.5 on Linux) does a full table scan to compute a count(*) on a base table after a vacuum analyze has been done with no following updates that might have outdated any statistics. Strangly the explain command does give the correct number of tuples instantaniously from...
1
1416
by: xpcer | last post by:
hi, friends, i have an problem, like this, i have tables, when i want to use "select" statement that include "join" sintaxt, my query will execute about 1 hour, so long. can u tell me how to optimizing my query, may be using index, or another?
0
2118
by: rashmigaikwad | last post by:
Hi All, I need help in optimizing the query mentioned below: SELECT SUM(CASE WHEN PROD_TYP='HBRMC' AND INC_MULTIPLE < 2.50 AND LTV <= 0.75 OR LTV IS NULL THEN ADV_IN_QTR ELSE 0 END) HBRMC_2p5, SUM(CASE WHEN PROD_TYP='HBNMC' AND INC_MULTIPLE < 2.50 AND LTV <= 0.75 OR LTV IS NULL THEN ADV_IN_QTR ELSE 0 END) HBNMC_2p5, SUM(CASE...
2
1812
by: Paul Furman | last post by:
I don't know, maybe this isn't strange but someone else set up the shopping card coding I'm working with, the way it works is to get the time() in seconds like 1172693735 and that's the shopper_ID number and the shopping_cart_last_modified number is generated the same way once they check out... then an invoice number is generated by...
5
2870
by: John Rivers | last post by:
Hello has anybody else noticed I have queries that SQL 2000 optimizes correctly and they run very fast Yet SQL 2005 keeps using a dumb query plan and queries run very slow The problem seems to stem from the assumption that data in a derived
0
7401
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7807
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7756
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5971
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5326
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4944
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3450
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1879
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1014
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.