SQL Server SELECT LAST N Rows

前端 未结 18 2256
猫巷女王i
猫巷女王i 2020-11-28 03:13

This is a known question but the best solution I\'ve found is something like:

SELECT TOP N *
FROM MyTable
ORDER BY Id DESC

I\'ve a table wi

18条回答
  •  没有蜡笔的小新
    2020-11-28 04:09

    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'
    

提交回复
热议问题