A technique I use to query the MOST RECENT rows in very large tables (100+ million or 1+ billion rows) is limiting the query to "reading" only the most recent "N" percentage of RECENT ROWS. This is real world applications, for example I do this for non-historic Recent Weather Data, or recent News feed searches or Recent GPS location data point data.
This is a huge performance improvement if you know for certain that your rows are in the most recent TOP 5% of the table for example. Such that even if there are indexes on the Tables, it further limits the possibilites to only 5% of rows in tables which have 100+ million or 1+ billion rows. This is especially the case when Older Data will require Physical Disk reads and not only Logical In Memory reads.
This is well more efficient than SELECT TOP | PERCENT | LIMIT as it does not select the rows, but merely limit the portion of the data to be searched.
DECLARE @RowIdTableA BIGINT DECLARE @RowIdTableB BIGINT DECLARE @TopPercent FLOAT -- Given that there is an Sequential Identity Column -- Limit query to only rows in the most recent TOP 5% of rows SET @TopPercent = .05 SELECT @RowIdTableA = (MAX(TableAId) - (MAX(TableAId) * @TopPercent)) FROM TableA SELECT @RowIdTableB = (MAX(TableBId) - (MAX(TableBId) * @TopPercent)) FROM TableB SELECT * FROM TableA a INNER JOIN TableB b ON a.KeyId = b.KeyId WHERE a.Id > @RowIdTableA AND b.Id > @RowIdTableB AND a.SomeOtherCriteria = 'Whatever'
idis indexed then it will just scan that index in reverse and stop after the first 5 rows. If it is not indexed then it will need to do aTOP Nsort. This won't be worse than any other way of doing it. It doesn't sort the whole table (though it would need to scan the whole table)ROW_NUMBER OVER PARTITION BYfor this type of query in MS SQL Server is much, much faster that the other methods. So I for one am happy that it got marked as the accepted answer, all other things being equal.