SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Limiting rows in CTE with pagination? SQL 2008R2


Limiting rows in CTE with pagination? SQL 2008R2

Author
Message
themitchelli
themitchelli
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
Points: 2 Visits: 4
I have been asked to look at tuning a improving an SP query that uses an CTE and rownumber on a table that holds 30m records. It looks like it is having to work on the full result set of 30m rows to do the numbering so it is taking minutes to run. I want to limit it the query within the CTE to look at only the top 1000 rows to speed up performance but am struggling with the syntax.

The output is used in a webscreen that requires pagination control so the RowNumber and TotalRowCount are required for 'Page x of y' control.

Every change I attempt either breaks the query or messes up the pagination. Is there a way I can limit the CTE to the top x records? Code example below. I'd really appreciate some assistance.

Many thanks

Steve

Declare @sortcriteria nvarchar (50) = 'Date DESC',
@filterCriteria nvarchar (50) = '12345'

;WITH CTE AS (
SELECT
-- get
ROW_NUMBER() OVER(
ORDER BY
CASE
WHEN @sortCriteria = 'Date ASC' then T.AuditDate
WHEN @sortCriteria = 'UserName ASC' then T.UserName
END ASC,
CASE
WHEN @sortCriteria = 'Date DESC' then T.AuditDate
WHEN @sortCriteria = 'UserName ASC' then T.UserName
END DESC) AS SortingRowNumber,
COUNT(*) OVER(PARTITION BY NULL) AS TotalRowCount,
Col1,
Col2
FROM Table1 T
INNER JOIN Tables ON ....
WHERE
T.Id = @FilterCritera
)

SELECT
SortingRowNumber,
TotalRowCount,
Col1,
Col2
FROM
CTE
themitchelli
themitchelli
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
Points: 2 Visits: 4
Ah sorry - forum newbe - please move to the SQL2008 - T SQL board Unsure
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search