query_optimizer 0.1.3 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a84bf981c1b8a7f3c07f43590478b70f7229a288
4
- data.tar.gz: d8002be95bb78fd6e9abfb17a9581f5a47d1a373
3
+ metadata.gz: 44b32ac494ec80e6312efc98513f669dffbc1c55
4
+ data.tar.gz: 67212322656adebcdfa0844d9a67af76b0e49732
5
5
  SHA512:
6
- metadata.gz: 7f4dee4b8038e7e154d74d58cb7a40ca71751d1a640ff266a7c3e25ec1e8cde98e4514c4191a245fea7c6b2e1073aafd7b1c8e74320907ad8de3791b8150bc82
7
- data.tar.gz: e53f70803071d10027112d704e82afcea065cbcab1b7f32f4d5b229e314972b72d22522aaf4db87b19f1f36a84f2e33a8725ca72330add95a1c925be99b87be5
6
+ metadata.gz: c8525bdf459d7fa634a5d8364e659261b126811b63c783dd67fe7b2ee2bcf3adce82361a88652a9bd5a859d1a22f78246febc82048bed06cbc853a66df2d687e
7
+ data.tar.gz: f6149a7af5461d70027a6a8c6f4a94d8a7ecfe6cdc04ee19645ee4af8f55525005b4c0c4eb24aae7a79cfb27c29d99503b4aac28a5a9c9c50a6dfe936a92432f
data/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  # QueryOptimizer
2
2
 
3
- This is rails gem which will minimize query for many_to_many relationship.
4
- Minimize query by query_optimizer just pass your model name as QueryOptimizer.optimize_query(Model1,Model2)
3
+ This is rails gem which will minimize query for one-to-one, one-to-many database relationships.
4
+ Minimize query by query_optimizer just pass your table name as Post.optimize_query(:comments)
5
5
 
6
6
  ## Installation
7
7
 
data/README.rdoc CHANGED
@@ -6,7 +6,7 @@
6
6
 
7
7
  == FEATURES:
8
8
 
9
- query_optimizer is the best gem for optimizing query in rails for has_many and belongs_to relationship two tables.
9
+ query_optimizer is the best gem for optimizing query in rails for has_many or has_one and belongs_to relationship two tables.
10
10
  In rails If you want all posts and its related comments in a single query than you can't get it
11
11
  as Post.incldes(:comments) or Post.joins(:comments) will return only posts array and Comment.incldes(:posts) will return only comments array
12
12
  For fetching each post comment you have to fire query from view as below output of post.comments in each with index loop.
@@ -29,16 +29,230 @@ For fetching each post comment you have to fire query from view as below output
29
29
  Rendered posts/index.html.erb within layouts/application (31.7ms)
30
30
 
31
31
  == DESCRIPTION:
32
- so to minimize query you can use query_optimizer you can just pass your model name as QueryOptimizer.optimize_query(Model1,Model2)
32
+ so to minimize query you can use query_optimizer you can just pass your table name as Post.optimize_query(:comments)
33
33
 
34
34
  require 'query_optimizer'
35
- QueryOptimizer.optimize_query(Post,Comment)
35
+ Post.optimize_query(:comments)
36
36
 
37
- (1.1ms) SELECT posts.id AS posts_id,posts.name AS posts_name,posts.description AS posts_description,posts.created_at AS posts_created_at,posts.updated_at AS posts_updated_at,comments.id AS comments_id,comments.name AS comments_name,comments.description AS comments_description,comments.post_id AS comments_post_id,comments.created_at AS comments_created_at,comments.updated_at AS comments_updated_at FROM posts, comments where posts.id=comments.post_id
37
+ (1.1ms) SELECT posts.id AS posts_id,posts.name AS posts_name,posts.description AS posts_description,posts.created_at AS posts_created_at,posts.updated_at AS posts_updated_at,posts.author_id AS posts_author_id,comments.id AS comments_id,comments.name AS comments_name,comments.description AS comments_description,comments.post_id AS comments_post_id,comments.created_at AS comments_created_at,comments.updated_at AS comments_updated_at FROM posts,comments where posts.id=comments.post_id
38
38
 
