Repository navigation
Description
Summary
In my stress environment, the database is pinned to around 95% CPU and it seems like the most troublesome query is the following:
WITH builds_of_job AS (
SELECT id FROM builds WHERE job_id = $1
)
SELECT EXISTS (
SELECT 1
FROM build_resource_config_version_inputs i
JOIN builds_of_job b ON b.id = i.build_id
WHERE i.name = $2
AND i.version_md5 = $3
AND i.resource_id = $4
);
The following image is a screenshot of the db inspection query I ran against my stress env
You can see that the builds_of_job query takes significantly more avg and total time than the other queries below it.
This query has been worked on many times and we even had an PR that I created a while ago that reworks it further #6403. I think we ended up not completing the work because the environment we were seeing the query be slow on no longer showed the slowness, but now that I have the stress environment I can do the rest of the work to improve this query.
When I run the query on my stress env, I get the following explain analyze results:
Result (cost=25768.26..25768.27 rows=1 width=1) (actual time=2334.258..2334.259 rows=1 loops=1)
CTE builds_of_job
-> Index Only Scan using builds_ordered_by_job_id_idx on builds (cost=0.56..24932.55 rows=34333 width=8) (actual time=0.870..2159.823 rows=35377 loops=1)
Index Cond: (job_id = 1)
Heap Fetches: 4713
InitPlan 2 (returns $1)
-> Hash Join (cost=20.30..835.72 rows=1 width=0) (actual time=2334.257..2334.257 rows=1 loops=1)
Hash Cond: (b.id = i.build_id)
-> CTE Scan on builds_of_job b (cost=0.00..686.66 rows=34333 width=8) (actual time=0.880..2226.518 rows=35377 loops=1)
-> Hash (cost=20.29..20.29 rows=1 width=8) (actual time=3.416..3.416 rows=3 loops=1)
Buckets: 1024 Batches: 1 Memory Usage: 9kB
-> Index Scan using build_inputs_resource_versions_idx on build_resource_config_version_inputs i (cost=0.56..20.29 rows=1 width=8) (actual time=1.365..3.412 rows=3 loops=1)
Index Cond: ((resource_id = 1) AND (version_md5 = 'b5f0011a6d168c264ea8595b1696d62a'::text))
Filter: (name = '1m'::text)
Planning time: 10.428 ms
Execution time: 2336.828 ms
The slowest part of the query comes from us needing to fetch all the builds for that job. I chose a job that had a lot of builds, which is why you can see the time it took to run was very long.
I think what I'm going to do is implement the thing we discussed in the old PR. I'm going to first check the version against the previous build and if it exists then we are finished there. But if it does not exist then I will run a reworked query
EXPLAIN ANALYZE WITH builds_of_version AS (
SELECT build_id
FROM build_resource_config_version_inputs
WHERE name = $1
AND version_md5 = $2
AND resource_id = $3
) SELECT EXISTS (
SELECT 1
FROM builds b
JOIN builds_of_version v ON b.id = v.build_id
WHERE job_id = $4
);
Where the difference is basically it narrows down the rows of builds by first filtering on the versions and then checking to see if that build exists for that job. The problem with this query is that if you have a version that is used by a ton of builds, it will end up having to grab all those builds that use the query and that can be just as slow as the previous query. That is why if we add the logic of first checking to see if the version is the same as the previous build, that might avoid us needing to do this long query. Also, I mentioned in the PR that if I add this new index create index someindex on build_resource_config_version_inputs (resource_id, version_md5, name, build_id); then it will help speed up that slow query.
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone

Activity
db: optimize IsFirstOccurrence query for jobs with many builds
db: optimize IsFirstOccurrence query for jobs with many builds