query_optimizer 0.0.1
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 +7 -0
- data/.gitignore +14 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/LICENSE.txt +22 -0
- data/Manifest +7 -0
- data/README.md +31 -0
- data/README.rdoc +48 -0
- data/Rakefile +2 -0
- data/lib/query_optimizer/version.rb +3 -0
- data/lib/query_optimizer.rb +18 -0
- data/query_optimizer.gemspec +23 -0
- metadata +83 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: b944486a59381999a835a04739a3695e1d6be614
|
4
|
+
data.tar.gz: a297b632e1c138a6715e02eb4a60dfe4219bde26
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6e4c046a2e1cf5cca33c258abee51a3609059b0f3e6b8b4649003dc030b38a1b6a3c5062bf093ffbd5c4a36c706d19de34fc71c32838b9ddb9742991ae4b99df
|
7
|
+
data.tar.gz: 445d202f2bd6c36ea90f90570786a50e00b3d418b8518beffd81dce168bfe5ccedb807fd218cce7e5a70c4e11490fb9af18910b4570a7631a75292b67c1c8af3
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 akshaygoyal88
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
22
|
+
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Akshay
|
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/Manifest
ADDED
data/README.md
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# QueryOptimizer
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'query_optimizer'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install query_optimizer
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
TODO: Write usage instructions here
|
24
|
+
|
25
|
+
## Contributing
|
26
|
+
|
27
|
+
1. Fork it ( https://github.com/[my-github-username]/query_optimizer/fork )
|
28
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
29
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
30
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
31
|
+
5. Create a new Pull Request
|
data/README.rdoc
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
{<img src="https://badge.fury.io/rb/query_optimizer.png" alt="Gem Version" />}[http://badge.fury.io/rb/query_optimizer]
|
2
|
+
|
3
|
+
= query_optimizer
|
4
|
+
|
5
|
+
* http://github.com/akshaygoyal88/query_optimizer
|
6
|
+
|
7
|
+
== FEATURES:
|
8
|
+
|
9
|
+
query_optimizer is the best gem for optimizing query in rails for has_many and belongs_to relationship two tables.
|
10
|
+
In rails If you want all posts and its related comments in a single query than you can't get it
|
11
|
+
as Post.incldes(:comments) or Post.joins(:comments) will return only posts array and Comment.incldes(:posts) will return only comments array
|
12
|
+
For fetching each post comment you have to fire query from view as below output of post.comments in each with index loop.
|
13
|
+
|
14
|
+
Started GET "/" for 127.0.0.1 at 2015-10-18 14:12:11 +0530
|
15
|
+
Processing by PostsController#index as HTML
|
16
|
+
Post Load (0.5ms) SELECT `posts`.* FROM `posts`
|
17
|
+
Comment Load (0.3ms) SELECT `comments`.* FROM `comments` WHERE `comments`.`post_id` = 44
|
18
|
+
Rendered comments/_comment.html.erb (0.5ms)
|
19
|
+
Rendered posts/_post.html.erb (6.3ms)
|
20
|
+
Comment Load (0.6ms) SELECT `comments`.* FROM `comments` WHERE `comments`.`post_id` = 45
|
21
|
+
Rendered comments/_comment.html.erb (0.3ms)
|
22
|
+
Rendered posts/_post.html.erb (5.2ms)
|
23
|
+
Comment Load (0.6ms) SELECT `comments`.* FROM `comments` WHERE `comments`.`post_id` = 46
|
24
|
+
Rendered comments/_comment.html.erb (0.3ms)
|
25
|
+
Rendered posts/_post.html.erb (5.6ms)
|
26
|
+
Comment Load (0.6ms) SELECT `comments`.* FROM `comments` WHERE `comments`.`post_id` = 47
|
27
|
+
Rendered collection (0.0ms)
|
28
|
+
Rendered posts/_post.html.erb (4.1ms)
|
29
|
+
Rendered posts/index.html.erb within layouts/application (31.7ms)
|
30
|
+
|
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)
|
33
|
+
|
34
|
+
require 'query_optimizer'
|
35
|
+
QueryOptimizer.optimize_query(Post,Comment)
|
36
|
+
|
37
|
+
(1.1ms) SELECT posts.*,comments.* FROM posts INNER JOIN comments on posts.id=comments.post_id
|
38
|
+
=> [{"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
|
+
|
40
|
+
It will return posts and its related comments in a single array
|
41
|
+
|
42
|
+
== REQUIREMENTS:
|
43
|
+
|
44
|
+
* rails
|
45
|
+
|
46
|
+
== INSTALL:
|
47
|
+
|
48
|
+
* gem install query_optimizer
|
data/Rakefile
ADDED
@@ -0,0 +1,18 @@
|
|
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
|
+
results = ActiveRecord::Base.connection.execute("SELECT #{table1}.*,#{table2}.* FROM #{table1} INNER JOIN #{table2} on #{table1}.id=#{table2}.#{get_singular_name}_id")
|
7
|
+
table1_columns = model1.column_names
|
8
|
+
table2_columns = model2.column_names
|
9
|
+
table1_columns = table1_columns.map { |word| "#{table1}_#{word}" }
|
10
|
+
table2_columns = table2_columns.map { |word| "#{table2}_#{word}" }
|
11
|
+
both_columns = table1_columns+table2_columns
|
12
|
+
results.to_a.map{|i| Hash[both_columns.zip(i)]}
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
class ActiveRecord::Base
|
17
|
+
include QueryOptimizer
|
18
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'query_optimizer/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "query_optimizer"
|
8
|
+
spec.version = QueryOptimizer::VERSION
|
9
|
+
spec.authors = ["Akshay"]
|
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.}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: query_optimizer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Akshay
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-10-18 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
description: This is rails gem which will minimize query for many_to_many relationship.
|
42
|
+
email:
|
43
|
+
- akshay.goyal1008@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".gitignore"
|
49
|
+
- Gemfile
|
50
|
+
- LICENSE
|
51
|
+
- LICENSE.txt
|
52
|
+
- Manifest
|
53
|
+
- README.md
|
54
|
+
- README.rdoc
|
55
|
+
- Rakefile
|
56
|
+
- lib/query_optimizer.rb
|
57
|
+
- lib/query_optimizer/version.rb
|
58
|
+
- query_optimizer.gemspec
|
59
|
+
homepage: ''
|
60
|
+
licenses:
|
61
|
+
- MIT
|
62
|
+
metadata: {}
|
63
|
+
post_install_message:
|
64
|
+
rdoc_options: []
|
65
|
+
require_paths:
|
66
|
+
- lib
|
67
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '0'
|
72
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
requirements: []
|
78
|
+
rubyforge_project:
|
79
|
+
rubygems_version: 2.2.2
|
80
|
+
signing_key:
|
81
|
+
specification_version: 4
|
82
|
+
summary: This is rails gem which will minimize query for many_to_many relationship
|
83
|
+
test_files: []
|