39
- => [{"posts_id"=>44, "posts_name"=>"name1", "posts_description"=>"desc1", "posts_created_at"=>2015-02-01 10:40:05 UTC, "posts_updated_at"=>2015-02-01 10:40:05 UTC, "comments_id"=>4, "comments_name"=>"gdfdhddffdd", "comments_description"=>nil, "comments_post_id"=>44, "comments_created_at"=>2015-10-10 16:49:12 UTC, "comments_updated_at"=>2015-10-10 16:49:12 UTC}, {"posts_id"=>45, "posts_name"=>"name2", "posts_description"=>"desc2", "posts_created_at"=>2015-02-01 10:40:05 UTC, "posts_updated_at"=>2015-02-01 10:40:05 UTC, "comments_id"=>5, "comments_name"=>"dhdfdfhdhdfhfd", "comments_description"=>nil, "comments_post_id"=>45, "comments_created_at"=>2015-10-10 16:49:19 UTC, "comments_updated_at"=>2015-10-10 16:49:19 UTC}, {"posts_id"=>44, "posts_name"=>"name3", "posts_description"=>"desc3", "posts_created_at"=>2015-02-01 10:40:05 UTC, "posts_updated_at"=>2015-02-01 10:40:05 UTC, "comments_id"=>6, "comments_name"=>"hfdhdfhdf", "comments_description"=>nil, "comments_post_id"=>44, "comments_created_at"=>2015-10-10 16:51:26 UTC, "comments_updated_at"=>2015-10-10 16:51:26 UTC}, {"posts_id"=>46, "posts_name"=>"name3", "posts_description"=>"desc3", "posts_created_at"=>2015-02-01 10:41:14 UTC, "posts_updated_at"=>2015-02-01 10:41:14 UTC, "comments_id"=>7, "comments_name"=>"dhdfdfhdfhdf", "comments_description"=>nil, "comments_post_id"=>46, "comments_created_at"=>2015-10-10 16:51:32 UTC, "comments_updated_at"=>2015-10-10 16:51:32 UTC}]
39
+ => [{"posts_id"=>46,
40
+ "posts_name"=>"posts3",
41
+ "posts_description"=>"description3",
42
+ "posts_created_at"=>2015-02-01 10:41:14 UTC,
43
+ "posts_updated_at"=>2015-02-01 10:41:14 UTC,
44
+ "posts_author_id"=>2,
45
+ "comments_id"=>7,
46
+ "comments_name"=>"comment4",
47
+ "comments_description"=>"desc4",
48
+ "comments_post_id"=>46,
49
+ "comments_created_at"=>2015-10-10 16:51:32 UTC,
50
+ "comments_updated_at"=>2015-10-10 16:51:32 UTC},
51
+ {"posts_id"=>45,
52
+ "posts_name"=>"posts2",
53
+ "posts_description"=>"descriptio2",
54
+ "posts_created_at"=>2015-02-01 10:40:05 UTC,
55
+ "posts_updated_at"=>2015-02-01 10:40:05 UTC,
56
+ "posts_author_id"=>1,
57
+ "comments_id"=>5,
58
+ "comments_name"=>"comment2",
59
+ "comments_description"=>"desc2",
60
+ "comments_post_id"=>45,
61
+ "comments_created_at"=>2015-10-10 16:49:19 UTC,
62
+ "comments_updated_at"=>2015-10-10 16:49:19 UTC},
63
+ {"posts_id"=>44,
64
+ "posts_name"=>"postst1",
65
+ "posts_description"=>"description1",
66
+ "posts_created_at"=>2015-02-01 10:40:05 UTC,
67
+ "posts_updated_at"=>2015-02-01 10:40:05 UTC,
68
+ "posts_author_id"=>1,
69
+ "comments_id"=>6,
70
+ "comments_name"=>"comment3",
71
+ "comments_description"=>"desc3",
72
+ "comments_post_id"=>44,
73
+ "comments_created_at"=>2015-10-10 16:51:26 UTC,
74
+ "comments_updated_at"=>2015-10-10 16:51:26 UTC},
75
+ {"posts_id"=>44,
76
+ "posts_name"=>"postst1",
77
+ "posts_description"=>"description1",
78
+ "posts_created_at"=>2015-02-01 10:40:05 UTC,
79
+ "posts_updated_at"=>2015-02-01 10:40:05 UTC,
80
+ "posts_author_id"=>1,
81
+ "comments_id"=>4,
82
+ "comments_name"=>"comment1",
83
+ "comments_description"=>"desc1",
84
+ "comments_post_id"=>44,
85
+ "comments_created_at"=>2015-10-10 16:49:12 UTC,
86
+ "comments_updated_at"=>2015-10-10 16:49:12 UTC}]
87
+
88
+ It will return posts and its related comments in a single array.
89
+
90
+ It also works for multiple tables.If you want common data from posts,comments,replies table than you can use it as below
91
+
92
+ Post.optimize_query(:comments,:replies)
93
+
94
+ (1.2ms) SELECT posts.id AS posts_id,posts.name AS posts_name,posts.description AS posts_description,posts.created_at AS posts_created_at,posts.updated_at AS posts_updated_at,posts.author_id AS posts_author_id,comments.id AS comments_id,comments.name AS comments_name,comments.description AS comments_description,comments.post_id AS comments_post_id,comments.created_at AS comments_created_at,comments.updated_at AS comments_updated_at,replies.id AS replies_id,replies.description AS replies_description,replies.comment_id AS replies_comment_id,replies.created_at AS replies_created_at,replies.updated_at AS replies_updated_at FROM posts,comments,replies where posts.id=comments.post_id AND comments.id=replies.comment_id
95
+
96
+ => [{"posts_id"=>46,
97
+ "posts_name"=>"posts3",
98
+ "posts_description"=>"description3",
99
+ "posts_created_at"=>2015-02-01 10:41:14 UTC,
100
+ "posts_updated_at"=>2015-02-01 10:41:14 UTC,
101
+ "posts_author_id"=>2,
102
+ "comments_id"=>7,
103
+ "comments_name"=>"comment4",
104
+ "comments_description"=>"desc4",
105
+ "comments_post_id"=>46,
106
+ "comments_created_at"=>2015-10-10 16:51:32 UTC,
107
+ "comments_updated_at"=>2015-10-10 16:51:32 UTC,
108
+ "replies_id"=>4,
109
+ "replies_description"=>"reply for author 2",
110
+ "replies_comment_id"=>7,
111
+ "replies_created_at"=>2015-12-19 19:05:46 UTC,
112
+ "replies_updated_at"=>2015-12-19 19:05:46 UTC},
113
+ {"posts_id"=>45,
114
+ "posts_name"=>"posts2",
115
+ "posts_description"=>"descriptio2",
116
+ "posts_created_at"=>2015-02-01 10:40:05 UTC,
117
+ "posts_updated_at"=>2015-02-01 10:40:05 UTC,
118
+ "posts_author_id"=>1,
119
+ "comments_id"=>5,
120
+ "comments_name"=>"comment2",
121
+ "comments_description"=>"desc2",
122
+ "comments_post_id"=>45,
123
+ "comments_created_at"=>2015-10-10 16:49:19 UTC,
124
+ "comments_updated_at"=>2015-10-10 16:49:19 UTC,
125
+ "replies_id"=>3,
126
+ "replies_description"=>"reply333",
127
+ "replies_comment_id"=>5,
128
+ "replies_created_at"=>2015-12-19 16:52:30 UTC,
129
+ "replies_updated_at"=>2015-12-19 16:52:30 UTC},
130
+ {"posts_id"=>44,
131
+ "posts_name"=>"postst1",
132
+ "posts_description"=>"description1",
133
+ "posts_created_at"=>2015-02-01 10:40:05 UTC,
134
+ "posts_updated_at"=>2015-02-01 10:40:05 UTC,
135
+ "posts_author_id"=>1,
136
+ "comments_id"=>4,
137
+ "comments_name"=>"comment1",
138
+ "comments_description"=>"desc1",
139
+ "comments_post_id"=>44,
140
+ "comments_created_at"=>2015-10-10 16:49:12 UTC,
141
+ "comments_updated_at"=>2015-10-10 16:49:12 UTC,
142
+ "replies_id"=>2,
143
+ "replies_description"=>"reply222",
144
+ "replies_comment_id"=>4,
145
+ "replies_created_at"=>2015-12-19 16:52:16 UTC,
146
+ "replies_updated_at"=>2015-12-19 16:52:16 UTC},
147
+ {"posts_id"=>44,
148
+ "posts_name"=>"postst1",
149
+ "posts_description"=>"description1",
150
+ "posts_created_at"=>2015-02-01 10:40:05 UTC,
151
+ "posts_updated_at"=>2015-02-01 10:40:05 UTC,
152
+ "posts_author_id"=>1,
153
+ "comments_id"=>4,
154
+ "comments_name"=>"comment1",
155
+ "comments_description"=>"desc1",
156
+ "comments_post_id"=>44,
157
+ "comments_created_at"=>2015-10-10 16:49:12 UTC,
158
+ "comments_updated_at"=>2015-10-10 16:49:12 UTC,
159
+ "replies_id"=>1,
160
+ "replies_description"=>"reply111111111",
161
+ "replies_comment_id"=>4,
162
+ "replies_created_at"=>2015-12-19 16:52:09 UTC,
163
+ "replies_updated_at"=>2015-12-19 16:52:09 UTC}]
164
+
165
+ Just pass multiple table name as arguments as below
166
+
167
+ Author.optimize_query(:posts,:comments,:replies)
168
+
169
+ (1.1ms) SELECT authors.id AS authors_id,authors.name AS authors_name,authors.created_at AS authors_created_at,authors.updated_at AS authors_updated_at,posts.id AS posts_id,posts.name AS posts_name,posts.description AS posts_description,posts.created_at AS posts_created_at,posts.updated_at AS posts_updated_at,posts.author_id AS posts_author_id,comments.id AS comments_id,comments.name AS comments_name,comments.description AS comments_description,comments.post_id AS comments_post_id,comments.created_at AS comments_created_at,comments.updated_at AS comments_updated_at,replies.id AS replies_id,replies.description AS replies_description,replies.comment_id AS replies_comment_id,replies.created_at AS replies_created_at,replies.updated_at AS replies_updated_at FROM authors,posts,comments,replies where authors.id=posts.author_id AND posts.id=comments.post_id AND comments.id=replies.comment_id
170
+
171
+ => [{"authors_id"=>2,
172
+ "authors_name"=>"Author2",
173
+ "authors_created_at"=>2015-12-19 17:46:39 UTC,
174
+ "authors_updated_at"=>2015-12-19 17:46:39 UTC,
175
+ "posts_id"=>46,
176
+ "posts_name"=>"posts3",
177
+ "posts_description"=>"description3",
178
+ "posts_created_at"=>2015-02-01 10:41:14 UTC,
179
+ "posts_updated_at"=>2015-02-01 10:41:14 UTC,
180
+ "posts_author_id"=>2,
181
+ "comments_id"=>7,
182
+ "comments_name"=>"comment4",
183
+ "comments_description"=>"desc4",
184
+ "comments_post_id"=>46,
185
+ "comments_created_at"=>2015-10-10 16:51:32 UTC,
186
+ "comments_updated_at"=>2015-10-10 16:51:32 UTC,
187
+ "replies_id"=>4,
188
+ "replies_description"=>"reply for author 2",
189
+ "replies_comment_id"=>7,
190
+ "replies_created_at"=>2015-12-19 19:05:46 UTC,
191
+ "replies_updated_at"=>2015-12-19 19:05:46 UTC},
192
+ {"authors_id"=>1,
193
+ "authors_name"=>"Author1",
194
+ "authors_created_at"=>2015-12-19 17:46:32 UTC,
195
+ "authors_updated_at"=>2015-12-19 17:46:32 UTC,
196
+ "posts_id"=>45,
197
+ "posts_name"=>"posts2",
198
+ "posts_description"=>"descriptio2",
199
+ "posts_created_at"=>2015-02-01 10:40:05 UTC,
200
+ "posts_updated_at"=>2015-02-01 10:40:05 UTC,
201
+ "posts_author_id"=>1,
202
+ "comments_id"=>5,
203
+ "comments_name"=>"comment2",
204
+ "comments_description"=>"desc2",
205
+ "comments_post_id"=>45,
206
+ "comments_created_at"=>2015-10-10 16:49:19 UTC,
207
+ "comments_updated_at"=>2015-10-10 16:49:19 UTC,
208
+ "replies_id"=>3,
209
+ "replies_description"=>"reply333",
210
+ "replies_comment_id"=>5,
211
+ "replies_created_at"=>2015-12-19 16:52:30 UTC,
212
+ "replies_updated_at"=>2015-12-19 16:52:30 UTC},
213
+ {"authors_id"=>1,
214
+ "authors_name"=>"Author1",
215
+ "authors_created_at"=>2015-12-19 17:46:32 UTC,
216
+ "authors_updated_at"=>2015-12-19 17:46:32 UTC,
217
+ "posts_id"=>44,
218
+ "posts_name"=>"postst1",
219
+ "posts_description"=>"description1",
220
+ "posts_created_at"=>2015-02-01 10:40:05 UTC,
221
+ "posts_updated_at"=>2015-02-01 10:40:05 UTC,
222
+ "posts_author_id"=>1,
223
+ "comments_id"=>4,
224
+ "comments_name"=>"comment1",
225
+ "comments_description"=>"desc1",
226
+ "comments_post_id"=>44,
227
+ "comments_created_at"=>2015-10-10 16:49:12 UTC,
228
+ "comments_updated_at"=>2015-10-10 16:49:12 UTC,
229
+ "replies_id"=>2,
230
+ "replies_description"=>"reply222",
231
+ "replies_comment_id"=>4,
232
+ "replies_created_at"=>2015-12-19 16:52:16 UTC,
233
+ "replies_updated_at"=>2015-12-19 16:52:16 UTC},
234
+ {"authors_id"=>1,
235
+ "authors_name"=>"Author1",
236
+ "authors_created_at"=>2015-12-19 17:46:32 UTC,
237
+ "authors_updated_at"=>2015-12-19 17:46:32 UTC,
238
+ "posts_id"=>44,
239
+ "posts_name"=>"postst1",
240
+ "posts_description"=>"description1",
241
+ "posts_created_at"=>2015-02-01 10:40:05 UTC,
242
+ "posts_updated_at"=>2015-02-01 10:40:05 UTC,
243
+ "posts_author_id"=>1,
244
+ "comments_id"=>4,
245
+ "comments_name"=>"comment1",
246
+ "comments_description"=>"desc1",
247
+ "comments_post_id"=>44,
248
+ "comments_created_at"=>2015-10-10 16:49:12 UTC,
249
+ "comments_updated_at"=>2015-10-10 16:49:12 UTC,
250
+ "replies_id"=>1,
251
+ "replies_description"=>"reply111111111",
252
+ "replies_comment_id"=>4,
253
+ "replies_created_at"=>2015-12-19 16:52:09 UTC,
254
+ "replies_updated_at"=>2015-12-19 16:52:09 UTC}]
40
255
 
