I got 2 tables
First is where Topics are hold
Second is where Messages are hold.
I want to make a Recent Topics mod for my project. Its purpose to list most recent 30 topics..
the point is i can easly call
"select * from topics order by date desc limit 30;"
but i dont want to just list recent 30 topics i want to list updating recent 30 topics by messages.
alright let me tell you with a example whats on my mind ! :
lets say there are 5 topics and they are recent.
*Hello Whats UP? *Good Morning *Good Afternoon *Hello Nice! *There is a Bear behind you!I wrote a message to “There is a bear behind you” topic. the list must
updated to*There is a Bear behind you! *Hello Whats UP? *Good Morning *Good Afternoon *Hello Nice!
I hope you understand my example for what i have to do :/
What i have done so far is this code by searching stackoverflow :
SELECT DISTINCT a.* FROM topics a LEFT JOIN messages b ON a.id = b.topic_id ORDER BY b.date DESC LIMIT 30;
this query works like charm however my topics and messages tables are Freaking HUGE!
this is the explain
+----+-------------+-------+------+---------------+------+---------+-------------------+--------+---------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+-------------------+--------+---------------------------------+
| 1 | SIMPLE | a | ALL | NULL | NULL | NULL | NULL | 383056 | Using temporary; Using filesort |
| 1 | SIMPLE | b | ref | sira,sira_2 | sira | 4 | avare_sozluk.a.id | 6 | Distinct |
+----+-------------+-------+------+---------------+------+---------+-------------------+--------+---------------------------------+
OH! btw i used Distinct for there could be one ore more messages. So query can select the same topic twice or more.. I just want it to select for once to listing..
Anyone can help me with the performance of this query ? or if you have better query for listing like what i told please share :/ i am kinda newbie
A simple workaround would be to add another column to topics table –
last_message_at, which will contain date of last message. You will update this field while posting a message.Now you can do this
Should work pretty fast, if you index
last_message_at. Here you trade some write performance for read efficiency. I assume that your users read more than write.