Question

I trying to find out performance between paging methods which use SQL Server 2008. I have use flowing query:

WITH CTE(RowNo, UniqueId)
AS (
    SELECT TOP (5000+100) ROW_NUMBER() OVER (ORDER BY ReceiveDate) AS RowNo, UniqueId
    FROM LogEntries
    WHERE ServerId = '111.112.113.114'
)
SELECT 
    TOP 100 CTE.RowNo, CTE.UniqueId, L.[Subject]
    FROM
        CTE
        INNER JOIN LogEntries L ON L.UniqueId = CTE.UniqueId
    WHERE
        CTE.RowNo > 5000;

The Server Statistics report SQL Server took 232ms to complete the query. But when I remove TOP (5000+100), the job done half faster (just 135ms). I had tried repeatly number times, result as the same.

The table LogEntries has 12Mi rows / 3.3 GB size.

Any reason for limited result set run slower than full result?

After some suggest from SO'guys, with created the index on ReceiveDate (include ServerId) below is my fasted query, it took ~36ms without any extra ORDER BY required:

WITH CTE(RowNo, UniqueId)
AS (
    SELECT TOP 5100 ROW_NUMBER() OVER (ORDER BY ReceiveDate) AS RowNo, UniqueId
    FROM LogEntries
    WHERE ServerId = '111.112.113.114'
)
SELECT 
    CTE.RowNo, CTE.UniqueId, L.[Subject]
    FROM
        CTE
        LEFT JOIN LogEntries L ON L.UniqueId = CTE.UniqueId
    WHERE
        CTE.RowNo > 5000
Was it helpful?

Solution

you are already doing a row_number which require and order by on your sub result's table.

Like suggested, check the execution plan.

And you're top 5000 + 100 is redundant with your cte.rowno > 5000. Try cte.rowno between 5000 and 5100.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top