Hi PPL,Below a stored procedure I use for retrieving forum messages in a paged fashion, works great, but it's still rather slow for a popular forum. I've decreased the TOP argument from 400 to 20, but that made no big changes. Anyone got a better idea? CREATE PROCEDURE sp_pagedMessages ( @Page int, @RecsPerPage int, @Forum int, @SortOrder int )AS-- We don't want to return the # of rows inserted-- into our temporary table, so turn NOCOUNT ONSET NOCOUNT ON--Create a temporary tableCREATE TABLE #TempPagedMessages( [id] [numeric](18, 0) IDENTITY (1, 1) NOT NULL , [bericht] [numeric](18, 0), [onderwerp] [varchar] (50) NULL , [naam] [varchar](50) NULL , [email] [varchar](50) NULL , [verstuurd] [datetime] NULL , [forum] [numeric](18, 0) NULL , [start_bericht] [numeric](18, 0) NULL , [bekeken][int] NULL , [laatste_post][datetime] NULL, [gesloten][bit] NULL, [threads][int] NULL)-- Insert the rows from tblItems into the temp. tableIF @SortOrder = 0 INSERT INTO #TempPagedMessages (bericht, onderwerp, naam, email, verstuurd, forum, start_bericht, bekeken, laatste_post, gesloten, threads) SELECT TOP 400 bericht, onderwerp, naam, email, verstuurd, forum, start_bericht, bekeken, laatste_post, gesloten, threads = (SELECT count(*) AS aantal FROM berichten AS B WHERE b.start_bericht = c.bericht) FROM berichten AS C WHERE reactie_op IS NULL AND forum = @Forum ORDER BY verstuurd DESCElse IF @SortOrder = 1 INSERT INTO #TempPagedMessages (bericht, onderwerp, naam, email, verstuurd, forum, start_bericht, bekeken, laatste_post, gesloten, threads) SELECT TOP 400 bericht, onderwerp, naam, email, verstuurd, forum, start_bericht, bekeken, laatste_post, gesloten, threads = (SELECT count(*) AS aantal FROM berichten AS B WHERE b.start_bericht = c.bericht) FROM berichten AS C WHERE reactie_op IS NULL AND forum = @Forum ORDER BY laatste_post DESC -- Find out the first and last record we wantDECLARE @FirstRec int, @LastRec intSELECT @FirstRec = (@Page - 1) * @RecsPerPageSELECT @LastRec = (@Page * @RecsPerPage + 1)-- Now, return the set of paged records, plus, an indiciation of we-- have more records or not!SELECT *, MoreRecords = ( SELECT COUNT(*) FROM #TempPagedMessages TI WHERE TI.ID >= @LastRec ) , TotalThreads = (SELECT COUNT(*) FROM berichten WHERE forum = @Forum AND start_bericht IS NULL), TotalPosts = (SELECT COUNT(*) FROM berichten WHERE forum = @Forum AND start_bericht IS NOT NULL)FROM #TempPagedMessagesWHERE ID > @FirstRec AND ID < @LastRec-- Turn NOCOUNT back OFFSET NOCOUNT OFFGO
Edited by - fizgig on 07/16/2002 03:47:12