SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 top performance
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

yoyosh
Starting Member

27 Posts

Posted - 01/29/2013 :  02:12:32  Show Profile  Reply with Quote
I created simple query with several joins and a top(5) clause, which took more than a minute to execute.

However when I changed:

select top(5)

to

declare @i int
set @i = 5
select top (@i) ...

It took less than a second to execute. Why is that?

Below are links to execution plans:
top(5): http://imageshack.us/f/24/topsim.png/
top(@i): http://imageshack.us/f/203/topvar.png/

Thank you for help in advance

DonAtWork
Flowing Fount of Yak Knowledge

2144 Posts

Posted - 01/29/2013 :  07:12:53  Show Profile  Reply with Quote
Perhaps the execution plan was cached after the first run, and will make any following run a lot faster? Try running it the first way again and see.








How to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Go to Top of Page

yoyosh
Starting Member

27 Posts

Posted - 01/29/2013 :  08:06:36  Show Profile  Reply with Quote
I tried. It does not affect execution times in both cases.
Go to Top of Page

James K
Flowing Fount of Yak Knowledge

3326 Posts

Posted - 01/29/2013 :  08:11:18  Show Profile  Reply with Quote
Enable execution plan (control-m) and run both in the same query window. It will show you the relative costs and query plans. Examine the different query plans to see what is different. Is it just the query that you have shown that you are running, or is there more to it? Is it part of a stored procedure? If it is part of a stored procedure (or even if it is not), you can try statement level recompilation hint (add OPTION (RECOMPILE) at the end of the statement) to see if that makes any difference.
Go to Top of Page

yoyosh
Starting Member

27 Posts

Posted - 01/29/2013 :  08:14:44  Show Profile  Reply with Quote
As you could see from my initial post, i enabled them from the beginning. I have even attached links to them. What can you say about them?
Go to Top of Page

James K
Flowing Fount of Yak Knowledge

3326 Posts

Posted - 01/29/2013 :  08:25:41  Show Profile  Reply with Quote
Ah, I didn't see that - I am not able to access images from behind the corporate firewall. Hopefully someone else who is able to see the images can comment.
Go to Top of Page

yoyosh
Starting Member

27 Posts

Posted - 01/30/2013 :  07:37:56  Show Profile  Reply with Quote
Anyone could help me on that?
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000