Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Selecting TOP N - Performance Issue

Author  Topic 

keyursoni85
Posting Yak Master

233 Posts

Posted - 2013-09-07 : 05:08:52
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
Master Smack Fu Yak Hacker

3873 Posts

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

jackv
Master Smack Fu Yak Hacker

2179 Posts

Posted - 2013-09-07 : 11:08:28
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
   

- Advertisement -