QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------------------- Limit (cost=112.38..112.51 rows=51 width=88) (actual time=4.990..4.998 rows=51 loops=1) Buffers: shared hit=271 -> Sort (cost=112.38..112.51 rows=52 width=88) (actual time=4.986..4.989 rows=51 loops=1) Sort Key: created_at, id Sort Method: top-N heapsort Memory: 36kB Buffers: shared hit=271 -> Bitmap Heap Scan on messages (cost=4.69..110.90 rows=52 width=88) (actual time=1.292..2.987 rows=12000 loops=1) Recheck Cond: (chat_id = 'c939f183-65af-4ab7-b565-cd5328e24289'::uuid) Heap Blocks: exact=148 Buffers: shared hit=265 -> Bitmap Index Scan on messages_chat_keyset_idx (cost=0.00..4.67 rows=52 width=0) (actual time=1.247..1.248 rows=12000 loops=1) Index Cond: (chat_id = 'c939f183-65af-4ab7-b565-cd5328e24289'::uuid) Buffers: shared hit=117 Planning: Buffers: shared hit=102 Planning Time: 0.488 ms Execution Time: 5.050 ms (17 rows) QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------------------- Limit (cost=0.29..4.65 rows=51 width=65) (actual time=0.040..0.267 rows=51 loops=1) Buffers: shared hit=53 -> Index Scan using messages_chat_keyset_idx on messages (cost=0.29..1026.72 rows=12000 width=65) (actual time=0.039..0.258 rows=51 loops=1) Index Cond: (chat_id = 'c939f183-65af-4ab7-b565-cd5328e24289'::uuid) Buffers: shared hit=53 Planning: Buffers: shared hit=107 Planning Time: 0.661 ms Execution Time: 0.309 ms (9 rows)