4

-------------------- this takes 4 secs to execute (with 2000 000 rows) WHY?---------------------

DECLARE @AccountId INT 
DECLARE @Max INT 
DECLARE @MailingListId INT 

SET @AccountId = 6730
SET @Max = 2000
SET @MailingListId = 82924

SELECT TOP (@Max) anp_Subscriber.Id , Name, Email 
FROM anp_Subscription WITH(NOLOCK) 
  INNER JOIN anp_Subscriber WITH(NOLOCK) 
    ON anp_Subscriber.Id = anp_Subscription.SubscriberId
WHERE [MailingListId] = @MailingListId 
  AND Name LIKE '%joe%' 
  AND [AccountID] = @AccountId

--------------------- this takes < 1 sec to execute (with 2000 000 rows) -----------------------

SELECT TOP 2000 anp_Subscriber.Id ,Name, Email 
FROM anp_Subscription WITH(NOLOCK) 
  INNER JOIN anp_Subscriber WITH(NOLOCK)
    ON anp_Subscriber.Id = anp_Subscription.SubscriberId
WHERE [MailingListId] = 82924 
  AND Name LIKE '%joe%' 
  AND [AccountID] = 6730

Why the difference in excecution time? I want to use the query at the top. Can I do anything to optimize it?

Thanks in advance! /Christian

6
  • I think this is related: stackoverflow.com/questions/414336/… Commented Oct 22, 2011 at 15:55
  • And the MSDN article: technet.microsoft.com/en-gb/library/cc966425.aspx#E6TAE Commented Oct 22, 2011 at 15:56
  • In the first case, SQL Server must optimize the query with the parameters for any possible value of those parameters. In the second case, with the actual literal values, the query optimizer can choose a more appropriate query plan - since it needs to be perfect for just those particular values. Commented Oct 22, 2011 at 15:57
  • Could i specify the parameters more specifically in some way maybe? Commented Oct 22, 2011 at 16:10
  • 1
    Have you tried adding OPTION (OPTIMIZE FOR (@Max = 2000)) Commented Oct 22, 2011 at 16:30

3 Answers 3

6

Add OPTION (RECOMPILE) to the end of the query.

SQL Server doesn't "sniff" the values of the variables so you will be getting a plan based on guessed statistics rather than one tailored for the actual variable values.

Sign up to request clarification or add additional context in comments.

6 Comments

Thank you. This helped to improve the performance. But another question: Is it demanding on the server to do a recompile every time the query is excecuted? It will probably be used pretty much...
@user1008621 - Does seem a bit of a waste of time if you are going to be constantly recompiling with the same values you could try OPTION (OPTIMIZE FOR as per GilM's suggestion.
I also thought so, but the only value I can optimize for in this query is @Max, and for some reason the excecution time is not improved for this... Any idea of why?
@ChristianV - Not sure what you mean that's the only value you can optimize for. The syntax OPTION (OPTIMIZE FOR (@AccountId = 6730, @Max = 2000, @MailingListId = 82924)) should work? You could also look at the difference between the plans and use explicit query hints to get the plan you want (e.g. join type or access method)
Oh, I just ment that I have no idea of which accountId or mailingListId that is going to be used... So I don't know if it's the right way to optimize the query for those specific values? What do you mean by access method by the way?
|
0

One possible item to check is whether the MailingListId and AccountId fields in the tables are of type INT. If, for example, the types are BIGINT, the query optimizer will often not use the index on those fields. When you explicitly define the values instead of using variables, the values are implicitly converted to the proper type.

Make sure the types match.

Comments

0

The second query has to process ONLY 2000 records. Point.

The first has to process ALL records to find the maximum.

Top 2000 does not get you the highest 2000, it gets you the first 2000 of the result set - in any order.

if yo uwant to change them to be identical, the second should read

TOP 1

and then order by anp_Subscriber.Id descending (plus fast first option).

1 Comment

I am not using the Max function. Not smart of me to use @Max as a variable name maybe... :/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.