Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Administration
 Query runs quicker second time

Author  Topic 

ranvir_2k
Posting Yak Master

180 Posts

Posted - 2013-10-15 : 13:37:04
I have a query that took 4 hours to run the first time. It does selects, inserts, updates.

I cleared up the data that it inserted and ran it again straightaway afterwards. This time it took 6 mins. Is there any reason why it would be so much quicker the second time?

I rebooted the server and it was back to taking 4 hours.

How can I improve performance so that it always takes 6 mins.

Thanks

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-10-15 : 14:57:07
It is normal for a query to take a little longer when you run it the first time - SQL Server needs to build a query plan, needs to load the data from disk etc. When you run a second time, the data would alredy be in cache, it may be able to reuse the query plan etc.

However, the difference being four hours to six minutes is rather unusual and points to something else. Is it possible that there are one-time calculations that must be run the first time when you run the query? For example, if it is building a global temporary table, the first time around, but does not need to do that on subequent runs, this type of thing can happen.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2013-10-15 : 14:59:19
My reply in your other, similar post applies here: http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=188875

Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

ranvir_2k
Posting Yak Master

180 Posts

Posted - 2013-10-16 : 09:24:42
Thanks tkizer. I appreciate your help.

Is there a way for me to capture the exection plan that the stored procedure is using when it is run the second time?
I would then like to use this execution plan all of the time.

The stored procedure itself is over 2000 lines and was not written by myself. So looking at a graphical query plan will take quite some time.
Go to Top of Page

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-10-16 : 09:30:51
You can generate an XML query plan and then use that with the use plan query hint. This page shows you how to capture and how to use it: http://technet.microsoft.com/en-us/library/ms186954(v=sql.105).aspx There are some limitations - which are also described in that page. I have had limited success with use plan query hint.
Go to Top of Page

ranvir_2k
Posting Yak Master

180 Posts

Posted - 2013-10-16 : 13:42:02
I do see this memory error message in the error log:
A significant part of sql server process memory has been paged out. This may result in a performance degradation. Duration: 0 seconds. Working set (KB): 36612, committed (KB): 104768, memory utilization: 34%.

Could this be causing the slow running of the query?
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2013-10-16 : 13:51:06
Yes you need to get that memory issue fixed. Have you setup Lock Pages in Memory?

Also, what version of SQL Server are you using? SQL Server 2008 and newer has plan guides. I've recently deployed 3 plan guides to a critical application that was upgraded to SQL Server 2012 recently.

Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

ranvir_2k
Posting Yak Master

180 Posts

Posted - 2013-10-17 : 07:24:43
My SQL Server service account and SQL Agent account are different.

Does lock pages in memory need to be set for both accounts or just the SQL Server account?
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2013-10-17 : 14:22:30
quote:
Originally posted by ranvir_2k

My SQL Server service account and SQL Agent account are different.

Does lock pages in memory need to be set for both accounts or just the SQL Server account?



The SQL Server service account

Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/
Go to Top of Page
   

- Advertisement -