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
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Selecting TOP N - Performance Issue
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

keyursoni85
Posting Yak Master

India
233 Posts

Posted - 09/07/2013 :  05:08:52  Show Profile  Reply with Quote
Hi,

I am selecting TOP 1 record (last inserted) but I feel that it gives some performance effect. Because that table included millions of rows and TOP 1 downgrades performance.

I have given two ways for selecting TOP last one. Please suggest best way.



SELECT  [COLUMNS]....
FROM   LARGETABLE L 
	 inner join
	     (	select MAX(ID) maxchid
		from LARGETABLE
		where REFID = 1654424) chInner
	 on chInner.maxchid = ch.ID

---------------------------------

SELECT  TOP 1 [COLUMNS]....
FROM   LARGETABLE L 
WHERE  REFID = 1654424
ORDER  BY ID DESC

James K
Flowing Fount of Yak Knowledge

3557 Posts

Posted - 09/07/2013 :  08:35:33  Show Profile  Reply with Quote
Do you have any indexes on the table? An index on REFID, ID would help.
Go to Top of Page

jackv
Flowing Fount of Yak Knowledge

United Kingdom
2004 Posts

Posted - 09/07/2013 :  11:08:28  Show Profile  Visit jackv's Homepage  Reply with Quote
Have you checked the execution plans?Are there any clues which you can share

Jack Vamvas
--------------------
http://www.sqlserver-dba.com
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.05 seconds. Powered By: Snitz Forums 2000