41
- It will return posts and its related comments in a single array
42
256
 
43
257
  == REQUIREMENTS:
44
258
 
@@ -1,3 +1,3 @@
1
1
  module QueryOptimizer
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.4"
3
3
  end
@@ -1,13 +1,24 @@
1
1
  module QueryOptimizer
2
- def self.optimize_query(model1,model2)
3
- table1 = model1.table_name
4
- table2 = model2.table_name
5
- get_singular_name = table1.singularize
6
- table1_columns = model1.column_names
7
- table2_columns = model2.column_names
8
- table1alias = table1_columns.map { |word| "#{table1}.#{word} AS #{table1}_#{word}" }.join(',')
9
- table2alias = table2_columns.map { |word| "#{table2}.#{word} AS #{table2}_#{word}" }.join(',')
10
- results = ActiveRecord::Base.connection.select_all("SELECT #{table1alias},#{table2alias} FROM #{table1}, #{table2} where #{table1}.id=#{table2}.#{get_singular_name}_id").to_a
2
+ def self.included(base)
3
+ base.extend ClassMethods
4
+ end
5
+ module ClassMethods
6
+ def optimize_query(*args)
7
+ joins=[]
8
+ aliases = []
9
+ args.to_a.unshift(self.table_name.to_sym)
10
+ args.each_with_index do |table,index|
11
+ aliases << table.to_s.classify.constantize.column_names.map { |word| "#{table}.#{word} AS #{table}_#{word}" }.join(',')
12
+ next_value = args.to_a[index+1].nil? ? nil : args.to_a[index+1]
13
+ if args.count-2 == index
14
+ joins << "#{table}.id=#{next_value}.#{table.to_s.singularize}_id"
15
+ else
16
+ joins << "#{table}.id=#{next_value}.#{table.to_s.singularize}_id AND" if next_value
17
+ end
18
+ end
19
+ tables = args.map{|p| p.to_s}.join(',')
20
+ return ActiveRecord::Base.connection.select_all("SELECT #{aliases.join(',')} FROM #{tables} where #{joins.join(' ')}").to_a.sort{ |x, y| y["#{args.first.to_s}_id"] <=> x["#{args.first.to_s}_id"] }
21
+ end
11
22
  end
