~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/lp/registry/model/distroseriesdifference.py

[r=julian-edwards][bug=798301] Correct the behaviour of
 most_recent_publications(),
 and improve its performance by forcing PostgreSQL to consider indexes other
 than (archive, status) on SourcePackagePublishingHistory.

Show diffs side-by-side

added added

removed removed

Lines of Context:
118
118
        )
119
119
    conditions = And(
120
120
        DistroSeriesDifference.id.is_in(dsd.id for dsd in dsds),
121
 
        SourcePackagePublishingHistory.archiveID == Archive.id,
 
121
        # The + 0 below prevents PostgreSQL from using the (archive, status)
 
122
        # index on SourcePackagePublishingHistory, the use of which results in
 
123
        # a terrible query plan.
 
124
        SourcePackagePublishingHistory.archiveID + 0 == Archive.id,
122
125
        SourcePackagePublishingHistory.sourcepackagereleaseID == (
123
126
            SourcePackageRelease.id),
124
127
        SourcePackagePublishingHistory.status.is_in(statuses),
130
133
        conditions = And(
131
134
            conditions,
132
135
            DistroSeries.id == DistroSeriesDifference.parent_series_id,
133
 
            Archive.distributionID == DistroSeries.distributionID,
134
 
            Archive.purpose == ArchivePurpose.PRIMARY,
 
136
            SourcePackagePublishingHistory.distroseriesID == (
 
137
                DistroSeriesDifference.parent_series_id),
135
138
            )
136
139
    else:
137
140
        conditions = And(
138
141
            conditions,
139
142
            DistroSeries.id == DistroSeriesDifference.derived_series_id,
140
 
            Archive.distributionID == DistroSeries.distributionID,
141
 
            Archive.purpose == ArchivePurpose.PRIMARY,
 
143
            SourcePackagePublishingHistory.distroseriesID == (
 
144
                DistroSeriesDifference.derived_series_id),
142
145
            )
 
146
    # Ensure that the archive has the right purpose.
 
147
    conditions = And(
 
148
        conditions,
 
149
        Archive.distributionID == DistroSeries.distributionID,
 
150
        # DistroSeries.getPublishedSources() matches on MAIN_ARCHIVE_PURPOSES,
 
151
        # but we are only ever going to be interested in PRIMARY archives.
 
152
        Archive.purpose == ArchivePurpose.PRIMARY,
 
153
        )
143
154
    # Do we match on DistroSeriesDifference.(parent_)source_version?
144
155
    if match_version:
145
156
        if in_parent: