Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

The Database Forum


You are currently viewing our The Database Forum as a guest. Please register to participate.
Login



Reply
Which one is better: selecting in one query or more ?
Old 03-05-2008, 11:49 AM Which one is better: selecting in one query or more ?
Novice Talker

Posts: 7
Name: serkan
Trades: 0
Hi everybody,
I have a problem with selecting using 2 tables here is the definition:

Table1: forum_topics (id_topic,subject ,topic_date)
Table2: forum_posts (id_post,id_topic,id_user,post,post_date)

as you can guess table 1 keeps the topics and table 2 contains the posts for topics.

Now I want to display last 10 topics on homepage with following data:
- name of topic,
- the id_user who posted last to the topic
and want to order them in a way that the topic which has the last post will be displayed at top.

is it posibble to make all with one query. Or is it a better way to handle it with php? Which one is faster? which one is reliable?

If it is posible to make it with one query how can I do this. I tried some querries but cant order it by last post

Thanks for your help.
tutunmayan is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 03-05-2008, 12:00 PM Re: Which one is better: selecting in one query or more ?
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
It depends a lot of your indexes...
If your post table grows really big, the order by will become time consumming (I had to extract datas from a 3Go table once, and the order_by was crashing the server).

Otherwise, a simple left join would do the job nicely.
Code:
select 
  t.id_topic, t.subject, 
  p.id_user, p.id_post, p.post_date
from 
  forum_topics t, 
  forum_posts p
where t.topic_id=p.topic_id
order by p.post_date desc
limit 10 offset 0
Note that this query is ok for mysql/pgsql, but will need to be adapted for other db.
__________________
Only a biker knows why a dog sticks his head out the window.

Last edited by tripy; 03-05-2008 at 12:02 PM..
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 03-05-2008, 12:16 PM Re: Which one is better: selecting in one query or more ?
Novice Talker

Posts: 7
Name: serkan
Trades: 0
Thank you sooo much tripy,
query works fine but I need to group by id_topic because I want to display each topic once.
When I try grouping it, result don't come up with id_user who posted last to the topic

I know it is possible to get last poster with a new query in php while statement but this means making the same thing with 11 queries instead of one. I think this is not the best way.

Another way I can think is to push redundant data to forum_topics. (a new field like last_poster_id and updating it for each post). But I know this also is not a good approach.

Any ideas??
tutunmayan is offline
Reply With Quote
View Public Profile
 
Old 03-06-2008, 02:22 AM Re: Which one is better: selecting in one query or more ?
Ultra Talker

Posts: 310
Trades: 0
Here's how i'd do it:

Code:
 SELECT ft.subject, ft.topic_date, fp.id_post, fp.id_user, fp.post, fp.post_date     
   FROM forum_topics ft, forum_posts fp   
 WHERE ft.id_topic = fp.id_topic AND fp.id_post = 
                                            ( SELECT id_post 
                                              FROM forum_posts 
                                              WHERE id_topic = ft.id_topic 
                                              ORDER BY post_date DESC 
                                              LIMIT 0, 1 ) 
ORDER BY fp.post_date desc;
One more advice, use timestamp data type for post_date and topic_date fields.
dman_2007 is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Which one is better: selecting in one query or more ?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.53477 seconds with 12 queries