performance upgrade for tag subqueries #921
Open
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.
We recently noticed when upgrading a bunch of gems in our system that acts-as-taggable-on has some performance issues when doing a match_all query.
See below a fairly simple example of me fetching images that have two tags:
After fiddling with the query a bit to see what part was causing the inefficiencies I landed on the subquery comparisons in the two INNER JOINs, where they say that tag_id IN (SUBQUERY). If I simply changed the IN to an equals (=) then I saw performance improvements of ~110ms.
This change should be fine as long as we're not using wildcards, since the tags table has a unique constraint on name anyway.
Note that we're working with about 6500 tags, 1.8M taggings, and 330k assets, MySQL 5.7, Ruby 2.5.1, Rails 5.2, and I'm on a newer macbook pro (to bring some context to the performance numbers I saw).
I don't know what you usually do for version bumping in pull requests, but I bumped the version from what it was, that's what we do for our internal gems.