Repository navigation
Conversation
This should help improve the speed of this query to find if a version is the first occurrence within a job. Previously, the query would fetch all the builds within a job and then try to find a version within those list of builds that match the version. But that causes a lot of slowness when the job has a looot of builds. (the CTE part of the query is slow) The fix here is to have the first part of the query (the CTE) to find all the builds that have that version and then try to find a build that matches the job we are looking for. This will hopefully help reduce the number of rows from the CTE, because there are probably less builds that match the version than there are builds that are from the job. One concern is that there can be some versions of a resource that are being used by many builds within a pipeline, which can cause the query to be slow too because it needs to fetch all of those rows. But at least in the most common case, I believe that there will be less rows from narrowing down with a version vs narrowing down with a job id. Signed-off-by: Clara Fu <[email protected]>
|
Denormalizing here is a bit spooky on such a huge table - the increased disk space isn't ideal, but more concerning to me is the migration that it requires. If there was a way to denormalize Without denormalization, I definitely expect this to be faster in the vast majority of cases, but as you note, some versions are used by many builds. e.g. on CI, version 1.2.2 of More dramatic examples are So with this change, you've greatly optimized the query for jobs that have frequently changing versions as inputs, but long-lived versions may take a performance hit. The thing about these long-lived versions is that they rarely change build-to-build (obviously) - so what if we added a hot path for that case.
This would introduce a slight performance decrease for jobs with inputs that change frequently, but with the optimization you've already added, both queries should be quick in this case, so it hopefully wouldn't be too bad. For long-lived versions, we may still have to run the slow query (typically if an old version is pinned, given that the version doesn't change often by definition), but it should be much more rare WDYT? |
|
I definitely agree, seeing that data about those versions being used by so many builds is just confirmation that it might be super slow for some use cases. Just to get some more data, I ran the modified query using the Which is suuuper slow compared to if I run the same inputs against the original query that takes about 0.1 ms to complete. The idea you proposed is great! It would help to speed up the query for versions that have been used for many builds but still keeping the speed of versions that are changing often. I think the only times that it might have some performance drops are uncommon use cases like you mentioned, when versions are messed with. So I was hoping to see if there can be some optimization that could make the query faster even for versions that are used for many builds because I think (of course) ideally there are no slow queries even during uncommon cases. I think if we add this index If we do add that index, we can remove the |
|
Interesting! Just adding an index seems like a better approach than I suggested given that it works in the general case. Sounds like a pretty big index though - it's basically a complete clone of the table! Do we have an idea of how costly that migration would be? Or the additional storage requirements? Mostly just playing devil's advocate, I think your approach makes a lot of sense! |
|
I was actually hoping to implement both ur suggestion and the addition of the index to fully optimize the first occurrence query! But it is a pretty big index, the migration would probably take a few minutes (which it took about 1-2 minutes when I tried it on the ci deployment). I was hoping that because we are just replacing an existing index with this one, there actually wont be much more additional storage required for this. Just to give you an update on why I haven't been updating this PR, now that the I think that even though the query is really fast right now, we still want to refactor the query to not become a bottleneck when the database is under heavy memory usage. I'm just contemplating on whether to wait to properly compare the speeds of the approaches when the memory is high again or we could run a stress test or just go ahead with the new approach. 🤔 |
Maybe, but I'd expect the index to be a fair bit bigger, since now the index includes much more data per entry. Not sure how much that matters, though, as it's likely to be overshadowed by the larger tables (like build_events)
Woah, that's interesting that it gets 4 orders of magnitude slower when memory is constrained! In that case, I wonder if adding this index is worth it, given that in an unconstrained DB, the query speeds seem to be similarly fast.
Yeah, makes sense - I think your original refactor makes sense (
Not sure either...I feel like scaling hush-house down wouldn't be great for our tenants given the issues we were facing before. But yeah, maybe a stress test would be the way to go? Or maybe we should just deploy this on CI and see how things behave - we know this will be fast for frequently changing resources, but we could keep an eye on resources with a ton of versions like in |
|
I'm just going to close this for not until we find that we want to investigate it further! |
What does this PR accomplish?
Bug Fix | Feature | Documentation
On our large deployment, we were seeing that the first occurrence query was a bottleneck for the database.
concourse/atc/db/versions_db.go
Lines 38 to 47 in edae11a
There were consistently 20 of these queries being run on the database, which ends up clogging up the database connections. When I ran an
EXPLAIN ANALYZEon the query using a job that had 3000 builds, I saw that it was taking almost 15 seconds to runIt looks like most of the time that it takes is due to the fetching of all the builds for that job. Knowing this, I can think of two ways to fix this:
job_idcolumn by adding it to thebuild_resource_config_version_inputstableI think each of these options have its good and bad.
For the first option with refactoring the query, the way that I can think of to do this is to make it narrow down the builds by version first and then scope it down to the job id. This way there will be a smaller number of builds to work with, since the number of builds that match the version/resource will most likely and more commonly be smaller than the number of builds within a job. I say this with uncertainty because I understand that might not always be the case, for example, there might be some versions that are being used throughout many builds within a pipeline (a git version that has not been updated for 2 years but is being used for every build within several jobs). The following query would be how I imagine it would be written:
I tried running this option with
EXPLAIN ANALYZEand this is the results I got.Which is a lot faster as it took 30 ms vs 15000 ms. But this is also because I chose a job that had 3000 builds, which means that in this specific case it would be faster to use the refactored query. If, lets say, I chose an example that had a version used throughout many builds then the results might not be so much of an improvement. One thing to note is that if there is a version that is used throughout many builds, most likely it would be used throughout many builds of the same job so that means that both the original and refactored query would be slow in that situation.
For the second option with denormalizing, this means adding a
job_idcolumn to thebuild_resource_config_version_inputstable. We would then be able to just do a simple query without any CTE to find a version of a resource within a job. It would look something like thisI am unable to actually run the query without the column being there but I imagine it would be incredibly fast to run. This would remove any slowness due to needing to narrow down the set of builds first using a CTE, whether it is through a job_id or version, and would be directly using an index to fetch that exact row that we are looking for. This is probably the ideal choice, but it comes with the drawbacks of having to add a migration that will need to run for the incredibly large
build_resource_config_version_inputstable to add thejob_idinto the table. It will also result in duplication of thejob_idcolumn, as the data already exists within thebuildstable. This is very similar to what already exists within thesuccessful_build_outputstable (where it has ajob_id) and also what is currently being added to thebuild_image_resource_cachestable #5993. So I'm just unsure how far we want to go with this, or if it is actually a good idea to just spread that data throughout tables that we know are going to be really large and will benefit from this denormalization.Changes proposed by this PR:
The change introduces a refactor to the first occurrence query that should help improve its speed. Previously, the query would fetch all the builds within a job and then try to find a version within those list of builds that match the version. But that causes a lot of slowness when the job has a looot of builds. (the CTE part of the query is slow)
The fix here is to have the first part of the query (the CTE) to find all the builds that have that version and then try to find a build that matches the job we are looking for. This will hopefully help reduce the number of rows from the CTE, because there are probably less builds that match the version than there are builds that are from the job. But at least in the most common case, I believe that there will be less rows from narrowing down with a version vs narrowing down with a job id.
Notes to reviewer:
I submitted this PR with the first option but I actually just wanted to start a conversation in order to find what the best decision is going forward. The solution doesn't need to be one of these two options, those are just the ones I thought of at this moment. I am totally open to doing anything else we come up with!
Release Note
Contributor Checklist
Reviewer Checklist
BOSH and
Helm packaging; otherwise, ignored for
the integration
tests
(for example, if they are Garden configs that are not displayed in the
--helptext).