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 8290503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T12:50:07+00:00 2026-06-08T12:50:07+00:00

I am trying to find a straightforward way to improve the performance for very

  • 0

I am trying to find a straightforward way to improve the performance for very active forums where there are a huge number of posts and mysql can no longer do table sorts in memory and doesn’t seem to take full advantage of indexes.

This simple query finds the most recent post in each topic for a user to determine if they have any replies since (by later comparing the topic_time)

SELECT p.*, MAX(post_time) as post_time FROM forum_posts AS p   
WHERE p.poster_id = '1' AND p.post_status = '0' 
GROUP BY p.topic_id  
ORDER BY post_time DESC 
LIMIT 50

simple, flat table looks something like

post_id | poster_id | topic_id | post_status | post_time | post_text

However it’s performance falls apart when there are a million posts and the user themselves have tens of thousands of posts. MySQL can either no longer sort the table in memory or there are far too many rows to scan. It can take up to 3 seconds in realworld use which is unacceptable imho because it’s spiking the CPU during that time and slowing everyone else down.

I can make any combination of index of course, but mysql seems to mostly like to use a combo of

poster_id + post_time 

So it just selects the one user’s 50k posts out of the million and then starts grouping by topic_id and sorting. Strangely adding topic_id into the index mix doesn’t seem to help the performance, though it may be the order of the index fields?

I’ve tried to write an equivalent JOIN instead so I can use more than one index but I run into problems with the fact that each side has to be filtered by post_status and poster.

I was thinking it would be faster, at least for the first few pages, if mysql could be made to FIRST sort the data via it’s index by post_time and then start picking out the distinct topic_id for the user in descending order. I guess that would require a subquery and not sure a 50k result subquery would be any better, still needs a temporary table.

Of course a fundamental solution would be to augment the core design so that there is another table which just stores the max post_time for each user in each topic but this is far too big of a change unless no other solution can be found.

Thank you for any suggestions!


adding realworld example and EXPLAIN:

slow log

# Query_time: 2.751334  Lock_time: 0.000056 Rows_sent: 40  Rows_examined: 48286
SELECT   p.*, MAX(post_time) as post_time FROM forum_posts AS p   WHERE p.poster_id = '2' AND p.post_status = '0' GROUP BY p.topic_id  ORDER BY post_time DESC LIMIT 7000, 40;

explain

select_type     table   type    possible_keys   key     key_len     ref     rows    Extra
SIMPLE          p   ref poster_time poster_time 4   const   27072   Using where; Using temporary; Using filesort
  • 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-06-08T12:50:08+00:00Added an answer on June 8, 2026 at 12:50 pm

    First, fix your query to provide determinate results:

    SELECT p.topic_id, 
           MAX(post_time) as post_time 
    FROM forum_posts AS p   
    WHERE p.poster_id = '1' AND p.post_status = '0' 
    GROUP BY p.topic_id  
    ORDER BY post_time DESC 
      LIMIT 50 ;
    

    Then try it after adding an index on (post_status, poster_id, topic_id, post_time).

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

Sidebar

Related Questions

Trying to find a way to remove blank pages from a document I wrote
Trying to find the best way of create an overlap/overlay layer to fill the
Trying to find a way to send a POST HTTPS request from Python to
I am trying to find a way to Scale (either font or scaleX&Y) a
Is there a relatively straightforward way to do a date comparison, using jquery ,
I spent hours this afternoon trying to find a straightforward tutorial for installing PyCairo
Very straightforward CSS question that I haven't been able to find the answer to
I had a look around, trying to find a straightforward method for first saving
I'm following along with the VirtualTree demo and can't seem to find a way
I'm trying to find a quick way to change from an iPhone app to

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.