chache queries

  • 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.

  • 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

  • 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

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

  • 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

  • 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

  • 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

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

  • 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

  • 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

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

  • 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?

  • 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.

Viewing 10 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic. Login to reply