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

chache queries Expand / Collapse
Author
Message
Posted Tuesday, April 29, 2014 11:28 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Today @ 8:05 AM
Points: 23, Visits: 58
Hi ,

Do you happen to know of a way to invalidate cached query plans?
I would rather target a specific query instead of invalidating all of them.

Also do you know of any sql server setting that will cause cached query plans to invalidate even though only one character in the queries has changed?

exec sp_executesql N'select
cast(5 as int) as DisplaySequence,
mt.Description + '' '' + ct.Description as Source,
c.FirstName + '' '' + c.LastName as Name,
cus.CustomerNumber Code,
c.companyname as "Company Name",
a.Address1,
a.Address2,
a.City,
sp.Abbreviation,
a.PostalCode,
cphone.PhoneNumber as Phone,
cfax.PhoneNumber as Fax,
ccell.PhoneNumber as Cell,
cemail.EmailAddress as Email,
wa.WebAddress as Web,
cl.Id as Id,
c.Id as ContactId
from
contactlink cl
left join ContactTypes ct on ct.id = cl.ContactTypeID
left join Contacts c on c.ID = cl.ContactID
left join Customers cus on cus.id = cl.CustomerID
left join MasterTypes mt on mt.id = ct.MasterTypeID
left join contactaddresslink cal on cal.ContactID = c.id
left join Addresses a on a.id = cal.AddressID
left join StatesProvinces sp on sp.id = a.StateProvinceID
left join ContactPhoneLink cpl_phone on cpl_phone.ContactID = c.id and cpl_phone.PhoneTypeID = 1
left join phones cphone on cphone.id=cpl_phone.PhoneID
left join ContactPhoneLink cpl_fax on cpl_fax.ContactID = c.id and cpl_fax.PhoneTypeID = 2
left join phones cFax on cFax.id=cpl_fax.PhoneID
left join ContactPhoneLink cpl_cell on cpl_cell.ContactID = c.id and cpl_cell.PhoneTypeID = 3
left join phones cCell on cCell.id=cpl_cell.PhoneID
left join ContactEmailLink cel on cel.ContactID = c.id
left join Emails cEmail on cEmail.id = cel.EmailID
left join ContactWebLink cwl on cwl.ContactID = c.id
left join WebAddresses wa on wa.id = cwl.WebAddressID
where cus.id = @customerId
',N'@CustomerId int',@CustomerId=1065


In this query we have seen (on some databases) simply changing ‘@CustomerId int',@CustomerId=1065’ too ‘@customerId int',@customerId=1065’ fixed the a speed problem….just changed the case on the Customer bind parameter. On other servers this has no effect. I’m thinking the server is using an old cached query plan, but don’t know for sure.
Post #1566066
Posted Tuesday, April 29, 2014 3:03 PM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 1:30 PM
Points: 2,856, Visits: 3,041
You can find the plan handle and then use DBCC FREEPROCCACHE to drop an individual plan. However, be careful not to just run DBCC FREEPROCCACHE without a plan handle as this will empty the entire plan cache.

Read this for more information:
http://technet.microsoft.com/en-us/library/ms174283.aspx
Post #1566149
Posted Tuesday, April 29, 2014 3:37 PM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Today @ 3:23 PM
Points: 15,664, Visits: 28,062
You can get the plan_handle by querying sys.dm_exec_query_stats or sys.dm_exec_proc_stats if it's a stored procedure. If you need to use sys.dm_exec_query_stats, you can combine it with sys.dm_exec_sql_text in order to do a filter for your particular query.

It sounds like you're hitting bad parameter sniffing. One value is creating a poor plan, but other values are working fine. You'll need to capture both execution plans and then look at the compile value for the parameters (stored in the SELECT operator). Compare those values to the values in the statistics for the index in question to understand why you're getting a bad plan.

To fix it, you need to decide if you want to go for a specific plan or a generic plan. For a specific plan, look up OPTIMIZE FOR query hint, or, use RECOMPILE at the statement level. For a generic plan look up OPTIMIZE FOR UNKNOWN.

