query-analyzer 0.1.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.
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
@@ -0,0 +1,10 @@
1
+ = Changelog
2
+
3
+ == Version 0.1.1
4
+
5
+ * using jeweler for managing and releasing query-analyzer
6
+
7
+ == Version 0.1.0
8
+
9
+ * Turned it in to a gem.
10
+ * Changed the selection logic such that it shows all queries again.
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "shoulda", ">= 0"
10
+ gem "bundler", "~> 1.0.0"
11
+ gem "jeweler", "~> 1.5.2"
12
+ gem "rcov", ">= 0"
13
+ end
@@ -0,0 +1,20 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ git (1.2.5)
5
+ jeweler (1.5.2)
6
+ bundler (~> 1.0.0)
7
+ git (>= 1.2.5)
8
+ rake
9
+ rake (0.8.7)
10
+ rcov (0.9.9)
11
+ shoulda (2.11.3)
12
+
13
+ PLATFORMS
14
+ ruby
15
+
16
+ DEPENDENCIES
17
+ bundler (~> 1.0.0)
18
+ jeweler (~> 1.5.2)
19
+ rcov
20
+ shoulda
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Ludwig Bratke
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,108 @@
1
+ = Query Analyzer Plugin for MySQL on Rails
2
+
3
+ A gem version of the Query Analyzer plugin. Modified by Todd Tyree.
4
+
5
+ MODIFIED by John Eberly originally take from http://svn.nfectio.us
6
+
7
+ The Query Analyzer plugin will expand the usability of your log files
8
+ by providing query analysis using the MySQL query execution plan. Each SQL
9
+ select query will be 'EXPLAIN'ed and added to the log files right below
10
+ the original query.
11
+
12
+ Using this plugin and a good understanding of the results, you will be
13
+ able to analyze and optimize the queries your application is making.
14
+
15
+ Refer to http://www.mysql.org/doc/refman/5.0/en/explain.html for more
16
+ information on understanding the results.
17
+
18
+ = Compatibility
19
+
20
+ It only works with MySQL at the moment.
21
+
22
+ = Installation
23
+
24
+ sudo gem install git://github.com/tatyree/query-analyzer.git
25
+
26
+ In your environment.rb, *after* the Rails::Initializer block:
27
+
28
+ require 'query_analyzer'
29
+
30
+ Please <b>note the underscore!</b> <tt>require 'query-analyzer'</tt> won't find it.
31
+
32
+ = Example Use
33
+
34
+ Here is a real life usage of the plugin that detected the omission of indexes on
35
+ a table. In this case, it was a join table and the keys didn't have indexes (silly me!).
36
+ Names have been changed to protect the innocent (and make it fit 80 columns)
37
+
38
+ # development.log
39
+
40
+ P Load (0.008669)
41
+ => SELECT p.* FROM p INNER JOIN d ON p.id = d.p_id WHERE (d.p_id = 2 AND ((d.type = 'P')))
42
+
43
+ Analyzing P Load
44
+
45
+ select_type | key_len | type | Extra | id | possible_keys | rows | table | ref | key
46
+ ----------------------------------------------------------------------------------------------------
47
+ SIMPLE | | ALL | Using where | 1 | | 74 | d | |
48
+ SIMPLE | 4 | eq_ref | Using where | 1 | PRIMARY | 1 | p | d.p_id | PRIMARY
49
+
50
+
51
+ = Analyze the results
52
+
53
+ Looking at the results of the execution plan, we can see that the lookup in
54
+ the d table is missing an index (possible_keys=null) and performed a full
55
+ table scan (type=ALL) to satisfy the WHERE condition. In this case, there was only one
56
+ row that matched the condition in the table, but MySQL still had to search all 74 rows
57
+ in the table to find it, a key indicator of a missing
58
+ or malformed index(es). Once it has pulled all the records to satisfy the WHERE, it then
59
+ starts the p table join. This time, it was able to match d.p_id to p.id using
60
+ the PRIMARY key on the p table. The type=eq_ref indicates a 1 to 1 match against a primary
61
+ or unique column.
62
+
63
+ Lets add some indexes to the join table and see if we can cut that full table scan down in the
64
+ number of rows it needs to search.
65
+
66
+ #> script/generate Migration AddIndexesToD
67
+
68
+ # file: 005_add_indexes_to_d
69
+ class AddIndexesToD < ActiveRecord::Migration
70
+ def self.up
71
+ add_index :d, [ :p_id, :type ]
72
+ add_index :d, :type
73
+ end
74
+
75
+ def self.remove
76
+ remove_index :d, [ :p_id, :type ]
77
+ remove_index :d, :type
78
+ end
79
+ end
80
+
81
+
82
+ Now that we have an index on the foreign_key column and type, lets re-run the query and
83
+ see if we got rid of that full table scan.
84
+
85
+ # development.log
86
+
87
+ P Load (0.009011)
88
+ => SELECT p.* FROM p INNER JOIN d ON p.id = d.p_id WHERE (d.p_id = 2 AND ((d.type = 'P')))
89
+
90
+ Analyzing P Load
91
+
92
+ select_type | key_len | type | Extra | id | possible_keys | rows | table | ref | key
93
+ ------------------------------------------------------------------------------------------------------------------------------
94
+ SIMPLE | 255 | ref | Using where | 1 | d_p_id_type_index,d_type_index | 1 | d | const | d_p_id_type_index
95
+ SIMPLE | 4 | eq_ref | Using where | 1 | PRIMARY | 1 | p | d.p_id | PRIMARY
96
+
97
+ Okay. Now MySQL is using an index satisfy the WHERE condition. Using the index, it was able to
98
+ find the single row that matched, preventing the full table scan.
99
+
100
+
101
+ Credits:
102
+ The extension of the Array class for printing the columnized records was originally
103
+ written by Peter Cooper who adapted it from Courtenay from #caboose.
104
+
105
+ http://www.rubyinside.com/columnized-text-datasets-in-rails-71.html
106
+ http://habtm.com/articles/2006/06/10/pretty-tables-for-ruby-objects
107
+
108
+ Released under the MIT license (download your own if you need it)
@@ -0,0 +1,53 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'rake'
11
+
12
+ require 'jeweler'
13
+ Jeweler::Tasks.new do |gem|
14
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
15
+ gem.name = "query-analyzer"
16
+ gem.homepage = "http://github.com/eecnee/query-analyzer"
17
+ gem.license = "MIT"
18
+ gem.summary = %q{Run explain on all the sql queries rails generates.}
19
+ gem.description = %q{Run explain on all the sql queries rails generates.}
20
+ gem.email = %q{todd@snappl.co.uk}
21
+ gem.authors = ["John Eberly and Todd Tyree"]
22
+ # Include your dependencies below. Runtime dependencies are required when using your gem,
23
+ # and development dependencies are only needed for development (ie running rake tasks, tests, etc)
24
+ # gem.add_runtime_dependency 'jabber4r', '> 0.1'
25
+ # gem.add_development_dependency 'rspec', '> 1.2.3'
26
+ end
27
+ Jeweler::RubygemsDotOrgTasks.new
28
+
29
+ require 'rake/testtask'
30
+ Rake::TestTask.new(:test) do |test|
31
+ test.libs << 'lib' << 'test'
32
+ test.pattern = 'test/**/test_*.rb'
33
+ test.verbose = true
34
+ end
35
+
36
+ require 'rcov/rcovtask'
37
+ Rcov::RcovTask.new do |test|
38
+ test.libs << 'test'
39
+ test.pattern = 'test/**/test_*.rb'
40
+ test.verbose = true
41
+ end
42
+
43
+ task :default => :test
44
+
45
+ require 'rake/rdoctask'
46
+ Rake::RDocTask.new do |rdoc|
47
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
48
+
49
+ rdoc.rdoc_dir = 'rdoc'
50
+ rdoc.title = "query-analyzer #{version}"
51
+ rdoc.rdoc_files.include('README*')
52
+ rdoc.rdoc_files.include('lib/**/*.rb')
53
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.1
@@ -0,0 +1,56 @@
1
+ class Array
2
+ protected
3
+ def qa_columnized_row(fields, sized)
4
+ row = []
5
+ fields.each_with_index do |f, i|
6
+ row << sprintf("%0-#{sized[i]}s", f.to_s)
7
+ end
8
+ row.join(' | ')
9
+ end
10
+
11
+ public
12
+
13
+ def qa_columnized
14
+ sized = {}
15
+ self.each do |row|
16
+ row.values.each_with_index do |value, i|
17
+ sized[i] = [sized[i].to_i, row.keys[i].length, value.to_s.length].max
18
+ end
19
+ end
20
+
21
+ table = []
22
+ table << qa_columnized_row(self.first.keys, sized)
23
+ table << '-' * table.first.length
24
+ self.each { |row| table << qa_columnized_row(row.values, sized) }
25
+ table.join("\n ") # Spaces added to work with format_log_entry
26
+ end
27
+ end
28
+
29
+
30
+ # TODO: Postgres? Oracle? Firebird?
31
+ module ActiveRecord
32
+ module ConnectionAdapters
33
+ class MysqlAdapter < AbstractAdapter
34
+ private
35
+ alias_method :select_without_analyzer, :select
36
+
37
+ def select(sql, name = nil)
38
+ query_results = select_without_analyzer(sql, name)
39
+
40
+ # I went back to showing all queries. This makes for a heavy log file, but you pick up
41
+ # things like filesorts (which the latest plugin wouldn't have caught).
42
+
43
+ if @logger and @logger.level == Logger::DEBUG
44
+ @logger.debug(
45
+ ActiveRecord::Base.silence do
46
+ format_log_entry("\033[1;34mAnalyzing #{name} \033[0m\n\n",
47
+ "\033[1;34m#{select_without_analyzer("explain #{sql}", name).qa_columnized} \033[0m\n\n"
48
+ )
49
+ end
50
+ ) if sql =~ /^select/i
51
+ end
52
+ query_results
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,64 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{query-analyzer}
8
+ s.version = "0.1.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["John Eberly and Todd Tyree"]
12
+ s.date = %q{2011-04-02}
13
+ s.description = %q{Run explain on all the sql queries rails generates.}
14
+ s.email = %q{todd@snappl.co.uk}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ "CHANGELOG.rdoc",
22
+ "Gemfile",
23
+ "Gemfile.lock",
24
+ "LICENSE.txt",
25
+ "README.rdoc",
26
+ "Rakefile",
27
+ "VERSION",
28
+ "lib/query_analyzer.rb",
29
+ "query-analyzer.gemspec",
30
+ "test/helper.rb",
31
+ "test/test_query-analyzer.rb"
32
+ ]
33
+ s.homepage = %q{http://github.com/eecnee/query-analyzer}
34
+ s.licenses = ["MIT"]
35
+ s.require_paths = ["lib"]
36
+ s.rubygems_version = %q{1.6.2}
37
+ s.summary = %q{Run explain on all the sql queries rails generates.}
38
+ s.test_files = [
39
+ "test/helper.rb",
40
+ "test/test_query-analyzer.rb"
41
+ ]
42
+
43
+ if s.respond_to? :specification_version then
44
+ s.specification_version = 3
45
+
46
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
47
+ s.add_development_dependency(%q<shoulda>, [">= 0"])
48
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
49
+ s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
50
+ s.add_development_dependency(%q<rcov>, [">= 0"])
51
+ else
52
+ s.add_dependency(%q<shoulda>, [">= 0"])
53
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
54
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
55
+ s.add_dependency(%q<rcov>, [">= 0"])
56
+ end
57
+ else
58
+ s.add_dependency(%q<shoulda>, [">= 0"])
59
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
60
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
61
+ s.add_dependency(%q<rcov>, [">= 0"])
62
+ end
63
+ end
64
+
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+ require 'shoulda'
12
+
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
15
+ require 'query-analyzer'
16
+
17
+ class Test::Unit::TestCase
18
+ end
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ class TestQueryAnalyzer < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,139 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: query-analyzer
3
+ version: !ruby/object:Gem::Version
4
+ hash: 25
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 1
10
+ version: 0.1.1
11
+ platform: ruby
12
+ authors:
13
+ - John Eberly and Todd Tyree
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-04-02 00:00:00 +02:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ type: :development
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 0
31
+ version: "0"
32
+ name: shoulda
33
+ version_requirements: *id001
34
+ prerelease: false
35
+ - !ruby/object:Gem::Dependency
36
+ type: :development
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ~>
41
+ - !ruby/object:Gem::Version
42
+ hash: 23
43
+ segments:
44
+ - 1
45
+ - 0
46
+ - 0
47
+ version: 1.0.0
48
+ name: bundler
49
+ version_requirements: *id002
50
+ prerelease: false
51
+ - !ruby/object:Gem::Dependency
52
+ type: :development
53
+ requirement: &id003 !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ~>
57
+ - !ruby/object:Gem::Version
58
+ hash: 7
59
+ segments:
60
+ - 1
61
+ - 5
62
+ - 2
63
+ version: 1.5.2
64
+ name: jeweler
65
+ version_requirements: *id003
66
+ prerelease: false
67
+ - !ruby/object:Gem::Dependency
68
+ type: :development
69
+ requirement: &id004 !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ hash: 3
75
+ segments:
76
+ - 0
77
+ version: "0"
78
+ name: rcov
79
+ version_requirements: *id004
80
+ prerelease: false
81
+ description: Run explain on all the sql queries rails generates.
82
+ email: todd@snappl.co.uk
83
+ executables: []
84
+
85
+ extensions: []
86
+
87
+ extra_rdoc_files:
88
+ - LICENSE.txt
89
+ - README.rdoc
90
+ files:
91
+ - .document
92
+ - CHANGELOG.rdoc
93
+ - Gemfile
94
+ - Gemfile.lock
95
+ - LICENSE.txt
96
+ - README.rdoc
97
+ - Rakefile
98
+ - VERSION
99
+ - lib/query_analyzer.rb
100
+ - query-analyzer.gemspec
101
+ - test/helper.rb
102
+ - test/test_query-analyzer.rb
103
+ has_rdoc: true
104
+ homepage: http://github.com/eecnee/query-analyzer
105
+ licenses:
106
+ - MIT
107
+ post_install_message:
108
+ rdoc_options: []
109
+
110
+ require_paths:
111
+ - lib
112
+ required_ruby_version: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ hash: 3
118
+ segments:
119
+ - 0
120
+ version: "0"
121
+ required_rubygems_version: !ruby/object:Gem::Requirement
122
+ none: false
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ hash: 3
127
+ segments:
128
+ - 0
129
+ version: "0"
130
+ requirements: []
131
+
132
+ rubyforge_project:
133
+ rubygems_version: 1.6.2
134
+ signing_key:
135
+ specification_version: 3
136
+ summary: Run explain on all the sql queries rails generates.
137
+ test_files:
138
+ - test/helper.rb
139
+ - test/test_query-analyzer.rb