faster, from about 4.5s to 1.0s for the global "latest" channel. Note that the query is only fast if the "IndexBuildsOnJob" and "IndexBuildsOnJobAndIsCurrent" indices are dropped - if they exist, PostgreSQL will use those instead of the more efficient "IndexBuildsOnJobFinishedId" index. Looks like a bug in the planner to me…
2I2ZX6JBPIM5D57G52MGHKUD66CJMOYAYNYN6CSHNUHAFA46A5YQC
E2TOU3L66CH5DA4XPATQM5YM63SFXX63V7SDOIGS4ND3GR7HQALAC
SJN2QPWHFYKX7CJMW4XZHI2P2THH7MECZZIHQMCH6EKBZ45G3DOAC
LBNVQXUBEZ45SOTGVXK5UEZXIAIZTJLWZNUYFI4JZ6J65N3KPDVQC
7UJ5YV4VUSXMKB57KKXDG6KLIXMQXO45VO5UTOT46BHQJBFZ7XKQC
SZYY2EQQK2JUAHMDPBASGWC2RON2GA5G6FHBLO4GAAX2XTM6QLSQC
J5UVLXOK6EDIL5I7VKWH4V2QDS4DPD7FHRK6XBWSXFRQS4JKXFZQC
Z4KRJX4QVMQ5F233DC3XMSE5ZJYHKX3UF5QWAGHDC54EC5NUBXUQC
6QRHXIM3XHCDLSIIBEGETDV67V6LTV55QMHC64ZPBMLTAECM5N3QC
N22GPKYTOLZLBGTGDATQDVZ4R5APZEAOIA7L32X4UXBH4XNI7MWAC
(select project, jobset, job, system, max(id) as id
(select
(select max(id) from builds b
where
project = activeJobs.project and jobset = activeJobs.jobset
and job = activeJobs.job and system = activeJobs.system
and finished = 1
and exists (select 1 from buildresultinfo where id = b.id and buildstatus = 0)
) as id
create index IndexBuildsOnJob on Builds(project, jobset, job);
create index IndexBuildsOnJobAndIsCurrent on Builds(project, jobset, job, isCurrent);
#create index IndexBuildsOnJob on Builds(project, jobset, job);
#create index IndexBuildsOnJobAndIsCurrent on Builds(project, jobset, job, isCurrent);