Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7541167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T07:49:28+00:00 2026-05-30T07:49:28+00:00

I got 2 tables First is where Topics are hold Second is where Messages

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T07:49:29+00:00Added an answer on May 30, 2026 at 7:49 am

    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

    SELECT * 
    FROM topics 
    ORDER BY last_message_at DESC 
    LIMIT 30;
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's what I'm trying to accomplish: I've got two tables, call them first and
I've got two tables in SQL, one with a project and one with categories
I've got two tables, 'Players' and 'Items'. Players have a list of items. I
I'm struggling with an exception using linq-to-sql Concat() I've got 2 tables. The first
I've got two un-related (no FK's defined) tables. The first table contains some tasks
I've got two tables with similar structure: - First table: id and col1,col2,col3 -
First a little background, I'm using jdk 1.6. I've got a 2 column table
I'm working with a SQL Server DB that's got tables spread across multiple schemas
I got two tables Product(Productname VARCHAR(10), Rating REAL) and Review(RatingID INT, Productname VARCHAR(10), Rating
I got two tables in my database: user and call. User exists of 3

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.