-- =========== Test #1 =========== -- BEGIN; SET enable_seqscan TO false; SET enable_bitmapscan TO false; SET enable_hashjoin TO false; SET enable_mergejoin TO false; EXPLAIN ANALYZE SELECT * FROM threads AS thr JOIN msgs AS msg ON msg.thrid = thr.id WHERE thr.forid = 1 ORDER BY thr.id DESC, msg.msgid DESC LIMIT 100; ROLLBACK; QUERY PLAN ------------------------------------------------------------------------------------------------------------------------------------------------------------------- Limit (cost=64537577.92..64537677.92 rows=100 width=447) (actual time=61923.813..61924.379 rows=100 loops=1) -> Sort (cost=64537577.92..64633839.92 rows=96262 width=447) (actual time=61923.804..61924.038 rows=100 loops=1) Sort Key: thr.id, msg.msgid -> Nested Loop (cost=0.00..61339121.37 rows=96262 width=447) (actual time=0.248..40366.113 rows=431350 loops=1) -> Index Scan using threads_uni_forid_id on threads thr (cost=0.00..14229.22 rows=2850 width=176) (actual time=0.127..5246.470 rows=8339 loops=1) Index Cond: (forid = 1) -> Index Scan using msgs_pkey on msgs msg (cost=0.00..12595.51 rows=2974 width=271) (actual time=1.544..4.000 rows=52 loops=8339) Index Cond: (msg.thrid = "outer".id) Total runtime: 62079.452 ms (9 rows) -- =========== Test #2 =========== -- EXPLAIN ANALYZE SELECT * FROM threads AS thr JOIN msgs AS msg ON msg.thrid = thr.id WHERE thr.forid = 1 ORDER BY thr.id DESC LIMIT 100; QUERY PLAN ---------------------------------------------------------------------------------------------------------------------------------------------------------------- Limit (cost=0.00..63721.01 rows=100 width=447) (actual time=9.579..10.775 rows=100 loops=1) -> Nested Loop (cost=0.00..61339121.37 rows=96262 width=447) (actual time=9.574..10.438 rows=100 loops=1) -> Index Scan Backward using threads_uni_forid_id on threads thr (cost=0.00..14229.22 rows=2850 width=176) (actual time=0.082..0.145 rows=4 loops=1) Index Cond: (forid = 1) -> Index Scan using msgs_pkey on msgs msg (cost=0.00..12595.51 rows=2974 width=271) (actual time=2.399..2.475 rows=25 loops=4) Index Cond: (msg.thrid = "outer".id) Total runtime: 11.071 ms (7 rows)