Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 123»»»

SQL Plan Cache Expand / Collapse
Author
Message
Posted Wednesday, August 15, 2012 8:55 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Today @ 3:20 AM
Points: 1,375, Visits: 2,661
Hi All

I've been using the below script to check my Plan Cache for Ad-hoc plans

SELECT objtype 
, usecounts
, COUNT(*) AS [no_of_plans]
FROM sys.dm_exec_cached_plans
WHERE cacheobjtype = 'Compiled Plan'
AND usecounts = '1'
GROUP BY objtype , usecounts
ORDER BY objtype , usecounts

How do I know what I high number is for my system?

I've also used the below script to check Compilations/sec
select * from sys.dm_os_performance_counters
where counter_name like '%compilations%'

My system reports an amount around 622364035 - Is this normal?, it seems high

Thanks


Post #1345321
Posted Wednesday, August 15, 2012 7:40 PM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Today @ 3:51 AM
Points: 15,517, Visits: 27,897
That number does seem high, but only because I've primarily worked on systems with stored procedures. If you're working with lots of dynamic or ad hoc SQL, then that number can be "normal." The only way to know for sure is to observe the system over time, recording the values, and compare one state to another in order to determine what's "normal" for the situation.

----------------------------------------------------
"The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood..." Theodore Roosevelt
The Scary DBA
Author of: SQL Server 2012 Query Performance Tuning
SQL Server 2008 Query Performance Tuning Distilled
and
SQL Server Execution Plans

Product Evangelist for Red Gate Software
Post #1345627
Posted Wednesday, August 15, 2012 11:54 PM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Today @ 3:20 AM
Points: 1,375, Visits: 2,661
Grant Fritchey (8/15/2012)
That number does seem high, but only because I've primarily worked on systems with stored procedures. If you're working with lots of dynamic or ad hoc SQL, then that number can be "normal." The only way to know for sure is to observe the system over time, recording the values, and compare one state to another in order to determine what's "normal" for the situation.


Thanks

To make sure I have this right, if a Query is submitted to SQL Server and it's not part of a Stored Procedure, SQL Server has to generate a plan for it everytime?

Thanks
Post #1345677
Posted Wednesday, August 15, 2012 11:56 PM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Today @ 3:20 AM
Points: 1,375, Visits: 2,661
Grant Fritchey (8/15/2012)
That number does seem high, but only because I've primarily worked on systems with stored procedures. If you're working with lots of dynamic or ad hoc SQL, then that number can be "normal." The only way to know for sure is to observe the system over time, recording the values, and compare one state to another in order to determine what's "normal" for the situation.


How do I know when my Cache is bloated? And cannot take anymore

Does it depend on my memory available or is it dependant on CPU?

Thanks
Post #1345679
Posted Thursday, August 16, 2012 2:07 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 3:52 AM
Points: 42,445, Visits: 35,501
SQLSACT (8/15/2012)
To make sure I have this right, if a Query is submitted to SQL Server and it's not part of a Stored Procedure, SQL Server has to generate a plan for it everytime?

Thanks


No. Ad-hoc SQL statements have their plans cached and reused just like stored procedures Only difference is the matching is on statement text not object id.



Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass

Post #1345727
Posted Thursday, August 16, 2012 2:11 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Today @ 3:20 AM
Points: 1,375, Visits: 2,661
GilaMonster (8/16/2012)
SQLSACT (8/15/2012)
To make sure I have this right, if a Query is submitted to SQL Server and it's not part of a Stored Procedure, SQL Server has to generate a plan for it everytime?

Thanks


No. Ad-hoc SQL statements have their plans cached and reused just like stored procedures Only difference is the matching is on statement text not object id.


Thanks

Regarding the 3 select statements below, would SQL use the same plan for the first 2 and a different plan for the 3rd one?

select Col1 from Table1 where Col1 = '10';
select Col1 from Table1 where Col1 = '10';
select Col1 from Table1 where Col1 = '12';

Post #1345731
Posted Thursday, August 16, 2012 2:13 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 3:52 AM
Points: 42,445, Visits: 35,501
In that trivial case, if they are submitted in 3 separate batches, there will be one plan (autoparameterised) used 3 times.


Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass

Post #1345733
Posted Thursday, August 16, 2012 2:25 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Today @ 3:20 AM
Points: 1,375, Visits: 2,661
GilaMonster (8/16/2012)
In that trivial case, if they are submitted in 3 separate batches, there will be one plan (autoparameterised) used 3 times.


Thanks

What would cause SQL Server to generate a seperate plan for the 3rd select statement?

Thanks
Post #1345738
Posted Thursday, August 16, 2012 2:28 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 3:52 AM
Points: 42,445, Visits: 35,501
A query complex enough that it doesn't qualify for autoparameterisation (and this is the point where you go to BoL to see what autoparam is)


Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass

Post #1345741
Posted Thursday, August 16, 2012 2:42 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Today @ 3:20 AM
Points: 1,375, Visits: 2,661
GilaMonster (8/16/2012)
A query complex enough that it doesn't qualify for autoparameterisation (and this is the point where you go to BoL to see what autoparam is)


Will do, thanks

Please help me understand something

I've executed those select scripts seperately. Then I ran the below script to check the cache:
SELECT TOP ( 100 ) [text] 
, cp.size_in_bytes
, cp.usecounts
FROM sys.dm_exec_cached_plans AS cp
CROSS APPLY sys.dm_exec_sql_text(plan_handle) st
WHERE cp.cacheobjtype = 'Compiled Plan'
AND cp.objtype = 'Adhoc'
ORDER BY cp.size_in_bytes DESC

Results attached

Then I ran this script to check Query Stats:
SELECT 
SUBSTRING(t.text, ( s.statement_start_offset / 2 ) + 1,
( ( CASE statement_end_offset
WHEN -1 THEN DATALENGTH(t.text)
ELSE s.statement_end_offset
END - s.statement_start_offset ) / 2 ) + 1)
AS statement_text,
last_execution_time,
execution_count ,
statement_start_offset AS stmt_start_offset ,
total_logical_reads / execution_count AS avg_logical_reads ,
total_logical_writes / execution_count AS avg_logical_writes ,
total_physical_reads / execution_count AS avg_physical_reads ,
t.text,
qp.query_plan
FROM sys.dm_exec_query_stats AS s
CROSS APPLY sys.dm_exec_sql_text(s.sql_handle) AS t
CROSS APPLY sys.dm_exec_query_plan(s.plan_handle) qp
where SUBSTRING(t.text, ( s.statement_start_offset / 2 ) + 1,
( ( CASE statement_end_offset
WHEN -1 THEN DATALENGTH(t.text)
ELSE s.statement_end_offset
END - s.statement_start_offset ) / 2 ) + 1) like '%Col1%'
ORDER BY avg_physical_reads DESC

Results attached

The cached_plan script shows 2 plans were generated, one plan shows a usecount of 2 and the other plan shows a usecount of 1

The Query_stats shows one plan with an execution count of 3

Which one is correct here or am I missing something?

Thanks


  Post Attachments 
Cache Result.JPG (2 views, 17.60 KB)
Query Stats Result.JPG (6 views, 18.61 KB)
Post #1345750
« Prev Topic | Next Topic »

Add to briefcase 123»»»

Permissions Expand / Collapse