this is a recursive implementation - loads as many revs as are needed - and could be trivially tweaked to return the referenced revisions inline..
explain analyze WITH RECURSIVE mp_revs AS (
select sequence as sequence, revision as revision from branchrevision where branch=492361 and sequence = (select max(sequence) from branchrevision where branch=492361)
UNION
select branchrevision.sequence, branchrevision.revision from mp_revs, branchrevision, revision where branchrevision.branch=492361 and branchrevision.revision=revision.id and branchrevision.sequence=mp_revs.sequence-1 and Revision.revision_date >= '2009-08-10 21:56:30.623339+00:00'
)
SELECT sequence, revision FROM mp_revs; QUERY PLAN
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
CTE Scan on mp_revs (cost=12367.71..12368.13 rows=21 width=8) (actual time=46.900..161.402 rows=4178 loops=1)
CTE mp_revs
-> Recursive Union (cost=1.40..12367.71 rows=21 width=8) (actual time=46.896..156.775 rows=4178 loops=1)
-> Index Scan using revisionnumber_branch_sequence_unique on branchrevision (cost=1.40..122.42 rows=1 width=8) (actual time=46.887..46.889 rows=1 loops=1) Index Cond: ((branch = 492361) AND (sequence = $2)) InitPlan 2 (returns $2) -> Result (cost=1.39..1.40 rows=1 width=0) (actual time=46.854..46.854 rows=1 loops=1) InitPlan 1 (returns $1) -> Limit (cost=0.00..1.39 rows=1 width=4) (actual time=46.845..46.846 rows=1 loops=1) -> Index Scan Backward using revisionnumber_branch_sequence_unique on branchrevision (cost=0.00..29518.34 rows=21199 width=4) (actual time=46.843..46.843 rows=1 loops=1) Index Cond: (branch = 492361) Filter: (sequence IS NOT NULL)
-> Nested Loop (cost=0.00..1224.49 rows=2 width=8) (actual time=0.021..0.024 rows=1 loops=4178)
-> Nested Loop (cost=0.00..1210.61 rows=2 width=8) (actual time=0.011..0.013 rows=1 loops=4178) -> WorkTable Scan on mp_revs (cost=0.00..0.20 rows=10 width=4) (actual time=0.000..0.001 rows=1 loops=4178) -> Index Scan using revisionnumber_branch_sequence_unique on branchrevision (cost=0.00..121.03 rows=1 width=8) (actual time=0.009..0.009 rows=1 loops=4178) Index Cond: ((public.branchrevision.branch = 492361) AND (public.branchrevision.sequence = (mp_revs.sequence - 1)))
-> Index Scan using changeset_pkey on revision (cost=0.00..6.93 rows=1 width=4) (actual time=0.008..0.009 rows=1 loops=4178) Index Cond: (revision.id = public.branchrevision.revision) Filter: (revision.revision_date >= '2009-08-10 21:56:30.623339'::timestamp without time zone)
Total runtime: 163.074 ms
(21 rows)
this is a recursive implementation - loads as many revs as are needed - and could be trivially tweaked to return the referenced revisions inline.. sequence, branchrevision. revision from mp_revs, branchrevision, revision where branchrevision. branch= 492361 and branchrevision. revision= revision. id and branchrevision. sequence= mp_revs. sequence- 1 and Revision. revision_ date >= '2009-08-10 21:56:30. 623339+ 00:00'
QUERY PLAN ------- ------- ------- ------- ------- ------- ------- ------- ------- ------- ------- ------- ------- ------- ------- ------- ------- ------- ------- ------- ------- ------- ------- ------- ------- ------- ------- ------- -- 71..12368. 13 rows=21 width=8) (actual time=46. 900..161. 402 rows=4178 loops=1) 40..12367. 71 rows=21 width=8) (actual time=46. 896..156. 775 rows=4178 loops=1) branch_ sequence_ unique on branchrevision (cost=1.40..122.42 rows=1 width=8) (actual time=46.887..46.889 rows=1 loops=1)
Index Cond: ((branch = 492361) AND (sequence = $2))
InitPlan 2 (returns $2)
-> Result (cost=1.39..1.40 rows=1 width=0) (actual time=46.854..46.854 rows=1 loops=1)
InitPlan 1 (returns $1)
- > Limit (cost=0.00..1.39 rows=1 width=4) (actual time=46.845..46.846 rows=1 loops=1)
-> Index Scan Backward using revisionnumber_ branch_ sequence_ unique on branchrevision (cost=0. 00..29518. 34 rows=21199 width=4) (actual time=46.843..46.843 rows=1 loops=1)
Index Cond: (branch = 492361)
Filter: (sequence IS NOT NULL)
-> WorkTable Scan on mp_revs (cost=0.00..0.20 rows=10 width=4) (actual time=0.000..0.001 rows=1 loops=4178)
-> Index Scan using revisionnumber_ branch_ sequence_ unique on branchrevision (cost=0.00..121.03 rows=1 width=8) (actual time=0.009..0.009 rows=1 loops=4178)
Index Cond: ((public. branchrevision. branch = 492361) AND (public. branchrevision. sequence = (mp_revs.sequence - 1)))
Index Cond: (revision.id = public. branchrevision. revision)
Filter: (revision. revision_ date >= '2009-08-10 21:56:30. 623339' ::timestamp without time zone)
explain analyze WITH RECURSIVE mp_revs AS (
select sequence as sequence, revision as revision from branchrevision where branch=492361 and sequence = (select max(sequence) from branchrevision where branch=492361)
UNION
select branchrevision.
)
SELECT sequence, revision FROM mp_revs;
-------
CTE Scan on mp_revs (cost=12367.
CTE mp_revs
-> Recursive Union (cost=1.
-> Index Scan using revisionnumber_
-> Nested Loop (cost=0.00..1224.49 rows=2 width=8) (actual time=0.021..0.024 rows=1 loops=4178)
-> Nested Loop (cost=0.00..1210.61 rows=2 width=8) (actual time=0.011..0.013 rows=1 loops=4178)
-> Index Scan using changeset_pkey on revision (cost=0.00..6.93 rows=1 width=4) (actual time=0.008..0.009 rows=1 loops=4178)
Total runtime: 163.074 ms
(21 rows)