Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Optimizing Query Challange

Author  Topic 

fizgig
Starting Member

34 Posts

Posted - 2002-07-16 : 03:43:35
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 ON
SET NOCOUNT ON
--Create a temporary table
CREATE 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. table

IF @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 DESC
Else
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 want
DECLARE @FirstRec int, @LastRec int
SELECT @FirstRec = (@Page - 1) * @RecsPerPage
SELECT @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 #TempPagedMessages
WHERE ID > @FirstRec AND ID < @LastRec
-- Turn NOCOUNT back OFF
SET NOCOUNT OFF
GO





Edited by - fizgig on 07/16/2002 03:47:12

royv
Constraint Violating Yak Guru

455 Posts

Posted - 2002-07-16 : 07:30:29
If you are using SQL Server 2000, the first thing is you can use the table variable instead of a temp table

*************************
Someone done told you wrong!
Go to Top of Page

smccreadie
Aged Yak Warrior

505 Posts

Posted - 2002-07-16 : 07:35:17
At the risk of stating the obvious, make sure your permanent tables are indexed optimally.

If you're still having performance issues, look at it in profiler and where the bottleneck is.


Go to Top of Page
   

- Advertisement -