12
23
  end
13
24
 
@@ -8,8 +8,8 @@ Gem::Specification.new do |spec|
8
8
  spec.version = QueryOptimizer::VERSION
9
9
  spec.authors = ["Akshay"]
10
10
  spec.email = ["akshay.goyal1008@gmail.com"]
11
- spec.summary = %q{This is rails gem which will minimize query for many_to_many relationship}
12
- spec.description = %q{This is rails gem which will minimize query for many_to_many relationship.}
11
+ spec.summary = %q{This is rails gem which will minimize query for one-to-one, one-to-many database relationships.}
12
+ spec.description = %q{This is rails gem which will minimize query for one-to-one, one-to-many database relationships.}
13
13
  spec.homepage = ""
14
14
  spec.license = "MIT"
15
15
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: query_optimizer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Akshay
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-10-25 00:00:00.000000000 Z
11
+ date: 2015-12-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -38,7 +38,8 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '10.0'
41
- description: This is rails gem which will minimize query for many_to_many relationship.
41
+ description: This is rails gem which will minimize query for one-to-one, one-to-many
42
+ database relationships.
42
43
  email:
43
44
  - akshay.goyal1008@gmail.com
44
45
  executables: []
@@ -79,5 +80,6 @@ rubyforge_project:
79
80
  rubygems_version: 2.2.2
80
81
  signing_key:
81
82
  specification_version: 4
82
- summary: This is rails gem which will minimize query for many_to_many relationship
83
+ summary: This is rails gem which will minimize query for one-to-one, one-to-many database
84
+ relationships.
83
85
  test_files: []