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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 How can I reduce high SQL parse and compile times?

Author  Topic 

eyechart
Master Smack Fu Yak Hacker

3575 Posts

Posted - 2007-12-06 : 16:59:47
I am trying to tune a really busy SQL environment. I have been able to successfully implement new indexes and have been able to eliminate all of the long running and high read SQL from the environment. I no longer have bad query plans (for the most part). I still have very high cpu utilization.

Some background: The box this is running on is an 8 core opteron HP DL585 w/32GB RAM. The instance this database is on has 20GB RAM allocated. The OS is x64, the SQL Server is x64 EE. the OS is current w/service packs and hotfixes. SQL Server is at post SP2 build 3186. The buffer cache hit ratio is 99.8%. The cpu utilization is 90-100% all of the time. Disk queuing is minimal (SAN connected to high-end Hitachi SAN).

I can't really post any information about the tables or the queries here or any sample data. I know that makes it tough for anyone to give assistance, but I am really not in need of help optimizing queries or tuning indexes, etc.

Instead, I am a little stumped on how to reduce the 'parse and compile time' for this query and that is what I really need help with. For example, here is STATISTICS IO and STATISTICS TIME output for one of 2 queries that is basically burying this box:

SQL Server parse and compile time: 
CPU time = 0 ms, elapsed time = 1 ms.

SQL Server Execution Times:
CPU time = 0 ms, elapsed time = 1 ms.
SQL Server parse and compile time:
CPU time = 0 ms, elapsed time = 619 ms.

Table 'OBFUSCATED1'. Scan count 0, logical reads 8, physical reads 0,
read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob
read-ahead reads 0.
Table 'OBFUSCATED2'. Scan count 4, logical reads 8, physical reads 0,
read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob
read-ahead reads 0.
Table 'OBFUSCATED3'. Scan count 0, logical reads 12, physical reads
0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob
read-ahead reads 0.
Table 'OBFUSCATED4'. Scan count 1, logical reads 4, physical reads 0,
read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob
read-ahead reads 0.


(1 row(s) affected)

SQL Server Execution Times:
CPU time = 0 ms, elapsed time = 6 ms.
SQL Server parse and compile time:
CPU time = 0 ms, elapsed time = 1 ms.

SQL Server Execution Times:
CPU time = 0 ms, elapsed time = 1 ms.


The query plan looks as good as it can be. The duration I get on this query (~650ms, matches with what I see when I trace with profiler). The tables in play here are fairly large, 40+ million rows in one and 114 million in another. Query plan is all index seeks. The application uses ad-hoc queries for everything with no use of prepared statements and very little use of stored procedures. That is something that I won't be able to change. Aside from that, everything looks good except for the parse and compile time.

btw, the procedure cache on this host looks like this - some info snipped out - sizes in MB:


SQL Plans 3593.234375
TokenAndPermUserStore 288.406250
Default 205.132812
SOS_Node 88.070312
SchemaMgr Store 87.765625
Object Plans 86.851562
SNIPacket 26.015625
Bound Trees 20.062500
SystemRowsetStore 7.640625
sxcCacheStore 3.609375


3.5GB for SQL Plans seems pretty large. My guess is that system is spending a long time scanning through the procedure cache looking for a plan for this query and that takes 619ms to do.

My thinking is that this is as good as it gets until the code is changed to use stored procs and prepared statements for greater efficiency. Any ideas? Have you solved a problem like this? THanks.



-ec

evilDBA
Posting Yak Master

155 Posts

Posted - 2007-12-06 : 17:10:18
Well, if you can not change your application...
If you have almost 4Gb of query plans, probably they are rarely reused.
SO you can just drop them using DBCC command say, every minute.
If yoyur guess is right (is spending a long time scanning through the procedure cache looking for a plan) then CPU will decrease.
If 619 ms is a time to generate a plan and put it into the cache, then it would not help.
Go to Top of Page

rmiao
Master Smack Fu Yak Hacker

7266 Posts

Posted - 2007-12-06 : 23:00:35
Are statistics up to date?
Go to Top of Page

eyechart
Master Smack Fu Yak Hacker

3575 Posts

Posted - 2007-12-07 : 03:44:15
quote:
Originally posted by rmiao

Are statistics up to date?



yep. and all indexes have been rebuilt too.



-ec
Go to Top of Page

pootle_flump

1064 Posts

Posted - 2007-12-07 : 04:20:01
These are just a couple of suggestions (I think the nub of the problem is you want more plan reuse right?):
If SQL Server is generating ad-hoc plans for statements that could, in theory, share the same plan but cannot be prepared then you could try using the OPTION PARAMETERIZATION FORCED hint. This forces all statements to be prepared and and the bonus is it will increase plan reuse. The downside is it will increase plan reuse
Another option - do you know about plan guides? I have not used them in anger but you can effectively force a statement to use the plan you specify instead of compiling a new plan.
Go to Top of Page

eyechart
Master Smack Fu Yak Hacker

3575 Posts

Posted - 2007-12-07 : 15:07:55
quote:
Originally posted by pootle_flump

These are just a couple of suggestions (I think the nub of the problem is you want more plan reuse right?):
If SQL Server is generating ad-hoc plans for statements that could, in theory, share the same plan but cannot be prepared then you could try using the OPTION PARAMETERIZATION FORCED hint. This forces all statements to be prepared and and the bonus is it will increase plan reuse. The downside is it will increase plan reuse
Another option - do you know about plan guides? I have not used them in anger but you can effectively force a statement to use the plan you specify instead of compiling a new plan.



Since there are only a few queries that are causing the problem I went with the plan guides. That has essentially solved our problem.

Parse and compile times are now down to 1ms for these queries, where before they were 500-700ms. We are now seeing 1500-2000 forced parameterizations/sec and cpu load has dropped to almost nothing. wow.

Thanks for the excellent suggestion.

btw, here is a microsoft technet page with information on how to use this feature (including code). http://technet.microsoft.com/en-us/library/ms191275.aspx



-ec
Go to Top of Page

pootle_flump

1064 Posts

Posted - 2007-12-10 : 03:17:52
Glad it worked I only really knew about them after seeing Kalen Delaney present here in Blighty last month.
Go to Top of Page
   

- Advertisement -