Wrox Programmer Forums
|
BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0
This is the forum to discuss the Wrox book ASP.NET 2.0 Website Programming: Problem - Design - Solution by Marco Bellinaso; ISBN: 9780764584640
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 7th, 2007, 07:46 AM
Friend of Wrox
 
Join Date: Mar 2007
Posts: 488
Thanks: 2
Thanked 11 Times in 10 Posts
Default TIP: Optimization of Row_Number queries

Just a quick little one here. When doing Row_Number() paging of data, it's advantageous to include the TOP() function in the sql as well, as we 'know' the depth of the data that we are going to be querying. So, if we look at the query in the DAL\SQLClient\SqlForumsProvider\GetThreads method, we see the following query:

string sql = string.Format(@"SELECT * FROM
(
SELECT tbh_Posts.PostID, tbh_Posts.AddedDate, tbh_Posts.AddedBy, tbh_Posts.AddedByIP,
tbh_Posts.ForumID, tbh_Posts.ParentPostID, tbh_Posts.Title, tbh_Posts.Approved,
tbh_Posts.Closed, tbh_Posts.ViewCount, tbh_Posts.ReplyCount, tbh_Posts.LastPostDate,
tbh_Posts.LastPostBy, tbh_Forums.Title AS ForumTitle, tbh_Posts.Priority, 0 as Rating,
     ROW_NUMBER() OVER (ORDER BY {0}) AS RowNum
     FROM tbh_Posts INNER JOIN tbh_Forums ON tbh_Posts.ForumID = tbh_Forums.ForumID
     WHERE ParentPostID = 0 AND Approved = 1
) ForumThreads
WHERE ForumThreads.RowNum BETWEEN {1} AND {2}
ORDER BY RowNum ASC", sortExpression, lowerBound, upperBound);

This can be changed to (new addition in RED):

string sql = string.Format(@"SELECT * FROM
(
SELECT TOP {2} tbh_Posts.PostID, tbh_Posts.AddedDate, tbh_Posts.AddedBy, tbh_Posts.AddedByIP,
tbh_Posts.ForumID, tbh_Posts.ParentPostID, tbh_Posts.Title, tbh_Posts.Approved,
tbh_Posts.Closed, tbh_Posts.ViewCount, tbh_Posts.ReplyCount, tbh_Posts.LastPostDate,
tbh_Posts.LastPostBy, tbh_Forums.Title AS ForumTitle, tbh_Posts.Priority, 0 as Rating,
     ROW_NUMBER() OVER (ORDER BY {0}) AS RowNum
     FROM tbh_Posts INNER JOIN tbh_Forums ON tbh_Posts.ForumID = tbh_Forums.ForumID
     WHERE ParentPostID = 0 AND Approved = 1
) ForumThreads
WHERE ForumThreads.RowNum BETWEEN {1} AND {2}
ORDER BY RowNum ASC", sortExpression, lowerBound, upperBound);

The format function uses param {2} and plugs in the maximum rows to be queried. THe same can be done in any of the stored procedures. For example, the tbh_Articles_GetArticles would have the following line amended:

SELECT * FROM
(
    SELECT tbh_Articles.ArticleID,
// ..etc...

to:

SELECT * FROM
(
    SELECT TOP ((@PageIndex+1)*@PageSize) tbh_Articles.ArticleID,
// ..etc...

Hope this is helpful. Obviously, in small datasets (of under a few thousand rows), this effect is negligible. In large datasets, the benefits are more profound.




jimi

http://www.jamestollan.com
__________________
jimi

http://www.originaltalent.com





Similar Threads
Thread Thread Starter Forum Replies Last Post
Python code optimization icopec Python 2 December 13th, 2011 05:13 AM
Optimization akkad C# 5 November 1st, 2006 05:27 AM
Combining Queries or results from 2 queries Ford SQL Server 2000 24 November 7th, 2005 08:54 PM
Pass-Through Query Optimization redrobot5050 Access VBA 3 June 18th, 2004 01:25 AM
Query optimization SubodhKumar SQL Language 1 October 22nd, 2003 06:35 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.