Two causes of slowdown in slow COUNT query: Joining with Person is unnecessary, and the DISTINCT in the subselect is unnecessary. The DISTINCT is the big slowdown. The following query performs in under a second, as opposed to around 45 seconds on a production slave:
SELECT COUNT(*)
FROM Archive, ValidPersonOrTeamCache
WHERE
Archive.purpose = 2
AND Archive.distribution = 1
AND Archive.owner = ValidPersonOrTeamCache.id
AND Archive.id IN (
SELECT archive
FROM SourcepackagePublishingHistory
WHERE status IN (2, 1))
AND Archive.fti @@ ftq('munin')
AND Archive.private = FALSE
AND Archive.enabled = TRUE AND (1=1);
Two causes of slowdown in slow COUNT query: Joining with Person is unnecessary, and the DISTINCT in the subselect is unnecessary. The DISTINCT is the big slowdown. The following query performs in under a second, as opposed to around 45 seconds on a production slave:
SELECT COUNT(*) amCache distribution = 1 amCache. id blishingHistory
FROM Archive, ValidPersonOrTe
WHERE
Archive.purpose = 2
AND Archive.
AND Archive.owner = ValidPersonOrTe
AND Archive.id IN (
SELECT archive
FROM SourcepackagePu
WHERE status IN (2, 1))
AND Archive.fti @@ ftq('munin')
AND Archive.private = FALSE
AND Archive.enabled = TRUE AND (1=1);