Snitz Forums 2000
Snitz Forums 2000
Home | Profile | Register | Active Topics | Members | Search | FAQ
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 Community Forums
 Code Support: ASP (Non-Forum Related)
 Make query efficient?
 New Topic  Topic Locked
 Printer Friendly
Author Previous Topic Topic Next Topic  

sr_erick
Senior Member

USA
1318 Posts

Posted - 11 November 2004 :  23:19:31  Show Profile  Visit sr_erick's Homepage  Send sr_erick a Yahoo! Message
Is there any way to make this query more efficient? I think most of the lag comes from the order by clause. I want to display latest topics but this is being just way too slow. Any ways I can speed it up? It is according to last replied to topic. Thanks!

SELECT TOP 5 FORUM_TOPICS.TOPIC_ID, FORUM_TOPICS.T_SUBJECT, FORUM_TOPICS.T_AUTHOR, FORUM_TOPICS.T_REPLIES, FORUM_MEMBERS.M_NAME, FORUM_TOPICS.T_LAST_POST_AUTHOR, FORUM_TOPICS.T_LAST_POST, MEMBERS_1.M_NAME AS LAST_POST_AUTHOR_NAME, FORUM_TOPICS.T_MESSAGE FROM FORUM_MEMBERS, FORUM_FORUM, FORUM_TOPICS, FORUM_MEMBERS AS MEMBERS_1 WHERE FORUM_TOPICS.T_LAST_POST_AUTHOR = MEMBERS_1.MEMBER_ID AND FORUM_FORUM.FORUM_ID = FORUM_TOPICS.FORUM_ID AND FORUM_MEMBERS.MEMBER_ID = FORUM_TOPICS.T_AUTHOR ORDER BY FORUM_TOPICS.T_LAST_POST DESC;




Erick
Snowmobile Fanatics


Edited by - sr_erick on 11 November 2004 23:19:59

pdrg
Support Moderator

United Kingdom
2897 Posts

Posted - 12 November 2004 :  05:14:00  Show Profile  Send pdrg a Yahoo! Message
you could try experimenting with something like (pseudocode to follow)

select <all my fields> from <mytables> where <join conditions, although I prefer these in the from sub-clause> and FORUM_TOPICS.TOPIC_ID IN (SELECT top 5 TOPIC_ID from FORUM_TOPICS order by T_LAST_POST DESC) order by T_LAST_POST DESC

so you're only pulling on all the fields when you've identified the subset of records you want!

may help out

(edit...)
if this is SQL Server 7, we could also probably do some execution plan optimisations using 'Hints' (see BOL)

Edited by - pdrg on 12 November 2004 05:16:36
Go to Top of Page

dayve
Forum Moderator

USA
5820 Posts

Posted - 12 November 2004 :  11:37:37  Show Profile  Visit dayve's Homepage
Depending on the results you are displaying on the screen will help us "cut the fat". What information do you need to have displayed as the ending result?

Go to Top of Page

sr_erick
Senior Member

USA
1318 Posts

Posted - 12 November 2004 :  14:08:08  Show Profile  Visit sr_erick's Homepage  Send sr_erick a Yahoo! Message
Topic Author (name and ID)
Subject
Date
Reply count
Topic Message

Probably don't need the last post author




Erick
Snowmobile Fanatics


Edited by - sr_erick on 12 November 2004 14:09:08
Go to Top of Page

dayve
Forum Moderator

USA
5820 Posts

Posted - 12 November 2004 :  14:58:49  Show Profile  Visit dayve's Homepage
Then you should only need 2 tables with this query.

SELECT TOP 5 FORUM_TOPICS.TOPIC_ID, FORUM_TOPICS.T_SUBJECT, FORUM_TOPICS.T_REPLIES, FORUM_TOPICS.T_DATE, FORUM_MEMBERS.M_NAME
FROM FORUM_TOPICS INNER JOIN FORUM_MEMBERS ON FORUM_TOPICS.T_AUTHOR = FORUM_MEMBERS.MEMBER_ID
ORDER BY FORUM_TOPICS.T_DATE DESC;

Go to Top of Page

dayve
Forum Moderator

USA
5820 Posts

Posted - 12 November 2004 :  15:01:56  Show Profile  Visit dayve's Homepage
wooops, that won't hide private topics though. errr, let me try that again in a minute.

Go to Top of Page

MarcelG
Retired Support Moderator

Netherlands
2625 Posts

Posted - 12 November 2004 :  15:14:07  Show Profile  Visit MarcelG's Homepage
erick, have you had a look at my sidebar.asp ? It's on your server, so go ahead

portfolio - linkshrinker - oxle - twitter
Go to Top of Page

sr_erick
Senior Member

USA
1318 Posts

Posted - 10 January 2005 :  20:09:13  Show Profile  Visit sr_erick's Homepage  Send sr_erick a Yahoo! Message
Sorry to bring this back up again. I've revisited the idea. There has to be a more efficient way. I need like say, the latest 5 replied to topics and who replied, reply date, topic title, etc. What's the absolutly most efficient way to get this?




Erick
Snowmobile Fanatics

Go to Top of Page

HuwR
Forum Admin

United Kingdom
20584 Posts

Posted - 11 January 2005 :  04:06:35  Show Profile  Visit HuwR's Homepage
SELECT TOP 5 FORUM_TOPICS.T_SUBJECT, FORUM_TOPICS.T_LAST_POST, FORUM_MEMBERS.M_NAME
FROM (FORUM_TOPICS INNER JOIN FORUM_MEMBERS ON FORUM_TOPICS.T_LAST_POST_AUTHOR = FORUM_MEMBERS.MEMBER_ID) INNER JOIN FORUM_FORUM ON FORUM_TOPICS.FORUM_ID = FORUM_FORUM.FORUM_ID
WHERE FORUM_FORUM.F_PRIVATEFORUMS=0
ORDER BY FORUM_TOPICS.T_LAST_POST DESC

This will give you
Topic Subject
Date of last reply
Author of last reply

If you wanted the original topic author aswell, then you will need to aadd a join to a second copy of member table. If you state exactly what you want I will redo for you.

From your several posts, it is not obvious what you actually want to return from your query
Go to Top of Page

HuwR
Forum Admin

United Kingdom
20584 Posts

Posted - 11 January 2005 :  04:08:58  Show Profile  Visit HuwR's Homepage
The initial query posted is about as efficient as it can be to return all the post info, it may be that your database needs better indexes
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Topic Locked
 Printer Friendly
Jump To:
Snitz Forums 2000 © 2000-2021 Snitz™ Communications Go To Top Of Page
This page was generated in 0.4 seconds. Powered By: Snitz Forums 2000 Version 3.4.07