Small width layout Medium width layout Maximum width layout Small text Medium text Large text
     Search
Downloads Downloads Directory Directory Forums Forums Forge Forge Blogs Blogs        Marketplace Marketplace Careers Program Careers
Community › Forums Register  |  

DotNetNuke Marketplace
  Ads  
Engage Software - Training Partner for DotNetNuke
 


  Sponsors  

Meet Our Sponsors

DataSprings - Great Ideas. Always Flowing.
R2integrated - formerly bi4ce
Jango Studios - Skins, Modules and Hosting for DotNetNuke
eUKhost.com is commited to offer exceptional UK Windows Web Hosting solutions with quality 24x7 technical support.Our plans support ASP.Net, ASP, ASP.NET Ajax extensions, XML, MSSQL, MySQL, PHP,DNN, multiple domains and Shared SSL as standard.
SmarterTools
Verndale
 


DotNetNuke Forums
 
  Forum  DotNetNuke® Pro...  Forum Module [L...  Turning: Stored Procedure 'Forum_SearchGetResults' in Forum 4.4.3
Previous Previous
 
Next Next
New Post 7/8/2008 6:25 PM
User is offline Yoshihiro Kawabata
4 posts
10th Ranked


Turning: Stored Procedure 'Forum_SearchGetResults' in Forum 4.4.3 

Hello, I turning Stored Procedure 'Forum_SearchGetResults' in Forum 4.4.3.

Turning Point:
  rewrite EXEC(@sql)  to sp_executesql @sql with parameters.

Our Result:
  6 times fast, 40% less reads of keyword search on our forum ( 11,000 posts, 2,300 threads, 33 forums).

New Source:
ALTER PROCEDURE [dbo].[Forum_SearchGetResults]
(
    @Filter  NVARCHAR(500),
    @PageIndex INT,
    @PageSize INT,
    @UserID  INT,
    @ModuleID INT,
    @FromDate DATETIME,
    @ToDate DATETIME,
    @ThreadStatusID INT
)
AS
    -- Create a temp table to store the select results
 CREATE TABLE #PageIndex
    (
     IndexID INT IDENTITY (1, 1) NOT NULL,
     ThreadID INT
    )
    -- Create dynamic SQL to populate temporary table
 DECLARE @sql NVARCHAR(3000)

--  2008/7/9 : Original
-- SELECT  @sql =    'INSERT INTO #PageIndex(ThreadID) SELECT DISTINCT(P.ThreadID) CreatedDate ' +    
--                'FROM dbo.Forum_Posts P ' +
--                'INNER JOIN dbo.Forum_Threads T ON P.ThreadID = T.ThreadID ' +
--                'INNER JOIN dbo.Forum_Forums F ON T.ForumID = F.ForumID ' +
--                'INNER JOIN dbo.Forum_Groups G ON F.GroupID = G.GroupID ' +       
--                'INNER JOIN dbo.Forum_Posts LP ON T.ThreadID = LP.ThreadID ' +
--                'WHERE P.ThreadID = T.ThreadID AND ' +
--                'T.ForumID = F.ForumID AND ' +       
--                'F.GroupID = G.GroupID ' +
--                'AND P.[CreatedDate] >  ''' + CONVERT(VARCHAR, @FromDate) + ''' ' +
--                'AND P.[CreatedDate] < ''' + CONVERT(VARCHAR, @ToDate) + ''' ' +
--                'AND P.IsApproved = 1 ' +
--                'AND F.IsActive = 1 ' +
--                'AND P.IsLocked = 0 ' +
--                'AND G.ModuleID = ' + CONVERT(VARCHAR, @ModuleID) +
--                'AND ( (F.EnableForumsThreadStatus = 0) ' +
--                'OR (F.EnableForumsThreadStatus = 1 AND T.ThreadStatus = ' + CONVERT(VARCHAR, @ThreadStatusID) + ') ' +
--                'OR (F.EnableForumsThreadStatus = 1 AND -1 = ' + CONVERT(VARCHAR, @ThreadStatusID) + ') ) ' +
--                'AND ( F.PublicView = 1 OR ' +
--                    ' F.ForumID IN ( ' +
--                        ' SELECT ForumID ' +
--                        ' FROM  dbo.Forum_ForumPermission ' +
--                        ' WHERE AllowAccess=1 AND (RoleID IN (SELECT RoleID FROM dbo.UserRoles WHERE UserID = ' + CONVERT(VARCHAR, @UserID) + ' )' +
--                        ' OR EXISTS (SELECT TOP 1 1 FROM dbo.Users WHERE UserID = ' + CONVERT(VARCHAR, @UserID) + '  and IsSuperUser=1))))  ' +
--                @Filter + ' GROUP BY P.ThreadID , P.CreatedDate ORDER BY P.ThreadID DESC '
-- EXEC(@sql)
-- ]

-- [ 2008/7/9 NEW
     SELECT  @sql =    'INSERT INTO #PageIndex(ThreadID) SELECT DISTINCT(P.ThreadID) CreatedDate ' +    
                'FROM dbo.Forum_Posts P ' +
                'INNER JOIN dbo.Forum_Threads T ON P.ThreadID = T.ThreadID ' +
                'INNER JOIN dbo.Forum_Forums F ON T.ForumID = F.ForumID ' +
                'INNER JOIN dbo.Forum_Groups G ON F.GroupID = G.GroupID ' +       
                'INNER JOIN dbo.Forum_Posts LP ON T.ThreadID = LP.ThreadID ' +
                'WHERE P.ThreadID = T.ThreadID AND ' +
                'T.ForumID = F.ForumID AND ' +       
                'F.GroupID = G.GroupID ' +
                'AND P.[CreatedDate] > @FromDate ' +
                'AND P.[CreatedDate] < @ToDate ' +
                'AND P.IsApproved = 1 ' +
                'AND F.IsActive = 1 ' +
                'AND P.IsLocked = 0 ' +
                'AND G.ModuleID = @ModuleID ' +
                'AND ( (F.EnableForumsThreadStatus = 0) ' +
                'OR (F.EnableForumsThreadStatus = 1 AND T.ThreadStatus = @ThreadStatusID) ' +
                'OR (F.EnableForumsThreadStatus = 1 AND -1 = @ThreadStatusID)) ' +
                'AND ( F.PublicView = 1 OR ' +
                    ' F.ForumID IN ( ' +
                        ' SELECT ForumID ' +
                        ' FROM  dbo.Forum_ForumPermission ' +
                        ' WHERE AllowAccess=1 AND (RoleID IN (SELECT RoleID FROM dbo.UserRoles WHERE UserID = @UserID )' +
                        ' OR EXISTS (SELECT TOP 1 1 FROM dbo.Users WHERE UserID = @UserID  and IsSuperUser=1))))  ' +
                @Filter +
                ' GROUP BY P.ThreadID , P.CreatedDate ' +
                ' ORDER BY P.ThreadID DESC '

 EXEC sp_executesql @sql,
     N' @FromDate DATETIME, @ToDate Datetime, @ModuleID int, @ThreadStatusID int, @UserID int',
        @FromDate, @ToDate, @ModuleID, @ThreadStatusID, @UserID
-- ]

    -- All of the rows are inserted into the table - now select the correct subset
 DECLARE @PageLowerBound INT
 DECLARE @PageUpperBound INT
 DECLARE @RecordCount INT
    -- Set the page bounds
 SET @PageLowerBound = @PageSize * @PageIndex
 SET @PageUpperBound = @PageLowerBound + @PageSize + 1
 SELECT @RecordCount = COUNT(*) FROM #PageIndex

    -- Select the data out of the temporary table
 SELECT
     PageIndex.ThreadID,
     P.[Subject],
     P.[CreatedDate],
     T.[ForumID],
     T.[Replies],
     T.[Views],
     F.[Name] AS ForumName,
     RecordCount = @RecordCount,
     T.IsPinned,
     T.LastPostedPostID,
     T.ThreadStatus,
     ISNULL((SELECT COUNT([ThreadID]) FROM dbo.Forum_ThreadRating  WHERE ThreadID = PageIndex.ThreadID), 0) AS RatingCount,
     ISNULL((SELECT AVG([Rate]) FROM dbo.Forum_ThreadRating  WHERE ThreadID = PageIndex.ThreadID), 0) AS Rating
 FROM
     dbo.Forum_Posts P
     INNER JOIN dbo.Forum_Threads T ON P.ThreadID = T.ThreadID
     INNER JOIN dbo.Forum_Forums F ON T.ForumID = F.ForumID
     INNER JOIN #PageIndex PageIndex ON T.ThreadID = PageIndex.ThreadID
 WHERE
     PageIndex.IndexID > @PageLowerBound AND
     PageIndex.IndexID < @PageUpperBound AND
     PageIndex.ThreadID = P.ThreadID AND 
     P.[ThreadID] = T.[ThreadID] AND
     P.[CreatedDate] > @FromDate AND
     P.[CreatedDate] < @ToDate AND
     T.[ForumID] = F.[ForumID] AND
     F.[IsActive] = 1 AND
        (P.[CreatedDate] = (SELECT MAX([CreatedDate]) FROM dbo.Forum_Posts WHERE ThreadID = PageIndex.ThreadID)) AND
        ( (F.EnableForumsThreadStatus = 0) OR
        (F.EnableForumsThreadStatus = 1 AND T.ThreadStatus = @ThreadStatusID) OR
        (F.EnableForumsThreadStatus = 1 AND -1 = @ThreadStatusID) ) AND
        (F.PublicView = 1 OR
     F.ForumID IN (
         SELECT ForumID
         FROM dbo.Forum_ForumPermission
         WHERE AllowAccess=1 AND (RoleID IN (SELECT RoleID FROM dbo.UserRoles WHERE UserID = @UserID)
         OR EXISTS (SELECT TOP 1 1 FROM dbo.Users WHERE UserID = @UserID AND IsSuperUser=1))))    
 ORDER BY
     P.CreatedDate DESC
----

Regards,

 
New Post 7/9/2008 2:03 PM
User is offline Matthias Schlomann
1507 posts
www.taunusstein.org
5th Ranked








Re: Turning: Stored Procedure 'Forum_SearchGetResults' in Forum 4.4.3 

Is this a correction or ehancement of your first post?


Regards
Matthias
www.moonlightradio.net
Chat with me via MSN
 
New Post 7/9/2008 6:37 PM
User is offline Crispy
3433 posts
www.apptheory.com
4th Ranked










Re: Turning: Stored Procedure 'Forum_SearchGetResults' in Forum 4.4.3 

Yosh, I have rewritten the sproc because what is there for the initial select is wrong. I did include the "rewrite EXEC(@sql)  to sp_executesql @sql with parameters" but without the parameters because I am still doing the convert in the select @sql statement. I did some profiling and found no difference but the sp_executesql is faster in theory.

 

ALTER

PROCEDURE [dbo].[dnn_Forum_SearchGetResults]

(

@Filter

NVARCHAR(500),

@PageIndex

INT,

@PageSize

INT,

@UserID

INT,

@ModuleID

INT,

@FromDate

DATETIME,

@ToDate

DATETIME,

@ThreadStatusID

INT

)

AS

 

-- Create a temp table to store the select results

 

 

CREATE TABLE #PageIndex(

IndexID

INT IDENTITY (1, 1) NOT NULL,

CreatedDate

DATETIME NOT NULL,

ThreadID

INT

 

)

 

-- Create dynamic SQL to populate temporary table

 

DECLARE @sql NVARCHAR(2000)

 

 

 

 

 

 

 

 

SELECT @sql = 'INSERT INTO #PageIndex(ThreadID, CreatedDate) SELECT (ThreadID), ' +'(SELECT CreatedDate FROM dbo.dnn_Forum_Threads FT INNER JOIN dbo.dnn_Forum_Posts FP ' +'ON FT.LastPostedPostID = FP.PostID ' +'WHERE FT.ThreadID = PS.ThreadID) As LastPostDate ' +'FROM dbo.dnn_Forum_vw_PostSearch PS ' +'WHERE [CreatedDate] > ''' + CONVERT(VARCHAR, @FromDate) + ''' ' +'AND [CreatedDate] < ''' + CONVERT(VARCHAR, @ToDate) + ''' ' +'AND IsApproved = 1 ' +

 

'AND IsActive = 1 ' +

 

'AND ModuleID = ' + CONVERT(VARCHAR, @ModuleID) +

 

 

 

 

'AND ( (EnableForumsThreadStatus = 0) ' +'OR (EnableForumsThreadStatus = 1 AND ThreadStatus = ' + CONVERT(VARCHAR, @ThreadStatusID) + ') ' +'OR (EnableForumsThreadStatus = 1 AND -1 = ' + CONVERT(VARCHAR, @ThreadStatusID) + ') ) ' +'AND ( PublicView = 1 OR ' +

 

' ForumID IN ( ' +

 

' SELECT ForumID ' +

 

' FROM dbo.dnn_Forum_ForumPermission ' +

 

 

' WHERE AllowAccess=1 AND ' +'(RoleID IN (SELECT RoleID FROM dbo.dnn_UserRoles WHERE UserID = ' + CONVERT(VARCHAR, @UserID) + ' )' +

 

 

 

' OR EXISTS ' +'(SELECT TOP 1 1 FROM dbo.dnn_Users WHERE UserID = ' + CONVERT(VARCHAR, @UserID) +' AND IsSuperUser=1)))) ' +

@Filter

+ 'GROUP BY ThreadID ORDER BY LastPostDate DESC'

 

 

EXEC sp_executesql @sql-- All of the rows are inserted into the table - now select the correct subset

 

DECLARE @PageLowerBound INT

 

DECLARE @PageUpperBound INT

 

DECLARE @RecordCount INT

 

-- Set the page bounds

 

 

 

 

SET @PageLowerBound = @PageSize * @PageIndexSET @PageUpperBound = @PageLowerBound + @PageSize + 1-- Select the data out of the temporary table

 

SELECT

 

(I.ThreadID),

[Subject]

,

 

--[Name],

FP

.[CreatedDate],

T

.[ForumID],

[Replies]

,

[Views]

,

 

(SELECT COUNT(*) FROM #PageIndex) AS RecordCount,

IsPinned

,

LastPostedPostID

,

ThreadStatus

,

 

ISNULL((SELECT COUNT([ThreadID]) FROM dbo.dnn_Forum_ThreadRating WHERE ThreadID = I.ThreadID), 0) AS RatingCount,

 

 

 

 

 

ISNULL((SELECT AVG([Rate]) FROM dbo.dnn_Forum_ThreadRating WHERE ThreadID = I.ThreadID), 0) AS RatingFROM dbo.dnn_Forum_Threads TINNER JOIN #PageIndex I ON T.ThreadID = I.ThreadIDINNER JOIN dbo.dnn_Forum_Posts FP ON T.LastPostedPostID = FP.PostIDWHERE

I

.IndexID > @PageLowerBound AND

I

.IndexID < @PageUpperBound

ORDER

BY CreatedDate DESC



 
Previous Previous
 
Next Next
  Forum  DotNetNuke® Pro...  Forum Module [L...  Turning: Stored Procedure 'Forum_SearchGetResults' in Forum 4.4.3
 


Forum Policy

These Discussion Forums are dedicated to the discussion of the DotNetNuke Web Application Framework.

For the benefit of the community and to protect the integrity of the project, please observe the following posting guidelines:

1. No Advertising. This includes promotion of commercial and non-commercial products or services which are not directly related to DotNetNuke.
2. Discussion or promotion of DotNetNuke product releases under a different brand name are strictly prohibited.
3. No Flaming or Trolling.
4. No Profanity, Racism, or Prejudice.
5. Site Moderators have the final word on approving/removing a thread or post or comment.
6. English language posting only, please.

 


Cestus Websites
DotNetNuke websites en services in Nederland. Cestus Websites levert websites, projectmanagent, skins, modules, training en gespecialiseerde hosting op basis van het CMS DotNetNuke.
www.dotnetnuke-websites.nl
FREE Skins, Modules & Control Panel!
Choose how much you want to pay on our Skins, Modules, Containers and Control Panel - Starting from FREE!
www.dnngroup.com
Disgrafic
Diseño y creación de portales web dinámicos :: Desarrollo de módulos personalizados :: Soluciones a medida :: Imagen Corporativa :: Tu proveedor DNN en España
www.disgrafic.com

DotNetNuke Corporation   Terms Of Use  Privacy Statement
DotNetNuke®, DNN®, and the DotNetNuke logo are trademarks of DotNetNuke Corporation
Hosted by MaximumASP