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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T04:26:56+00:00 2026-06-07T04:26:56+00:00

I have a table with comments almost 2 million rows. We receive roughly 500

  • 0

I have a table with comments almost 2 million rows. We receive roughly 500 new comments per day. Each comment is assigned to a specific ID. I want to grab the most popular “discussions” based on the specific ID.

I have an index on the ID column.

What is best practice? Do I just group by this ID and then sort by the ID who has the most comments? Is this most efficient for a table this size?

  • 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-07T04:27:00+00:00Added an answer on June 7, 2026 at 4:27 am

    Do I just group by this ID and then sort by the ID who has the most comments?

    That’s pretty much simply how I would do it. Let’s just assume you want to retrieve the top 50:

    SELECT id
    FROM comments
    GROUP BY id
    ORDER BY COUNT(1) DESC 
    LIMIT 50
    

    If your users are executing this query quite frequently in your application and you’re finding that it’s not running quite as fast as you’d like, one way you could optimize it is to store the result of the above query in a separate table (topdiscussions), and perhaps have a script or cron that runs intermittently every five minutes or so which would update that table.

    Then in your application, just have your users select from the topdiscussions table so that they only need to select from 50 rows rather than 2 million.

    The downside of this of course being that the selection will no longer be in real-time, but rather out of sync by up to five minutes or however often you want to update the table. How real-time you actually need it to be depends on the requirements of your system.

    Edit: As per your comments to this answer, I know a little more about your schema and requirements. The following query retrieves the discussions that are the most active within the past day:

    SELECT a.id, etc...
    FROM discussions a
    INNER JOIN comments b ON 
        a.id = b.discussion_id AND 
        b.date_posted > NOW() - INTERVAL 1 DAY
    GROUP BY a.id
    ORDER BY COUNT(1) DESC
    LIMIT 50
    

    I don’t know your field names, but that’s the general idea.

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

Sidebar

Related Questions

Background I have a table of comments. Each comment belongs to a specific row
Say I have a table Comments with these columns: Id, Comment, Category, CreatedDate, CommenterId
I have a comments table, whose structure is as: id, name, email, comment I
I have a comments table where I store the comment content, author id and
I have two databases: old and new. Both have a comments table. There are
I have a table like: table: comments, with rows: id, author, content, replyto I
i have this table of comments (little over 1 milion rows) that gets around
I have this mysql table called comments which looks like this: commentID parentID type
Ok I have a table in my SQL Server database that stores comments. My
I have table with around 70 000 rows. There is 6000 rows that i

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.