I am using SQL Server 2012 to try to implement efficient paging using OFFSET and FETCH. Here's some sample code:
SELECT ID, Name, ... , ColumnZ
FROM SomeTable
ORDER BY Name DESC
OFFSET @StartRowIndex ROWS
FETCH NEXT @MaximumRows ROWS ONLY
For this example, assume the following:
- Name column is NVARCHAR(200) and NOT indexed.
- @MaximumRows is set to 10
- SomeTable has many records e.g. 50,000 records.
Question: Will the above SQL code:
1. First retrieved only the next 10 records, and only then order by the Name column on those 10 records.
2. First order by the Name column on all 50,000 records, and only then retrieve the next 10 records of those sorted.
Or is it some other behaviour ... any ideas?
I know I can improve performance in this example by placing an index on the Name column.
But in reality, the order by column may be any column within the table, so I don't want to just index all or most columns.
Thanks