Forum Moderators: coopster

Message Too Old, No Replies

better query?

Is it possible to format the follwing query to make it faster?

         

enotalone

6:25 pm on Dec 31, 2003 (gmt 0)

10+ Year Member



reall a mysql question i guess, but used in php

SELECT topic_id, topic_title, topic_poster, topic_type, topic_status, username, topic_replies, topic_views, bb_topics.forum_id, forum_name, post_text FROM bb_topics, bb_users, bb_posts_text, bb_forums WHERE topic_status!='2' and topic_type!='2' and bb_topics.forum_id!='640' and bb_topics.topic_poster=bb_users.user_id and bb_topics.forum_id= bb_forums.forum_id and bb_posts_text.post_id=bb_topics.topic_first_post_id ORDER BY topic_type DESC, topic_id DESC LIMIT 20";

volatilegx

10:20 pm on Dec 31, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The only way I can think of is to add indexes to the conditional fields, i.e:

topic_status
topic_type
bb_topics.forum_id
bb_topics.topic_poster
bb_topics.forum_id
bb_posts_text.post_id
bb_users.user_id
bb_forums.forum_id
bb_topics.topic_first_post_id

and possibly the topic_id field as well, since it is in one of the ORDER BY parameters. Additional indexes will increase the size of the database, but will make searching faster.

daosmith

1:49 pm on Jan 1, 2004 (gmt 0)

10+ Year Member



I'm not sure if it makes the query any faster but it is conceptually clearer if you use a natural join instead of 'from tbl_1, tbl_2 ... where tbl_1.key = tbl_2.key' e.g.
you can say -

SELECT attr_1, attr_2, attr_3...
FROM tbl_1 NATURAL JOIN tbl_2

- and it will have the same effect as if you used the 'from' and 'where' clauses as above. I think this is only really useful for one of your comparisons as the others have differing key names but it does make it a bit more manageable.

I seem to remember there are custom joins as well that allow you to specify the attributes to join on but the syntax eludes me...