arel-mysql-index-hint 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +15 -0
- data/.rspec +2 -0
- data/.travis.yml +8 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +33 -0
- data/Rakefile +5 -0
- data/Schemafile +33 -0
- data/arel-mysql-index-hint.gemspec +26 -0
- data/lib/arel-mysql-index-hint.rb +5 -0
- data/lib/arel-mysql-index-hint/active_record-hint_methods.rb +11 -0
- data/lib/arel-mysql-index-hint/active_record-querying.rb +3 -0
- data/lib/arel-mysql-index-hint/arel-visitors-mysql.rb +42 -0
- data/spec/arel-mysql-index-hint/eager_load_spec.rb +53 -0
- data/spec/arel-mysql-index-hint/includes_spec.rb +53 -0
- data/spec/arel-mysql-index-hint/joins_spec.rb +105 -0
- data/spec/arel-mysql-index-hint/preload_spec.rb +22 -0
- data/spec/arel-mysql-index-hint/without_join_spec.rb +103 -0
- data/spec/init.sql +45 -0
- data/spec/models.rb +37 -0
- data/spec/spec_helper.rb +43 -0
- metadata +185 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: b494fd4bb3092702e7964b5c9cfc1b0b2f9b6e69
|
4
|
+
data.tar.gz: 39d422ba66f4ef1cd670a1228aeb19ace36cd1ae
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0c4b471e7da946caf78029c2d3f8f0c41fada63d10db3b2a741a39d2413eeb565cdf37db1c3040a8c5ba7e825be69e280b32eb2684eb501ac885a8a6a7848cae
|
7
|
+
data.tar.gz: c0e5a925a0177f4f4fb1dcedc5a6d9b58a5b3563ee83b918360a1ef2b74a56329822f6d18253c98ed5b2a9cae16b0b616843ab3e0fddb54df54af805a5bbcfb1
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Genki Sugawara
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
# arel-mysql-index-hint
|
2
|
+
|
3
|
+
Add index hint to MySQL query in Arel.
|
4
|
+
|
5
|
+
[![Gem Version](https://badge.fury.io/rb/arel-mysql-index-hint.svg)](http://badge.fury.io/rb/arel-mysql-index-hint)
|
6
|
+
[![Build Status](https://travis-ci.org/winebarrel/arel-mysql-index-hint.svg?branch=master)](https://travis-ci.org/winebarrel/arel-mysql-index-hint)
|
7
|
+
[![Coverage Status](https://img.shields.io/coveralls/winebarrel/arel-mysql-index-hint.svg)](https://coveralls.io/r/winebarrel/arel-mysql-index-hint?branch=master)
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'arel-mysql-index-hint'
|
15
|
+
```
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install arel-mysql-index-hint
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
Article.joins(:comments).hint(articles: {idx_article: :use})
|
29
|
+
# => "SELECT `articles`.* FROM `articles` use INDEX (idx_article) INNER JOIN `comments` ON `comments`
|
30
|
+
|
31
|
+
Article.joins(:comments).hint(comments: {idx_comment: :force})
|
32
|
+
# => "SELECT `articles`.* FROM `articles` INNER JOIN `comments` force INDEX (idx_comment) ON `comments"
|
33
|
+
```
|
data/Rakefile
ADDED
data/Schemafile
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
create_table "microposts", force: true do |t|
|
2
|
+
t.string "content"
|
3
|
+
t.integer "user_id"
|
4
|
+
t.datetime "created_at"
|
5
|
+
t.datetime "updated_at"
|
6
|
+
end
|
7
|
+
|
8
|
+
add_index "microposts", ["user_id", "created_at"], name: "index_microposts_on_user_id_and_created_at"
|
9
|
+
|
10
|
+
create_table "relationships", force: true do |t|
|
11
|
+
t.integer "follower_id"
|
12
|
+
t.integer "followed_id"
|
13
|
+
t.datetime "created_at"
|
14
|
+
t.datetime "updated_at"
|
15
|
+
end
|
16
|
+
|
17
|
+
add_index "relationships", ["followed_id"], name: "index_relationships_on_followed_id"
|
18
|
+
add_index "relationships", ["follower_id", "followed_id"], name: "index_relationships_on_follower_id_and_followed_id", unique: true
|
19
|
+
add_index "relationships", ["follower_id"], name: "index_relationships_on_follower_id"
|
20
|
+
|
21
|
+
create_table "users", force: true do |t|
|
22
|
+
t.string "name"
|
23
|
+
t.string "email"
|
24
|
+
t.datetime "created_at"
|
25
|
+
t.datetime "updated_at"
|
26
|
+
t.string "password_digest"
|
27
|
+
t.string "remember_token"
|
28
|
+
t.boolean "admin"
|
29
|
+
end
|
30
|
+
|
31
|
+
add_index "users", ["email"], name: "index_users_on_email", unique: true
|
32
|
+
add_index "users", ["remember_token"], name: "index_users_on_remember_token"
|
33
|
+
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
Gem::Specification.new do |spec|
|
3
|
+
spec.name = "arel-mysql-index-hint"
|
4
|
+
spec.version = "0.1.0"
|
5
|
+
spec.authors = ["Genki Sugawara"]
|
6
|
+
spec.email = ["sgwr_dts@yahoo.co.jp"]
|
7
|
+
spec.summary = %q{Add index hint to MySQL query in Arel.}
|
8
|
+
spec.description = %q{Add index hint to MySQL query in Arel.}
|
9
|
+
spec.homepage = "https://github.com/winebarrel/arel-mysql-index-hint"
|
10
|
+
spec.license = "MIT"
|
11
|
+
|
12
|
+
spec.files = `git ls-files -z`.split("\x0")
|
13
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
14
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
15
|
+
spec.require_paths = ["lib"]
|
16
|
+
|
17
|
+
spec.add_dependency "activerecord", "~> 4.1.0"
|
18
|
+
spec.add_dependency "arel", "~> 5.0.0"
|
19
|
+
|
20
|
+
spec.add_development_dependency "bundler"
|
21
|
+
spec.add_development_dependency "rake"
|
22
|
+
spec.add_development_dependency "mysql2"
|
23
|
+
spec.add_development_dependency "rspec", ">= 3.0.0"
|
24
|
+
spec.add_development_dependency "factory_girl"
|
25
|
+
spec.add_development_dependency "coveralls"
|
26
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
module ActiveRecord::HintMethods
|
2
|
+
def hint(index_hint_by_table)
|
3
|
+
thread_index_hint_by_table = (Thread.current[Arel::Visitors::MySQL::INDEX_HINT_BY_TABLE_THREAD_KEY] ||= {}.with_indifferent_access)
|
4
|
+
thread_index_hint_by_table.update(index_hint_by_table)
|
5
|
+
self
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
ActiveRecord::Relation.class_eval do
|
10
|
+
include ActiveRecord::HintMethods
|
11
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
class Arel::Visitors::MySQL
|
2
|
+
INDEX_HINT_BY_TABLE_THREAD_KEY = "INDEX_HINT_BY_TABLE_THREAD_KEY"
|
3
|
+
|
4
|
+
def accept_with_index_hint(object)
|
5
|
+
retval = accept_without_index_hint(object)
|
6
|
+
clear_index_hint_by_table
|
7
|
+
retval
|
8
|
+
end
|
9
|
+
|
10
|
+
alias_method_chain :accept, :index_hint
|
11
|
+
|
12
|
+
def visit_Arel_Table_with_index_hint(o, a)
|
13
|
+
sql = visit_Arel_Table_without_index_hint(o, a)
|
14
|
+
index_hint = get_index_hint(o.name)
|
15
|
+
|
16
|
+
if index_hint
|
17
|
+
append_index_hint(sql, index_hint)
|
18
|
+
else
|
19
|
+
sql
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
alias_method_chain :visit_Arel_Table, :index_hint
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def get_index_hint(table)
|
28
|
+
index_hint_by_table = Thread.current[INDEX_HINT_BY_TABLE_THREAD_KEY]
|
29
|
+
index_hint_by_table.try(:[], table)
|
30
|
+
end
|
31
|
+
|
32
|
+
def clear_index_hint_by_table
|
33
|
+
Thread.current[INDEX_HINT_BY_TABLE_THREAD_KEY] = nil
|
34
|
+
end
|
35
|
+
|
36
|
+
def append_index_hint(sql, index_hint)
|
37
|
+
sql + " " + index_hint.map {|index, hint_type|
|
38
|
+
index = Array(index)
|
39
|
+
"#{hint_type} INDEX (#{index.join(', ')})"
|
40
|
+
}.join(", ")
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
describe "arel-mysql-index-hint" do
|
2
|
+
describe "#eager_load" do
|
3
|
+
context "when single index" do
|
4
|
+
subject do
|
5
|
+
User.
|
6
|
+
eager_load(:microposts).
|
7
|
+
where(microposts: {id: 1}).
|
8
|
+
hint(microposts: {index_microposts_on_user_id_and_created_at: hint_type}).
|
9
|
+
to_sql
|
10
|
+
end
|
11
|
+
|
12
|
+
let(:sql) do
|
13
|
+
"SELECT `users`.`id` AS t0_r0, `users`.`name` AS t0_r1, `users`.`email` AS t0_r2, `users`.`created_at` AS t0_r3, `users`.`updated_at` AS t0_r4, `users`.`password_digest` AS t0_r5, `users`.`remember_token` AS t0_r6, `users`.`admin` AS t0_r7, `microposts`.`id` AS t1_r0, `microposts`.`content` AS t1_r1, `microposts`.`user_id` AS t1_r2, `microposts`.`created_at` AS t1_r3, `microposts`.`updated_at` AS t1_r4 " +
|
14
|
+
"FROM `users` " +
|
15
|
+
"LEFT OUTER JOIN `microposts` " +
|
16
|
+
"#{hint_type} INDEX (index_microposts_on_user_id_and_created_at) " +
|
17
|
+
"ON `microposts`.`user_id` = `users`.`id` " +
|
18
|
+
"WHERE `microposts`.`id` = 1"
|
19
|
+
end
|
20
|
+
|
21
|
+
let(:hint_type) { :force }
|
22
|
+
|
23
|
+
it { is_expected.to eq sql }
|
24
|
+
end
|
25
|
+
|
26
|
+
context "when multiple indexes" do
|
27
|
+
subject do
|
28
|
+
User.
|
29
|
+
eager_load(:microposts).
|
30
|
+
where(microposts: {id: 1}).
|
31
|
+
hint(
|
32
|
+
users: {index_users_on_email: hint_type},
|
33
|
+
microposts: {index_microposts_on_user_id_and_created_at: hint_type},
|
34
|
+
).
|
35
|
+
to_sql
|
36
|
+
end
|
37
|
+
|
38
|
+
let(:sql) do
|
39
|
+
"SELECT `users`.`id` AS t0_r0, `users`.`name` AS t0_r1, `users`.`email` AS t0_r2, `users`.`created_at` AS t0_r3, `users`.`updated_at` AS t0_r4, `users`.`password_digest` AS t0_r5, `users`.`remember_token` AS t0_r6, `users`.`admin` AS t0_r7, `microposts`.`id` AS t1_r0, `microposts`.`content` AS t1_r1, `microposts`.`user_id` AS t1_r2, `microposts`.`created_at` AS t1_r3, `microposts`.`updated_at` AS t1_r4 " +
|
40
|
+
"FROM `users` " +
|
41
|
+
"force INDEX (index_users_on_email) " +
|
42
|
+
"LEFT OUTER JOIN `microposts` " +
|
43
|
+
"#{hint_type} INDEX (index_microposts_on_user_id_and_created_at) " +
|
44
|
+
"ON `microposts`.`user_id` = `users`.`id` " +
|
45
|
+
"WHERE `microposts`.`id` = 1"
|
46
|
+
end
|
47
|
+
|
48
|
+
let(:hint_type) { :force }
|
49
|
+
|
50
|
+
it { is_expected.to eq sql }
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
describe "arel-mysql-index-hint" do
|
2
|
+
describe "#includes" do
|
3
|
+
context "when single index" do
|
4
|
+
subject do
|
5
|
+
User.
|
6
|
+
includes(:microposts).
|
7
|
+
where(microposts: {id: 1}).
|
8
|
+
hint(microposts: {index_microposts_on_user_id_and_created_at: hint_type}).
|
9
|
+
to_sql
|
10
|
+
end
|
11
|
+
|
12
|
+
let(:sql) do
|
13
|
+
"SELECT `users`.`id` AS t0_r0, `users`.`name` AS t0_r1, `users`.`email` AS t0_r2, `users`.`created_at` AS t0_r3, `users`.`updated_at` AS t0_r4, `users`.`password_digest` AS t0_r5, `users`.`remember_token` AS t0_r6, `users`.`admin` AS t0_r7, `microposts`.`id` AS t1_r0, `microposts`.`content` AS t1_r1, `microposts`.`user_id` AS t1_r2, `microposts`.`created_at` AS t1_r3, `microposts`.`updated_at` AS t1_r4 " +
|
14
|
+
"FROM `users` " +
|
15
|
+
"LEFT OUTER JOIN `microposts` " +
|
16
|
+
"#{hint_type} INDEX (index_microposts_on_user_id_and_created_at) " +
|
17
|
+
"ON `microposts`.`user_id` = `users`.`id` " +
|
18
|
+
"WHERE `microposts`.`id` = 1"
|
19
|
+
end
|
20
|
+
|
21
|
+
let(:hint_type) { :force }
|
22
|
+
|
23
|
+
it { is_expected.to eq sql }
|
24
|
+
end
|
25
|
+
|
26
|
+
context "when multiple indexes" do
|
27
|
+
subject do
|
28
|
+
User.
|
29
|
+
includes(:microposts).
|
30
|
+
where(microposts: {id: 1}).
|
31
|
+
hint(
|
32
|
+
users: {index_users_on_email: hint_type},
|
33
|
+
microposts: {index_microposts_on_user_id_and_created_at: hint_type},
|
34
|
+
).
|
35
|
+
to_sql
|
36
|
+
end
|
37
|
+
|
38
|
+
let(:sql) do
|
39
|
+
"SELECT `users`.`id` AS t0_r0, `users`.`name` AS t0_r1, `users`.`email` AS t0_r2, `users`.`created_at` AS t0_r3, `users`.`updated_at` AS t0_r4, `users`.`password_digest` AS t0_r5, `users`.`remember_token` AS t0_r6, `users`.`admin` AS t0_r7, `microposts`.`id` AS t1_r0, `microposts`.`content` AS t1_r1, `microposts`.`user_id` AS t1_r2, `microposts`.`created_at` AS t1_r3, `microposts`.`updated_at` AS t1_r4 " +
|
40
|
+
"FROM `users` " +
|
41
|
+
"force INDEX (index_users_on_email) " +
|
42
|
+
"LEFT OUTER JOIN `microposts` " +
|
43
|
+
"#{hint_type} INDEX (index_microposts_on_user_id_and_created_at) " +
|
44
|
+
"ON `microposts`.`user_id` = `users`.`id` " +
|
45
|
+
"WHERE `microposts`.`id` = 1"
|
46
|
+
end
|
47
|
+
|
48
|
+
let(:hint_type) { :force }
|
49
|
+
|
50
|
+
it { is_expected.to eq sql }
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,105 @@
|
|
1
|
+
describe "arel-mysql-index-hint" do
|
2
|
+
describe "#joins" do
|
3
|
+
context "when single index" do
|
4
|
+
subject do
|
5
|
+
User.
|
6
|
+
joins(:microposts).
|
7
|
+
hint(microposts: {index_microposts_on_user_id_and_created_at: hint_type}).
|
8
|
+
to_sql
|
9
|
+
end
|
10
|
+
|
11
|
+
let(:sql) do
|
12
|
+
"SELECT `users`.* FROM `users` " +
|
13
|
+
"INNER JOIN `microposts` " +
|
14
|
+
"#{hint_type} INDEX (index_microposts_on_user_id_and_created_at) " +
|
15
|
+
"ON `microposts`.`user_id` = `users`.`id`"
|
16
|
+
end
|
17
|
+
|
18
|
+
context "force index" do
|
19
|
+
let(:hint_type) { :force }
|
20
|
+
it { is_expected.to eq sql }
|
21
|
+
end
|
22
|
+
|
23
|
+
context "use index" do
|
24
|
+
let(:hint_type) { :user }
|
25
|
+
it { is_expected.to eq sql }
|
26
|
+
end
|
27
|
+
|
28
|
+
context "ignore index" do
|
29
|
+
let(:hint_type) { :ignore }
|
30
|
+
it { is_expected.to eq sql }
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
context "when multiple indexes" do
|
35
|
+
subject do
|
36
|
+
User.
|
37
|
+
joins(:microposts).
|
38
|
+
hint(
|
39
|
+
users: {index_users_on_email: hint_type},
|
40
|
+
microposts: {index_microposts_on_user_id_and_created_at: hint_type},
|
41
|
+
).
|
42
|
+
to_sql
|
43
|
+
end
|
44
|
+
|
45
|
+
let(:sql) do
|
46
|
+
"SELECT `users`.* FROM `users` " +
|
47
|
+
"#{hint_type} INDEX (index_users_on_email) " +
|
48
|
+
"INNER JOIN `microposts` " +
|
49
|
+
"#{hint_type} INDEX (index_microposts_on_user_id_and_created_at) " +
|
50
|
+
"ON `microposts`.`user_id` = `users`.`id`"
|
51
|
+
end
|
52
|
+
|
53
|
+
context "force index" do
|
54
|
+
let(:hint_type) { :force }
|
55
|
+
it { is_expected.to eq sql }
|
56
|
+
end
|
57
|
+
|
58
|
+
context "use index" do
|
59
|
+
let(:hint_type) { :user }
|
60
|
+
it { is_expected.to eq sql }
|
61
|
+
end
|
62
|
+
|
63
|
+
context "ignore index" do
|
64
|
+
let(:hint_type) { :ignore }
|
65
|
+
it { is_expected.to eq sql }
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
context "when add hint before joins" do
|
70
|
+
subject do
|
71
|
+
User.
|
72
|
+
hint(microposts: {index_microposts_on_user_id_and_created_at: hint_type}).
|
73
|
+
joins(:microposts).
|
74
|
+
to_sql
|
75
|
+
end
|
76
|
+
|
77
|
+
let(:sql) do
|
78
|
+
"SELECT `users`.* FROM `users` " +
|
79
|
+
"INNER JOIN `microposts` " +
|
80
|
+
"#{hint_type} INDEX (index_microposts_on_user_id_and_created_at) " +
|
81
|
+
"ON `microposts`.`user_id` = `users`.`id`"
|
82
|
+
end
|
83
|
+
|
84
|
+
let(:hint_type) { :force }
|
85
|
+
|
86
|
+
it { is_expected.to eq sql }
|
87
|
+
end
|
88
|
+
|
89
|
+
context "when without index" do
|
90
|
+
subject do
|
91
|
+
User.
|
92
|
+
joins(:microposts).
|
93
|
+
to_sql
|
94
|
+
end
|
95
|
+
|
96
|
+
let(:sql) do
|
97
|
+
"SELECT `users`.* FROM `users` " +
|
98
|
+
"INNER JOIN `microposts` " +
|
99
|
+
"ON `microposts`.`user_id` = `users`.`id`"
|
100
|
+
end
|
101
|
+
|
102
|
+
it { is_expected.to eq sql }
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
describe "arel-mysql-index-hint" do
|
2
|
+
describe "#preload" do
|
3
|
+
subject do
|
4
|
+
User.
|
5
|
+
where(id: 1).
|
6
|
+
preload(:microposts).
|
7
|
+
hint(users: {index_users_on_email: hint_type}).
|
8
|
+
to_sql
|
9
|
+
end
|
10
|
+
|
11
|
+
let(:sql) do
|
12
|
+
"SELECT `users`.* " +
|
13
|
+
"FROM `users` " +
|
14
|
+
"force INDEX (index_users_on_email) " +
|
15
|
+
"WHERE `users`.`id` = 1"
|
16
|
+
end
|
17
|
+
|
18
|
+
let(:hint_type) { :force }
|
19
|
+
|
20
|
+
it { is_expected.to eq sql }
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,103 @@
|
|
1
|
+
describe "arel-mysql-index-hint" do
|
2
|
+
describe "#all" do
|
3
|
+
subject do
|
4
|
+
User.
|
5
|
+
all.
|
6
|
+
hint(users: {index_users_on_email: hint_type}).
|
7
|
+
to_sql
|
8
|
+
end
|
9
|
+
|
10
|
+
let(:sql) do
|
11
|
+
"SELECT `users`.* " +
|
12
|
+
"FROM `users` " +
|
13
|
+
"#{hint_type} INDEX (index_users_on_email)"
|
14
|
+
end
|
15
|
+
|
16
|
+
let(:hint_type) { :force }
|
17
|
+
|
18
|
+
it { is_expected.to eq sql }
|
19
|
+
end
|
20
|
+
|
21
|
+
describe "call nothing" do
|
22
|
+
subject do
|
23
|
+
User.
|
24
|
+
hint(users: {index_users_on_email: hint_type}).
|
25
|
+
to_sql
|
26
|
+
end
|
27
|
+
|
28
|
+
let(:sql) do
|
29
|
+
"SELECT `users`.* " +
|
30
|
+
"FROM `users` " +
|
31
|
+
"#{hint_type} INDEX (index_users_on_email)"
|
32
|
+
end
|
33
|
+
|
34
|
+
let(:hint_type) { :force }
|
35
|
+
|
36
|
+
it { is_expected.to eq sql }
|
37
|
+
end
|
38
|
+
|
39
|
+
describe "#limit" do
|
40
|
+
subject do
|
41
|
+
User.
|
42
|
+
limit(1).
|
43
|
+
hint(users: {index_users_on_email: hint_type}).
|
44
|
+
to_sql
|
45
|
+
end
|
46
|
+
|
47
|
+
let(:sql) do
|
48
|
+
"SELECT `users`.* " +
|
49
|
+
"FROM `users` " +
|
50
|
+
"#{hint_type} INDEX (index_users_on_email) " +
|
51
|
+
"LIMIT 1"
|
52
|
+
end
|
53
|
+
|
54
|
+
let(:hint_type) { :force }
|
55
|
+
|
56
|
+
it { is_expected.to eq sql }
|
57
|
+
end
|
58
|
+
|
59
|
+
describe "#first" do
|
60
|
+
subject do
|
61
|
+
User.
|
62
|
+
hint(users: {index_users_on_email: hint_type}).
|
63
|
+
first
|
64
|
+
end
|
65
|
+
|
66
|
+
let(:sql) do
|
67
|
+
"SELECT `users`.* FROM `users` " +
|
68
|
+
"#{hint_type} INDEX (index_users_on_email) " +
|
69
|
+
"ORDER BY `users`.`id` ASC " +
|
70
|
+
"LIMIT 1"
|
71
|
+
end
|
72
|
+
|
73
|
+
let(:hint_type) { :force }
|
74
|
+
|
75
|
+
it do
|
76
|
+
subject
|
77
|
+
|
78
|
+
expect(sql_log).to include sql
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
describe "#take" do
|
83
|
+
subject do
|
84
|
+
User.
|
85
|
+
hint(users: {index_users_on_email: hint_type}).
|
86
|
+
take
|
87
|
+
end
|
88
|
+
|
89
|
+
let(:sql) do
|
90
|
+
"SELECT `users`.* FROM `users` " +
|
91
|
+
"#{hint_type} INDEX (index_users_on_email) " +
|
92
|
+
"LIMIT 1"
|
93
|
+
end
|
94
|
+
|
95
|
+
let(:hint_type) { :force }
|
96
|
+
|
97
|
+
it do
|
98
|
+
subject
|
99
|
+
|
100
|
+
expect(sql_log).to include sql
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
data/spec/init.sql
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
# original: https://github.com/railstutorial/sample_app_rails_4
|
2
|
+
|
3
|
+
DROP DATABASE IF EXISTS `arel_mysql_index_hint_test`;
|
4
|
+
CREATE DATABASE `arel_mysql_index_hint_test` DEFAULT CHARSET=utf8;
|
5
|
+
|
6
|
+
USE `arel_mysql_index_hint_test`;
|
7
|
+
|
8
|
+
DROP TABLE IF EXISTS `microposts`;
|
9
|
+
CREATE TABLE `microposts` (
|
10
|
+
`id` int(11) NOT NULL AUTO_INCREMENT,
|
11
|
+
`content` varchar(255) DEFAULT NULL,
|
12
|
+
`user_id` int(11) DEFAULT NULL,
|
13
|
+
`created_at` datetime DEFAULT NULL,
|
14
|
+
`updated_at` datetime DEFAULT NULL,
|
15
|
+
PRIMARY KEY (`id`),
|
16
|
+
KEY `index_microposts_on_user_id_and_created_at` (`user_id`,`created_at`)
|
17
|
+
) DEFAULT CHARSET=utf8;
|
18
|
+
|
19
|
+
DROP TABLE IF EXISTS `relationships`;
|
20
|
+
CREATE TABLE `relationships` (
|
21
|
+
`id` int(11) NOT NULL AUTO_INCREMENT,
|
22
|
+
`follower_id` int(11) DEFAULT NULL,
|
23
|
+
`followed_id` int(11) DEFAULT NULL,
|
24
|
+
`created_at` datetime DEFAULT NULL,
|
25
|
+
`updated_at` datetime DEFAULT NULL,
|
26
|
+
PRIMARY KEY (`id`),
|
27
|
+
UNIQUE KEY `index_relationships_on_follower_id_and_followed_id` (`follower_id`,`followed_id`),
|
28
|
+
KEY `index_relationships_on_followed_id` (`followed_id`),
|
29
|
+
KEY `index_relationships_on_follower_id` (`follower_id`)
|
30
|
+
) DEFAULT CHARSET=utf8;
|
31
|
+
|
32
|
+
DROP TABLE IF EXISTS `users`;
|
33
|
+
CREATE TABLE `users` (
|
34
|
+
`id` int(11) NOT NULL AUTO_INCREMENT,
|
35
|
+
`name` varchar(255) DEFAULT NULL,
|
36
|
+
`email` varchar(255) DEFAULT NULL,
|
37
|
+
`created_at` datetime DEFAULT NULL,
|
38
|
+
`updated_at` datetime DEFAULT NULL,
|
39
|
+
`password_digest` varchar(255) DEFAULT NULL,
|
40
|
+
`remember_token` varchar(255) DEFAULT NULL,
|
41
|
+
`admin` tinyint(1) DEFAULT NULL,
|
42
|
+
PRIMARY KEY (`id`),
|
43
|
+
UNIQUE KEY `index_users_on_email` (`email`),
|
44
|
+
KEY `index_users_on_remember_token` (`remember_token`)
|
45
|
+
) DEFAULT CHARSET=utf8;
|
data/spec/models.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# original: https://github.com/railstutorial/sample_app_rails_4
|
2
|
+
require "factory_girl"
|
3
|
+
|
4
|
+
class Micropost < ActiveRecord::Base
|
5
|
+
belongs_to :user
|
6
|
+
end
|
7
|
+
|
8
|
+
class Relationship < ActiveRecord::Base
|
9
|
+
belongs_to :follower, class_name: "User"
|
10
|
+
belongs_to :followed, class_name: "User"
|
11
|
+
end
|
12
|
+
|
13
|
+
class User < ActiveRecord::Base
|
14
|
+
has_many :microposts, dependent: :destroy
|
15
|
+
has_many :relationships, foreign_key: "follower_id", dependent: :destroy
|
16
|
+
has_many :followed_users, through: :relationships, source: :followed
|
17
|
+
has_many :reverse_relationships, foreign_key: "followed_id",
|
18
|
+
class_name: "Relationship",
|
19
|
+
dependent: :destroy
|
20
|
+
has_many :followers, through: :reverse_relationships, source: :follower
|
21
|
+
end
|
22
|
+
|
23
|
+
FactoryGirl.define do
|
24
|
+
factory :user do
|
25
|
+
sequence(:name) { |n| "Person #{n}" }
|
26
|
+
sequence(:email) { |n| "person_#{n}@example.com"}
|
27
|
+
|
28
|
+
factory :admin do
|
29
|
+
admin true
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
factory :micropost do
|
34
|
+
content "Lorem ipsum"
|
35
|
+
user
|
36
|
+
end
|
37
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
if ENV['TRAVIS']
|
2
|
+
require 'simplecov'
|
3
|
+
require 'coveralls'
|
4
|
+
|
5
|
+
SimpleCov.formatter = Coveralls::SimpleCov::Formatter
|
6
|
+
|
7
|
+
SimpleCov.start do
|
8
|
+
add_filter "spec/"
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
require "arel-mysql-index-hint"
|
13
|
+
require "models"
|
14
|
+
|
15
|
+
$__arel_mysql_index_hint_sql_log__ = []
|
16
|
+
|
17
|
+
ActiveSupport::Notifications.subscribe('sql.active_record') do |name, start, finish, id, payload|
|
18
|
+
$__arel_mysql_index_hint_sql_log__ << payload[:sql]
|
19
|
+
end
|
20
|
+
|
21
|
+
RSpec.configure do |config|
|
22
|
+
config.before(:all) do
|
23
|
+
init_database
|
24
|
+
|
25
|
+
ActiveRecord::Base.establish_connection(
|
26
|
+
adapter: 'mysql2',
|
27
|
+
database: 'arel_mysql_index_hint_test'
|
28
|
+
)
|
29
|
+
end
|
30
|
+
|
31
|
+
config.before(:each) do
|
32
|
+
$__arel_mysql_index_hint_sql_log__.clear
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def init_database
|
37
|
+
sql_file = File.expand_path('../init.sql', __FILE__)
|
38
|
+
system("mysql -uroot < #{sql_file}")
|
39
|
+
end
|
40
|
+
|
41
|
+
def sql_log
|
42
|
+
$__arel_mysql_index_hint_sql_log__.dup
|
43
|
+
end
|
metadata
ADDED
@@ -0,0 +1,185 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: arel-mysql-index-hint
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Genki Sugawara
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-04-18 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activerecord
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 4.1.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 4.1.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: arel
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 5.0.0
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 5.0.0
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: mysql2
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rspec
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - '>='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 3.0.0
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - '>='
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 3.0.0
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: factory_girl
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - '>='
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - '>='
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: coveralls
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - '>='
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - '>='
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
description: Add index hint to MySQL query in Arel.
|
126
|
+
email:
|
127
|
+
- sgwr_dts@yahoo.co.jp
|
128
|
+
executables: []
|
129
|
+
extensions: []
|
130
|
+
extra_rdoc_files: []
|
131
|
+
files:
|
132
|
+
- .gitignore
|
133
|
+
- .rspec
|
134
|
+
- .travis.yml
|
135
|
+
- Gemfile
|
136
|
+
- LICENSE.txt
|
137
|
+
- README.md
|
138
|
+
- Rakefile
|
139
|
+
- Schemafile
|
140
|
+
- arel-mysql-index-hint.gemspec
|
141
|
+
- lib/arel-mysql-index-hint.rb
|
142
|
+
- lib/arel-mysql-index-hint/active_record-hint_methods.rb
|
143
|
+
- lib/arel-mysql-index-hint/active_record-querying.rb
|
144
|
+
- lib/arel-mysql-index-hint/arel-visitors-mysql.rb
|
145
|
+
- spec/arel-mysql-index-hint/eager_load_spec.rb
|
146
|
+
- spec/arel-mysql-index-hint/includes_spec.rb
|
147
|
+
- spec/arel-mysql-index-hint/joins_spec.rb
|
148
|
+
- spec/arel-mysql-index-hint/preload_spec.rb
|
149
|
+
- spec/arel-mysql-index-hint/without_join_spec.rb
|
150
|
+
- spec/init.sql
|
151
|
+
- spec/models.rb
|
152
|
+
- spec/spec_helper.rb
|
153
|
+
homepage: https://github.com/winebarrel/arel-mysql-index-hint
|
154
|
+
licenses:
|
155
|
+
- MIT
|
156
|
+
metadata: {}
|
157
|
+
post_install_message:
|
158
|
+
rdoc_options: []
|
159
|
+
require_paths:
|
160
|
+
- lib
|
161
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
162
|
+
requirements:
|
163
|
+
- - '>='
|
164
|
+
- !ruby/object:Gem::Version
|
165
|
+
version: '0'
|
166
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
167
|
+
requirements:
|
168
|
+
- - '>='
|
169
|
+
- !ruby/object:Gem::Version
|
170
|
+
version: '0'
|
171
|
+
requirements: []
|
172
|
+
rubyforge_project:
|
173
|
+
rubygems_version: 2.0.14
|
174
|
+
signing_key:
|
175
|
+
specification_version: 4
|
176
|
+
summary: Add index hint to MySQL query in Arel.
|
177
|
+
test_files:
|
178
|
+
- spec/arel-mysql-index-hint/eager_load_spec.rb
|
179
|
+
- spec/arel-mysql-index-hint/includes_spec.rb
|
180
|
+
- spec/arel-mysql-index-hint/joins_spec.rb
|
181
|
+
- spec/arel-mysql-index-hint/preload_spec.rb
|
182
|
+
- spec/arel-mysql-index-hint/without_join_spec.rb
|
183
|
+
- spec/init.sql
|
184
|
+
- spec/models.rb
|
185
|
+
- spec/spec_helper.rb
|