Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old August 7th, 2007, 07:46 AM
Friend of Wrox
 
Join Date: Mar 2007
Location: Creetown, UK
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
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 10:53 PM.


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