Yes, just changing the case on a parameter or a variable is a change to the procedure and SQL Server will recompile the plan for that proc.


----------------------------------------------------
"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 #1566154
Posted Wednesday, April 30, 2014 4:08 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Today @ 8:05 AM
Points: 23, Visits: 58
The query seems to have gone bad again on my machine with or without a capital ‘C’
I found a way to target and delete specific query plans, but it still is running slow from within .net.
I am trying to find out why it is slow sometimes and fast at other times, any help would be greatly appreciated.


Here is what I found

Find cached plans
SELECT plan_handle, st.text
FROM sys.dm_exec_cached_plans
CROSS APPLY sys.dm_exec_sql_text(plan_handle) AS st
WHERE text LIKE N'%wa.id = cwl.WebAddressID%'



Remove specific plan
DBCC FREEPROCCACHE (0x06001E00AC6A672F90961F6C0400000001000000000000000000000000000000000000000000000000000000
Post #1566287
Posted Wednesday, April 30, 2014 4:31 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Today @ 8:05 AM
Points: 23, Visits: 58
The query seems to have gone bad again on my machine with or without a capital ‘C’
I found a way to target and delete specific query plans, but it still is running slow from within .net.
I am trying to find out why it is slow sometimes and fast at other times, any help would be greatly appreciated.


Here is what I found

Find cached plans
SELECT plan_handle, st.text
FROM sys.dm_exec_cached_plans
CROSS APPLY sys.dm_exec_sql_text(plan_handle) AS st
WHERE text LIKE N'%wa.id = cwl.WebAddressID%'



Remove specific plan
DBCC FREEPROCCACHE (0x06001E00AC6A672F90961F6C0400000001000000000000000000000000000000000000000000000000000000
Post #1566293
Posted Wednesday, April 30, 2014 6:20 AM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Today @ 3:23 PM
Points: 15,664, Visits: 28,062
OK. Please follow my advice about investigating the execution plans.

Modifying the query will cause a recompile. But you also get recompiles from other sources. You can get a recompile because of changes to the statistics for the tables involved in the query. That's one of the most common sources. And if you're hitting bad parameter sniffing, the recompile can randomly (well, seemingly random, it's completely data driven) get a bad plan or a good plan because of these recompiles. It will stick with that bad plan until the next recompile event (including you removing the plan from cache).


----------------------------------------------------
"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 #1566346
Posted Wednesday, April 30, 2014 7:43 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Today @ 8:05 AM
Points: 23, Visits: 58
What I really need is a way to ensure that the query runs fast all the time, today it seems to be running fast on my machine. I have not done anything to code or database since I ran the following command yesterday.

USE <DATABASENAME>;
GO
CHECKPOINT;
GO
DBCC DROPCLEANBUFFERS;
GO


Is their any way??


Here is the full query that is being problematic

exec sp_executesql N'select
cast(5 as int) as DisplaySequence,
mt.Description + '' '' + ct.Description as Source,
c.FirstName + '' '' + c.LastName as Name,
cus.CustomerNumber Code,
c.companyname as "Company Name",
a.Address1,
a.Address2,
a.City,
sp.Abbreviation,
a.PostalCode,
cphone.PhoneNumber as Phone,
cfax.PhoneNumber as Fax,
ccell.PhoneNumber as Cell,
cemail.EmailAddress as Email,
wa.WebAddress as Web,
cl.Id as Id,
c.Id as ContactId
from
contactlink cl
left join ContactTypes ct on ct.id = cl.ContactTypeID
left join Contacts c on c.ID = cl.ContactID
left join Customers cus on cus.id = cl.CustomerID
left join MasterTypes mt on mt.id = ct.MasterTypeID
left join contactaddresslink cal on cal.ContactID = c.id
left join Addresses a on a.id = cal.AddressID
left join StatesProvinces sp on sp.id = a.StateProvinceID
left join ContactPhoneLink cpl_phone on cpl_phone.ContactID = c.id and cpl_phone.PhoneTypeID = 1
left join phones cphone on cphone.id=cpl_phone.PhoneID
left join ContactPhoneLink cpl_fax on cpl_fax.ContactID = c.id and cpl_fax.PhoneTypeID = 2
left join phones cFax on cFax.id=cpl_fax.PhoneID
left join ContactPhoneLink cpl_cell on cpl_cell.ContactID = c.id and cpl_cell.PhoneTypeID = 3
left join phones cCell on cCell.id=cpl_cell.PhoneID
left join ContactEmailLink cel on cel.ContactID = c.id
left join Emails cEmail on cEmail.id = cel.EmailID
left join ContactWebLink cwl on cwl.ContactID = c.id
left join WebAddresses wa on wa.id = cwl.WebAddressID
where cus.id = @customerId
',N'@customerId int',@customerId=1065
Post #1566399
Posted Wednesday, April 30, 2014 7:47 AM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Today @ 3:23 PM
Points: 15,664, Visits: 28,062
Since you've posted this question to a second location, we're now getting into cross-chatter. I made several recommendations over there for how to fix it. It depends on your data, your data distribution, and your statistics which solution is going to be the most viable for you. I also made a similar suggestion up above.

----------------------------------------------------
"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 #1566403
Posted Wednesday, April 30, 2014 11:04 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 8:34 PM
Points: 23,343, Visits: 32,079
Zohaib Anwar (4/30/2014)
What I really need is a way to ensure that the query runs fast all the time, today it seems to be running fast on my machine. I have not done anything to code or database since I ran the following command yesterday.

USE <DATABASENAME>;
GO
CHECKPOINT;
GO
DBCC DROPCLEANBUFFERS;
GO


Is their any way??


Here is the full query that is being problematic

exec sp_executesql N'select
cast(5 as int) as DisplaySequence,
mt.Description + '' '' + ct.Description as Source,
c.FirstName + '' '' + c.LastName as Name,
cus.CustomerNumber Code,
c.companyname as "Company Name",
a.Address1,
a.Address2,
a.City,
sp.Abbreviation,
a.PostalCode,
cphone.PhoneNumber as Phone,
cfax.PhoneNumber as Fax,
ccell.PhoneNumber as Cell,
cemail.EmailAddress as Email,
wa.WebAddress as Web,
cl.Id as Id,
c.Id as ContactId
from
contactlink cl
left join ContactTypes ct on ct.id = cl.ContactTypeID
left join Contacts c on c.ID = cl.ContactID
left join Customers cus on cus.id = cl.CustomerID
left join MasterTypes mt on mt.id = ct.MasterTypeID
left join contactaddresslink cal on cal.ContactID = c.id
left join Addresses a on a.id = cal.AddressID
left join StatesProvinces sp on sp.id = a.StateProvinceID
left join ContactPhoneLink cpl_phone on cpl_phone.ContactID = c.id and cpl_phone.PhoneTypeID = 1
left join phones cphone on cphone.id=cpl_phone.PhoneID
left join ContactPhoneLink cpl_fax on cpl_fax.ContactID = c.id and cpl_fax.PhoneTypeID = 2
left join phones cFax on cFax.id=cpl_fax.PhoneID
left join ContactPhoneLink cpl_cell on cpl_cell.ContactID = c.id and cpl_cell.PhoneTypeID = 3
left join phones cCell on cCell.id=cpl_cell.PhoneID
left join ContactEmailLink cel on cel.ContactID = c.id
left join Emails cEmail on cEmail.id = cel.EmailID
left join ContactWebLink cwl on cwl.ContactID = c.id
left join WebAddresses wa on wa.id = cwl.WebAddressID
where cus.id = @customerId
',N'@customerId int',@customerId=1065


Looking at the query above I have to ask why it is written as a dynamic sql query?



Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1566513
Posted Friday, May 2, 2014 3:53 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Today @ 8:05 AM
Points: 23, Visits: 58
How can I change it? Do you have any other way to write this? This query is used in an application having Linq SQL. I will appreciate if you will let me know other option that how can I write is instead of Dynamic sql.
Post #1566973
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse