mirakui / activerecord-mysql-index-hint Goto Github PK
View Code? Open in Web Editor NEWLicense: MIT License
License: MIT License
Hi there, we have been using your library for a while. Thanks for maintaining it!
Today I ran into a curious Rails bug rails/rails#24193 that stemmed from the way the hints are set. It's not specific to the hints, it has to do any queries that:
from(...)
to a stringeager_load(:some_association)
.count
(like most pagination libraries)I'm pretty sure the problematic line is https://github.com/rails/rails/blob/4232f7ee427a510eb07e420f5883611bcb0abfae/activerecord/lib/active_record/relation/finder_methods.rb#L408 but we can avoid this by making this line truthy https://github.com/rails/rails/blob/4232f7ee427a510eb07e420f5883611bcb0abfae/activerecord/lib/active_record/relation/finder_methods.rb#L402
I came across another hinting library https://github.com/winebarrel/arel-mysql-index-hint but it is "sticky" in that once you set a hint for one query, all subsequent queries against the model will reuse that hint.
So I wanted to ping you before submitting a PR.
What I'm envisioning is something like this:
Arel::Table
or create a subclass of Arel::Table
that includes an index hint attributeProduct.where(user_id: 1).force_index(:idx_user_id).first
) but we would instantiate an Arel::Table instead of setting the string.A rough sketch of the code would look like:
def from_with_index_hint(hint_type, *args)
return self if args.blank?
indexes = args.map {|index| connection.quote_column_name index }
temporary_arel_table = arel_table.dup # this is part of ActiveRecord
temporary_arel_table.index_hint = "#{hint_type} INDEX(#{indexes.join(', ')})"
self.from(temporary_arel_table)
end
So we just set the from()
to a different Arel::Table
and the overrided visitor that we write would output the hint.
What are your thoughts on a change like that?
While taking a look at [Feature] Active Record: Add use_index
I've found this gem.
I started testing it by running specs the current master branch and got this error likely due to bundler's feature.
https://travis-ci.org/yahonda/activerecord-mysql-index-hint/jobs/272447821
[!] There was an error parsing `4.1.gemfile`: You cannot specify the same gem twice coming from different sources.
You specified that activerecord-mysql-index-hint (>= 0) should come from source at `../` and source at `../`
. Bundler cannot continue.
# from /home/travis/build/yahonda/activerecord-mysql-index-hint/gemfiles/4.1.gemfile:9
# -------------------------------------------
#
> gemspec :path => "../"
#
I've pushed reboot_ci_at_travis branch to resolve this error. My first target is to run specs at Travis CI and wanted to get some feedback if this approach is correct.
Thanks.
Please if you can add mysql as a dependency so user won't be able to install gem if he is not using mysql.
I want to call force index in a join as in:
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.