Question

I have slow query:

   SELECT DISTINCT ON ( topic_category_id ) * FROM topic t
    WHERE abstime ( post_time + 24 * 3600 ) >= now ( )
    ORDER BY topic_category_id, post_time DESC LIMIT 10;

It's because I use DISTINCT but I can find out how I can change this query. I can't use GROUP BY because I need to be ordered by post_time. Please advise

Was it helpful?

Solution

This might be worth a try:

   SELECT DISTINCT ON ( topic_category_id ) * FROM topic t
    WHERE post_time >= abstime(now ( ) - 24 * 3600 )
    ORDER BY topic_category_id, post_time DESC LIMIT 10;

The reason it might be faster is that Postgres can do the time calc only once, rather than do the calc for each row returned.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top