Ensure index is used when running on MySQL #1639
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR fixes a performance issue when running notary server with MySQL. When calling
GetCurrent
to find the most recent metadata file for a GUN and role, we use this snippet:First
adds an additionalorder by
clause for theid
column, so this corresponds to SQL roughly like:We have an index on
gun, role, version
, and adding theorder by
onid
prevents this index from being used. Usually adding the primary key should be OK but because it's being added in the opposite direction (version
isDESC
andid
isASC
) it doesn't work. We don't need to add this primary key sort becausegun, role, version
already uniquely identifies the row.In the latest version of gorm 1.x there is a new method
Take
which behaves in the same way asFirst
but doesn't add the primary key sort. From some quick testing on Docker's notary instance, removing this from the query reduces the time this query takes on a very large repository from over 30 seconds to a few milliseconds.The main fix here is using
Take
instead ofFirst
on theGetCurrent
method. I've updated all usages ofFirst
toTake
in that file for consistency - I don't expect them to have much impact on performance. All of the other changes were necessary because of the gorm upgrade.