tatyree-query-analyzer 0.1.0

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.
data/README.rdoc ADDED
@@ -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
+ *NOTE THE UNDERSCORE!* +require 'query-analyzer'+ 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)
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'echoe'
4
+
5
+ Echoe.new('query-analyzer', '0.1.0') do |p|
6
+ p.description = "Run explain on all the sql queries rails generates."
7
+ p.url = "http://github.com/tatyree/cookieless_sessions"
8
+ p.author = "John Eberly and Todd Tyree"
9
+ p.email = "todd@snappl.co.uk"
10
+ p.ignore_pattern = ["tmp/*", "script/*"]
11
+ p.development_dependencies = []
12
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'query_analyzer'
@@ -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,31 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{query-analyzer}
5
+ s.version = "0.1.0"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["John Eberly and Todd Tyree"]
9
+ s.date = %q{2009-02-07}
10
+ s.description = %q{Run explain on all the sql queries rails generates.}
11
+ s.email = %q{todd@snappl.co.uk}
12
+ s.extra_rdoc_files = ["lib/query_analyzer.rb", "README.rdoc"]
13
+ s.files = ["init.rb", "lib/query_analyzer.rb", "Rakefile", "README.rdoc", "Manifest", "query-analyzer.gemspec"]
14
+ s.has_rdoc = true
15
+ s.homepage = %q{http://github.com/tatyree/cookieless_sessions}
16
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Query-analyzer", "--main", "README.rdoc"]
17
+ s.require_paths = ["lib"]
18
+ s.rubyforge_project = %q{query-analyzer}
19
+ s.rubygems_version = %q{1.3.1}
20
+ s.summary = %q{Run explain on all the sql queries rails generates.}
21
+
22
+ if s.respond_to? :specification_version then
23
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
24
+ s.specification_version = 2
25
+
26
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
27
+ else
28
+ end
29
+ else
30
+ end
31
+ end
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tatyree-query-analyzer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - John Eberly and Todd Tyree
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-02-07 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Run explain on all the sql queries rails generates.
17
+ email: todd@snappl.co.uk
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - lib/query_analyzer.rb
24
+ - README.rdoc
25
+ files:
26
+ - init.rb
27
+ - lib/query_analyzer.rb
28
+ - Rakefile
29
+ - README.rdoc
30
+ - Manifest
31
+ - query-analyzer.gemspec
32
+ has_rdoc: true
33
+ homepage: http://github.com/tatyree/cookieless_sessions
34
+ post_install_message:
35
+ rdoc_options:
36
+ - --line-numbers
37
+ - --inline-source
38
+ - --title
39
+ - Query-analyzer
40
+ - --main
41
+ - README.rdoc
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: "0"
49
+ version:
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: "1.2"
55
+ version:
56
+ requirements: []
57
+
58
+ rubyforge_project: query-analyzer
59
+ rubygems_version: 1.2.0
60
+ signing_key:
61
+ specification_version: 2
62
+ summary: Run explain on all the sql queries rails generates.
63
+ test_files: []
64
+