SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Administration
 Query runs quicker second time
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

ranvir_2k
Posting Yak Master

United Kingdom
174 Posts

Posted - 10/15/2013 :  13:37:04  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

3761 Posts

Posted - 10/15/2013 :  14:57:07  Show Profile  Reply with Quote
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

USA
37466 Posts

Posted - 10/15/2013 :  14:59:19  Show Profile  Visit tkizer's Homepage  Reply with Quote
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

United Kingdom
174 Posts

Posted - 10/16/2013 :  09:24:42  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

3761 Posts

Posted - 10/16/2013 :  09:30:51  Show Profile  Reply with Quote
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

United Kingdom
174 Posts

Posted - 10/16/2013 :  13:42:02  Show Profile  Reply with Quote
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

USA
37466 Posts

Posted - 10/16/2013 :  13:51:06  Show Profile  Visit tkizer's Homepage  Reply with Quote
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

United Kingdom
174 Posts

Posted - 10/17/2013 :  07:24:43  Show Profile  Reply with Quote
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?

Edited by - ranvir_2k on 10/17/2013 07:25:10
Go to Top of Page

tkizer
Almighty SQL Goddess

USA
37466 Posts

Posted - 10/17/2013 :  14:22:30  Show Profile  Visit tkizer's Homepage  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000