DEV Community

Vikas Rai
Vikas Rai

Posted on

Discussion: How to find top n users with five recent posts

Hello all,

I hope all are fine in this lockdown and learning new things in this period. I want to discuss something with you guys.

THE OPTIMAL WAY OF FINDING RECORDS FROM THE DATABASE.

Let us say, the users who have multiple comments on different posts and they are stored in users, comments and posts tables respectively. On a dashboard page we want to list top n users with the most five recent comments on different posts. HOW would you achieve this through the query? If possible, write your query also and the reason that your query is the most optimal one.

Top comments (3)

Collapse
 
devdrake0 profile image
Si

You're question needs more information. How can someone answer this without knowing your database schema and what indexes you have?

What queries have you already tried? Why are they not performant enough that you're asking for help? What Database are you using?

This is such a broad question, you're either not going to get an answer or it's going to be so generic it won't help you.

Collapse
 
nagamocha3000 profile image
Naga Mocha

What is being used to rank the users and determine who are the 'top', is it the number of comments or something else?

Collapse
 
vikasrai91 profile image
Vikas Rai

The users are ranked based on most recent comments. So, if there are 1000 users and we want to show top 20 users. Then, only those users should be fetched ordered by their latest comments on any post.