Repository navigation
Speed up database queries by adding a job_id column to build image resource caches table and adding an index for ordering builds of a job - #5993
Conversation
jamieklassen
left a comment
There was a problem hiding this comment.
I've been pondering this for a while, trying to understand why the expensive query in Finish() exists. I tried commenting it out, and the only db test that fails as a result is
ResourceCacheLifecycle CleanUpInvalidCaches the resource cache is used by a build when its a build of a job in a pipeline when the cache is for a saved image resource version for a finished build when the build has succeeded [It] removes resource caches for previous finished builds
what is not clear to me is this -- is this a functional requirement, or an optimization? If you fail to remove image_resource caches from older builds, is there some risk of them being erroneously reused in subsequent builds? I will try to test this myself now but I figured I'd just ask the question to avoid an even longer feedback loop.
| @@ -0,0 +1,12 @@ | |||
| BEGIN; | |||
| ALTER TABLE build_image_resource_caches | |||
| ADD COLUMN job_id integer REFERENCES jobs(id); | |||
There was a problem hiding this comment.
This is the controversial part of this change, since it denormalizes (there is redundancy between job_id on build_image_resource_caches and the result of joining builds on builds.id = build_image_resource_caches.build_id and selecting builds.job_id). I have to wonder if there's a better way.
There was a problem hiding this comment.
Right I wanted to remove the need to join to the builds table in order to fetch the job_id from the build because that is the part that makes the current query slow. This is definitely something I am open to finding a different way to not need to duplicate the job_id data everywhere because this is something I run into a lot, where the thing that makes a query slow is the joining to the builds table in order to grab the job_id from it.
| UPDATE build_image_resource_caches | ||
| SET job_id = builds.job_id | ||
| FROM builds | ||
| WHERE builds.id = build_id; |
There was a problem hiding this comment.
have you profiled this migration? I imagine it must sequentially scan build_image_resource_caches... but not every build has image_resource caches, and that table should be regularly cleaned up by the existing query in Finish() on db.Build as well as the CleanBuildImageResourceCaches method on db.resourceCacheLifecycle... so it's fast?
There was a problem hiding this comment.
I haven't profiled the migration but in a deployment like hush-house, there is approx 600k rows and in our prod environment there is 9k rows. I imagined the caches should be cleaned up pretty often so it shouldn't have too many rows to update?
|
tl;dr - what about outright deleting the complex query in the I just spent a while investigating the history of the concourse/atc/db/migration/migrations/1510262030_initial_schema.up.sql Lines 1314 to 1315 in 4b447f1 Actually the restriction is more of a last resort, since the resource cache lifecycle explicitly avoids deleting resource caches that are referenced by I asked before if explicitly deleting which unilaterally deletes all entries associated to builds that finished at least 24h ago: |
|
I wonder if it's worth having an operator flag to allow you to choose between the old and new |
|
I think that's fair to set up a controlled environment in order to see if this is a real problem/if the solution is a good fix. The reason why I pin pointed these two queries to optimize is because I saw them being the 1st and 2nd worst culprits for the slowest average and total time spent on these queries throughout the deployment. Your suggestion for removing the query is interesting, I haven't even thought of the possibility of removing the query entirely! In most cases it won't have much of an impact because image resource caches shouldn't be changing often and keeping them around for 24 hours is fine in my opinion. I guess the only thing I would be worried about is jobs that run often and are constantly using new images as the |
|
Another option (which is verging on anti-pattern territory, but also is maybe justified) would be for the 24 hour lifetime of an image_resource cache to be configurable -- if operators want to trade disk pressure for network pressure, they can shorten that lifespan. |
|
Okay I think I might separate that out into another PR for removing the query and allowing the gc interval for build image caches to be configurable. |
|
@jamieklassen Sorry its taken me a while to get to this, I took a stab at splitting this into two PRs and I noticed that the So it seems like the cleaning of build images in the |
|
@clarafu good point, I'm pretty sure I hadn't noticed that. I guess this means that build image resource caches currently stick around until a subsequent build of the same job succeeds, even if that means sticking around for a very long time. I haven't really thought through this model, and I haven't thought much about the disk saturation induced by long-lived volumes (i might think about it more if we make ft account for volumes). but my wild guess is that it would be OK for image_resource caches to always have a fixed lifespan? maybe that should be a separate conversation, as you say... |
|
@jamieklassen Hm if we did make it have a fixed lifespan, the situation I can think of is a user has a job that typically runs a build every day or two. Before switching images to have a fixed lifespan, each time a build runs it can reuse the same resource cache (no pulling of the image if it never got a new version) but if we did make it a fixed lifespan of 24 hours, any build that runs after that time will need to re-fetch the resource cache everytime. Knowing this, I think I'm a lot more hesitant to remove the slow query from |
|
Took a quick look at alternatives to the denormalization. One thing I found was |
|
Personally, I'm of the opinion that denormalizing One thing I'm not so sure about is whether |
|
@aoldershaw That's a really good point! I think right now I think Jamie was mostly concerned with how far we want to go with the denormalizing things for performance reasons and if we could find another way that didn't involve denormalizing. The current way the database is modelled is very much normalized, and I kind of broke that when I added the If you are fine with it, I think it would really help with the performance of the query. I've tried many different things to try to have the same result as just adding the |
Ah, right - that makes me feel better about it!
It's fine with me - it seems like any alternatives also have their downsides, and I don't think a little denormalization is so scary in this particular case, given that the denormalized state is effectively immutable and the |
Adding the job id column to the build image resource caches table will be denormalizing the tables as the information is able to be fetched from joining to the builds table. Even though it is breaking the current norm of normalized tables within the database, it greatly improves the speed of queries so that the queries no longer need to join to the large builds table. Signed-off-by: Clara Fu <[email protected]>
Signed-off-by: Clara Fu <[email protected]>
Signed-off-by: Clara Fu <[email protected]>
|
By the way, do you think it's worth backporting this optimization to 6.7.3? |
|
@aoldershaw Is it because that is going to be the enterprise version? |
This reverts a dropped index from #5993. Since this was never released, it shouldn't be a problem modifying the migration - we'll just need to be careful when we deploy CI/Hush House Signed-off-by: Aidan Oldershaw <[email protected]>
What does this PR accomplish?
Feature
This PR adds a few improvements to some database queries that are slow on large Concourse instances. A few that I noticed on our large environment are the query to delete build image resource caches
which takes 8100 ms with the following
EXPLAIN ANALYZEand listing builds of a job
which takes 7800 ms and produces the following
EXPLAIN ANALYZEChanges proposed by this PR:
This PR modifies the query for deleting build image resource caches by adding a
job_idcolumn in thebuild_image_resource_cachestable so it no longer needs toJOINto the builds table and finding all the builds for a job (which is the part that takes the longest). By having ajob_idcolumn, I was able to simplify the query toI was only able to test out this change on a smaller environment, which this is the
EXPLAIN ANALYZEbefore the changeand this is after adding the column
The second part of this PR adds a new index to speed up the query for fetching the builds for a job in order while taking into account rerun builds. The following
EXPLAIN ANALYZEis the difference after adding the index.Also removed an unused index in the
buildstable!Contributor Checklist
Reviewer Checklist
New config flags added? Ensure that they are added to the BOSH and Helm packaging; otherwise, ignored for the integration tests (for example, if they are Garden configs that are not displayed in the--helptext).