[ Team LiB ] |
Recipe 9.4 Improving Paging PerformanceProblemGiven an application that allows the user to page through a large result set in a data grid, you need to improve the performance of the paging. SolutionBuild a custom paging solution that overcomes the performance limitations of the overloaded Fill( ) method of the DataAdapter. The sample uses a single stored procedure, which is shown in Example 9-5:
Example 9-5. Stored procedure: SP0904_PageOrdersALTER PROCEDURE SP0904_PageOrders @PageCommand nvarchar(10), @FirstOrderId int = null, @LastOrderId int = null, @PageCount int output, @CurrentPage int output AS SET NOCOUNT ON select @PageCount = CEILING(COUNT(*)/10) from Orders -- first page is requested or previous page when the current -- page is already the first if @PageCommand = 'FIRST' or (@PageCommand = 'PREVIOUS' and @CurrentPage <= 1) begin select top 10 * from Orders order by OrderID set @CurrentPage = 1 return 0 end -- last page is requested or next page when the current -- page is already the last if @PageCommand = 'LAST' or (@PageCommand = 'NEXT' and @CurrentPage >= @PageCount) begin select a.* from (select TOP 10 * from orders order by OrderID desc) a order by OrderID set @CurrentPage = @PageCount return 0 end if @PageCommand = 'NEXT' begin select TOP 10 * from Orders where OrderID > @LastOrderId order by OrderID set @CurrentPage = @CurrentPage+1 return 0 end if @PageCommand = 'PREVIOUS' begin select a.* from ( select TOP 10 * from Orders where OrderId < @FirstOrderId order by OrderID desc) a order by OrderID set @CurrentPage = @CurrentPage-1 return 0 end if @PageCommand = 'GOTO' begin if @CurrentPage < 1 set @CurrentPage = 1 else if @CurrentPage > @PageCount set @CurrentPage = @PageCount declare @RowCount int set @RowCount = (@CurrentPage * 10) exec ('select * from (select top 10 a.* from (select top ' + @RowCount + ' * from Orders order by OrderID) a order by OrderID desc) b order by OrderID') return 0 end return 1 The sample code contains six event handlers and a single method:
The C# code is shown in Example 9-6. Example 9-6. File: ImprovePagingPerformanceForm.cs// Namespaces, variables, and constants using System; using System.Configuration; using System.Windows.Forms; using System.Data; using System.Data.SqlClient; private SqlDataAdapter da; private DataTable table; // Stored procedure name constants public const String PAGING_SP = "SP0904_PageOrders"; // Field name constants private const String ORDERID_FIELD = "OrderID"; private int currentPage; private int firstOrderId; private int lastOrderId; // . . . private void ImprovePagingPerformanceForm_Load(object sender, System.EventArgs e) { // Get the schema for the Orders table. da = new SqlDataAdapter("SELECT * FROM Orders", ConfigurationSettings.AppSettings["Sql_ConnectString"]); table = new DataTable("Orders"); da.FillSchema(table, SchemaType.Source); // Set up the paging stored procedure. SqlCommand cmd = new SqlCommand( ); cmd.CommandText = PAGING_SP; cmd.Connection = new SqlConnection( ConfigurationSettings.AppSettings["Sql_ConnectString"]); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add("@PageCommand", SqlDbType.NVarChar, 10); cmd.Parameters.Add("@FirstOrderId", SqlDbType.Int); cmd.Parameters.Add("@LastOrderId", SqlDbType.Int); cmd.Parameters.Add("@PageCount", SqlDbType.Int).Direction = ParameterDirection.Output; cmd.Parameters.Add("@CurrentPage", SqlDbType.Int).Direction = ParameterDirection.InputOutput; da = new SqlDataAdapter(cmd); // Get the first page of records. GetData("FIRST"); dataGrid.DataSource = table.DefaultView; } public void GetData(string pageCommand) { da.SelectCommand.Parameters["@PageCommand"].Value = pageCommand; da.SelectCommand.Parameters["@FirstOrderId"].Value = firstOrderId; da.SelectCommand.Parameters["@LastOrderId"].Value = lastOrderId; da.SelectCommand.Parameters["@CurrentPage"].Value = currentPage; table.Clear( ); da.Fill(table); if(table.Rows.Count > 0) { firstOrderId = (int)table.Rows[0][ORDERID_FIELD]; lastOrderId = (int)table.Rows[table.Rows.Count - 1][ORDERID_FIELD]; } else firstOrderId = lastOrderId = -1; int pageCount = (int)da.SelectCommand.Parameters["@PageCount"].Value; currentPage = (int)da.SelectCommand.Parameters["@CurrentPage"].Value; dataGrid.CaptionText = "Orders: Page " + currentPage + " of " + pageCount; } private void previousButton_Click(object sender, EventArgs args) { GetData("PREVIOUS"); } private void nextButton_Click(object sender, EventArgs args) { GetData("NEXT"); } private void firstButton_Click(object sender, System.EventArgs e) { GetData("FIRST"); } private void lastButton_Click(object sender, System.EventArgs e) { GetData("LAST"); } private void gotoPageButton_Click(object sender, System.EventArgs e) { try { currentPage = Convert.ToInt32(gotoPageTextBox.Text); } catch(Exception ex) { MessageBox.Show(ex.Message, "Improving Paging Performance", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } GetData("GOTO"); } DiscussionOverloads of the Fill( ) method of the DataAdapter allow a subset of data to be returned from a query by specifying the starting record and the number of records to return as arguments. This method should be avoided for paging through result sets—especially large ones—because it retrieves the entire result set for the query and subsequently discards the records outside of the specified range. Resources are used to process the entire result set instead of just the subset of required records. The solution shows how to create a stored procedure that will return a result set corresponding to a page of data from a larger result set. The TOP and WHERE clauses are used together with the primary key (any unique identifier would do) and the sort order. This allows first, last, next, and previous paging. The goto paging is done by nesting SELECT TOP n statements with alternate ascending and descending sorts to get the subset of the records for the page specified. The goto select statement uses a dynamic SQL statement executed using the T-SQL EXEC command. This allows a variable number of TOP n records to be selected within the statement. The EXEC command could also be used to dynamically calculate the top records for all statements so that the number of records per page could be supplied as an input parameter to the stored procedure rather than hardcoded. |
[ Team LiB ] |