문제

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

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top