Question : TSQL Query - retrieving data for use with virtual listview

I have a stored procedure running an an MSSQL 2005 server that I use to return data to populate a virtual listview VB.Net 2008).

Basically the stored procedure takes as parameters the start row number and end row number of the data that is to be retrieved, and returns just the rows of data that are required by the visible portion of the virtual listview. In other words the data is retrieved in pages.

The problem I have is that the columns of data in the listview can be sorted, so I also pass the sort info into the sproc. i.e. I pass in the column ID and sort direction so the sproc can pass back the data sorted by the correct column and in the correct direction.

This means that to select the correct range of data I have to select and sort the whole dataset in the sproc *before* I can filter it down to just the rows needed by the listview. Net result is that even if only 10 rows are required by the listview, the sproc has to select in all the rows in the table (could be 1000's), and sort them before it can return the 10 required rows.

Would appreciate any ideas how to improve the performance of this query as the whole point of using a virtual listview is to only select enough data to fill the visible portion of the listview, however in the sproc i have to select all records then filter them down.

An outline of the sproc can be found below.

Need to see if we can find a way to return the correct page of data, sorted by the correct column without having to select the whole table first.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
CREATE PROCEDURE [dbo].[GetVirtualListItems]
	@StartIndex int,
	@EndIndex int,
	@SortColumn int,
	@SortDirection int
AS
BEGIN
	WITH SortedData AS
	( 
		SELECT	MyTable.Id,
			MyTable.Firstname,
			MyTable.Surname,
			ROW_NUMBER() OVER(ORDER BY 
					CASE WHEN @SortColumn = 1 AND @SortDirection = 0 THEN Id END DESC, 
					CASE WHEN @SortColumn = 1 AND @SortDirection = 1 THEN Id END ASC,
					CASE WHEN @SortColumn = 2 AND @SortDirection = 0 THEN Firstname END DESC, 
					CASE WHEN @SortColumn = 2 AND @SortDirection = 1 THEN Firstname END ASC,
					CASE WHEN @SortColumn = 3 AND @SortDirection = 0 THEN Surname END DESC,  
					CASE WHEN @SortColumn = 3 AND @SortDirection = 1 THEN Surname END ASC
				)   AS 'RowNumber'		
				FROM MyTable							
	 ) 
	SELECT *
	FROM SortedData 
	WHERE RowNumber BETWEEN @StartIndex AND @EndIndex
END

Answer : TSQL Query - retrieving data for use with virtual listview

try something like this:


        With SQLPaging  
    As  
    (
            Select Top @StartIndex  - @EndIndex ) MyTable      .*, ROW_NUMBER() OVER (ORDER BY internalid) as row  
            SELECT      MyTable.Id,
                  MyTable.Firstname,
                  MyTable.Surname,
                  ROW_NUMBER() OVER(ORDER BY
                              CASE WHEN @SortColumn = 1 AND @SortDirection = 0 THEN Id END DESC,
                              CASE WHEN @SortColumn = 1 AND @SortDirection = 1 THEN Id END ASC,
                              CASE WHEN @SortColumn = 2 AND @SortDirection = 0 THEN Firstname END DESC,
                              CASE WHEN @SortColumn = 2 AND @SortDirection = 1 THEN Firstname END ASC,
                              CASE WHEN @SortColumn = 3 AND @SortDirection = 0 THEN Surname END DESC,  
                              CASE WHEN @SortColumn = 3 AND @SortDirection = 1 THEN Surname END ASC
                        )   AS 'RowNumber'            
                        FROM MyTable      
                  )  
   select * from SQLPaging where row > @StartIndex  
Random Solutions  
 
programming4us programming4us