I've got a dashboard legitimate-time updates and latest notices.

Imagine this can be a blogging system where one can sign up for authors posts and comments. Then i want 2 kind of notices to look during my dashboard, sorted by insertion time:

  1. Last posts
  2. Last comments

Imagine I only want 10 updates to appear after i load the dashboard (later updtes I am during ajax). How must i query the database and just how must i sort the outcomes?

I have considered querying the two tables (posts and comments) for data that I have activated, include that data to a listing and sort individuals recent results for datetime after which return the final 10, however i feel this isn't an excellent solution because it would take plenty of time to sort if individuals tables (and my monthly subscriptions) start to grow.

What exactly are your solutions/ideas with this problem?

Query the very first table ordering by climbing down insertion some time and restricting leads to 10. Then query the 2nd table exactly the same way. Then merge leads to python and return the very first 10 of these.

A django example could seem like this:

from operator import attrgetter

recent_posts = Post.objects.order_by('-created')[:10]
recent_comments = Comment.objects.order_by('-created')[:10]
both_combined = list(recent_posts) + list(recent_comments)
both_sorted = sorted(both_combined, key=attrgetter('created'), reverse=True)
most_recent = both_sorted[:10]