bah, wrong explain in my prior comment - here is the right one:
explain analyze SELECT max(branchrevision.sequence) as revno
FROM Revision JOIN BranchRevision ON BranchRevision.revision = Revision.id
WHERE BranchRevision.branch = 492361 AND branchrevision.sequence is not null and Revision.revision_date < '2009-08-10 21:56:30.623339+00:00'; QUERY PLAN
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Aggregate (cost=61101.68..61101.69 rows=1 width=4) (actual time=197.653..197.654 rows=1 loops=1)
-> Nested Loop (cost=0.00..61091.07 rows=4245 width=4) (actual time=0.068..194.178 rows=9084 loops=1)
-> Index Scan using revisionnumber_branch_sequence_unique on branchrevision (cost=0.00..29518.34 rows=4245 width=8) (actual time=0.043..70.559 rows=13262 loops=1) Index Cond: (branch = 492361) Filter: (sequence IS NOT NULL)
-> Index Scan using changeset_pkey on revision (cost=0.00..7.43 rows=1 width=4) (actual time=0.008..0.008 rows=1 loops=13262) Index Cond: (revision.id = branchrevision.revision) Filter: (revision.revision_date < '2009-08-10 21:56:30.623339'::timestamp without time zone)
Total runtime: 197.746 ms
This is half the estimated cost, which is an improvement - and 20% of the measured hot cost, which suggests quite some scattered data.
bah, wrong explain in my prior comment - here is the right one:
explain analyze SELECT max(branchrevis ion.sequence) as revno revision = Revision.id branch = 492361 AND branchrevision. sequence is not null and Revision. revision_ date < '2009-08-10 21:56:30. 623339+ 00:00';
QUERY PLAN ------- ------- ------- ------- ------- ------- ------- ------- ------- ------- ------- ------- ------- ------- ------- ------- ------- ------- ------- ------- ------- ------- ------- ------ 68..61101. 69 rows=1 width=4) (actual time=197. 653..197. 654 rows=1 loops=1) 00..61091. 07 rows=4245 width=4) (actual time=0.068..194.178 rows=9084 loops=1) branch_ sequence_ unique on branchrevision (cost=0. 00..29518. 34 rows=4245 width=8) (actual time=0.043..70.559 rows=13262 loops=1)
Index Cond: (branch = 492361)
Filter: (sequence IS NOT NULL)
Index Cond: (revision.id = branchrevision. revision)
Filter: (revision. revision_ date < '2009-08-10 21:56:30. 623339' ::timestamp without time zone)
FROM Revision JOIN BranchRevision ON BranchRevision.
WHERE BranchRevision.
-------
Aggregate (cost=61101.
-> Nested Loop (cost=0.
-> Index Scan using revisionnumber_
-> Index Scan using changeset_pkey on revision (cost=0.00..7.43 rows=1 width=4) (actual time=0.008..0.008 rows=1 loops=13262)
Total runtime: 197.746 ms
This is half the estimated cost, which is an improvement - and 20% of the measured hot cost, which suggests quite some scattered data.