suggest-db-indices 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.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +35 -0
- data/Rakefile +2 -0
- data/lib/suggest_db_indices/core.rb +174 -0
- data/lib/suggest_db_indices/version.rb +3 -0
- data/lib/suggest_db_indices.rb +5 -0
- data/suggest_db_indices.gemspec +20 -0
- metadata +68 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Isak Sky
|
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,35 @@
|
|
1
|
+
# Suggest-Db-Indices
|
2
|
+
|
3
|
+
A gem for your rails project that suggests indices for you to add in your database. Currently it suggests adding indexes to unindexed foreign keys.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'suggest-db-indices'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install suggest-db-indices
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
1. rails console
|
22
|
+
2. require 'suggest_db_indices'
|
23
|
+
3. SuggestDbIndices.go!
|
24
|
+
|
25
|
+
## Contributing
|
26
|
+
|
27
|
+
1. Fork it
|
28
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
29
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
30
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
31
|
+
5. Create new Pull Request
|
32
|
+
|
33
|
+
## Possible future features
|
34
|
+
1. I have built some things for analyzing the rails log files and looking at columns used in the queries that get run. I need to come up with a good way to use these results.
|
35
|
+
2. Next to each add index statement, there should be a justification.
|
data/Rakefile
ADDED
@@ -0,0 +1,174 @@
|
|
1
|
+
module SuggestDbIndices
|
2
|
+
class << self
|
3
|
+
def indexed_columns_by_table
|
4
|
+
@indexed_columns_by_table ||= connection.tables.reduce({}) do |h, table_name|
|
5
|
+
# Note: can index on multiple columns, which complicates things. Assuming user has done
|
6
|
+
# this correctly for now...
|
7
|
+
h.merge table_name => connection.indexes(table_name).map {|index| index.columns}.flatten
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def non_pk_column_names table_name
|
12
|
+
connection.columns(table_name).reject do |column|
|
13
|
+
column.name == primary_key_name(connection, table_name)
|
14
|
+
end.map(&:name)
|
15
|
+
end
|
16
|
+
|
17
|
+
def non_pk_columns_by_table
|
18
|
+
@non_pk_columns_by_table ||= connection.tables.reduce({}) do |h, table_name|
|
19
|
+
h.merge! table_name => non_pk_column_names(table_name)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
# Stole this from activerecord schema dumper code
|
24
|
+
def primary_key_name connection, table_name
|
25
|
+
if connection.respond_to?(:pk_and_sequence_for)
|
26
|
+
connection.pk_and_sequence_for(table_name).first rescue nil
|
27
|
+
elsif connection.respond_to?(:primary_key)
|
28
|
+
connection.primary_key(table_name)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def foreign_key? column_name
|
33
|
+
column_name.end_with? "_id"
|
34
|
+
end
|
35
|
+
|
36
|
+
NUM_LINES_TO_READ = 1000
|
37
|
+
|
38
|
+
def connection
|
39
|
+
ActiveRecord::Base.connection
|
40
|
+
end
|
41
|
+
|
42
|
+
def unindexed_columns_by_table
|
43
|
+
non_pk_columns_by_table.reduce({}) do |h, (table, columns)|
|
44
|
+
h.merge table => columns - (indexed_columns_by_table[table] || [])
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def unindexed_foreign_key_columns_by_table
|
49
|
+
unindexed_columns_by_table.reduce({}) do |h, (table, columns)|
|
50
|
+
h.merge table => columns.select {|col| foreign_key?(col) }
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def go! config = {}
|
55
|
+
@config = default_options.reduce(config) do |h, (k,v)|
|
56
|
+
if h[k]
|
57
|
+
h
|
58
|
+
else
|
59
|
+
h.merge! k => v
|
60
|
+
end
|
61
|
+
end
|
62
|
+
generate_migration_file! format_index_migration_string unindexed_foreign_key_columns_by_table
|
63
|
+
end
|
64
|
+
|
65
|
+
def format_index_migration_string columns_by_table
|
66
|
+
add_index_statements = columns_by_table.reduce('') do |s, (table, columns)|
|
67
|
+
columns.each {|col| s += " add_index :#{table}, :#{col}\n" }
|
68
|
+
s
|
69
|
+
end
|
70
|
+
" def change\n#{add_index_statements}\n end\nend"
|
71
|
+
end
|
72
|
+
|
73
|
+
def generate_migration_file! migration_contents
|
74
|
+
_ , migration_file_path = Rails::Generators.invoke("active_record:migration",
|
75
|
+
["add_indexes_via_suggest_db_indices_#{rand(36**8).to_s(36)}",
|
76
|
+
'BoiledGoose:Animal'])
|
77
|
+
file_contents = File.read migration_file_path
|
78
|
+
search_string = "ActiveRecord::Migration"
|
79
|
+
stop_index = (file_contents.index(search_string)) + search_string.length
|
80
|
+
new_file_contents = file_contents[0..stop_index] + migration_contents
|
81
|
+
File.open(migration_file_path, 'w') {|f| f.write(new_file_contents) }
|
82
|
+
migration_file_path
|
83
|
+
end
|
84
|
+
|
85
|
+
def default_options
|
86
|
+
{:num_lines_to_scan => 10000,
|
87
|
+
:examine_logs => false,
|
88
|
+
:log_dir => ""}
|
89
|
+
end
|
90
|
+
|
91
|
+
def prepare_log_file! log_dir
|
92
|
+
puts "Preparing log files..."
|
93
|
+
tmpfile = Tempfile.new('tmplog')
|
94
|
+
log_file_names = Dir.glob File.join log_dir, '*.log'
|
95
|
+
puts "Found log files: #{log_file_names.inspect}"
|
96
|
+
|
97
|
+
puts "Tailing each file!"
|
98
|
+
log_file_names.each {|f| sh_dbg "tail -n #{NUM_LINES_TO_READ} #{f} >> #{tmpfile.path}" }
|
99
|
+
puts "Stripping color codes!"
|
100
|
+
stripped_log_file = Tempfile.new('stripped')
|
101
|
+
# Because text search is too tricky with colors
|
102
|
+
strip_color_codes! tmpfile.path, stripped_log_file.path
|
103
|
+
stripped_log_file
|
104
|
+
end
|
105
|
+
|
106
|
+
def scan_log_files_for_queried_columns log_dir, non_pk_columns_by_table = non_pk_columns_by_table
|
107
|
+
stripped_log_file = prepare_log_file! log_dir
|
108
|
+
|
109
|
+
queried_columns_by_table = hash_of_arrays
|
110
|
+
# For debugging: Record from which SQL statement we got each column
|
111
|
+
inferred_table_columns_by_raw_where_clause = hash_of_sets
|
112
|
+
|
113
|
+
while line = stripped_log_file.gets
|
114
|
+
line = remove_limit_clause(line.strip)
|
115
|
+
if matches = /SELECT.+FROM\s\W?(\w+)\W?\sWHERE(.+)/i.match(line)
|
116
|
+
table = matches[1]
|
117
|
+
|
118
|
+
raw_where_clause = matches[2]
|
119
|
+
# puts "Where: #{raw_where_clause}"
|
120
|
+
|
121
|
+
raw_where_clause.split.map do |s|
|
122
|
+
s.gsub('`','')
|
123
|
+
end.reduce([]) do |memo, identifier| #TODO: Stop reducing to array, reduce to counter
|
124
|
+
if identifier.include?('.')
|
125
|
+
current_table, column_candidate = identifier.split('.')
|
126
|
+
else
|
127
|
+
current_table, column_candidate = [table, identifier]
|
128
|
+
end
|
129
|
+
|
130
|
+
if non_pk_columns_by_table[current_table].include? column_candidate
|
131
|
+
# We only care about the identifiers that match up to a table and column.
|
132
|
+
# This is a ghetto way to to avoid having to parse SQL (extremely difficult)
|
133
|
+
memo << [current_table, column_candidate]
|
134
|
+
else
|
135
|
+
memo
|
136
|
+
end
|
137
|
+
end.each do |(table, column)|
|
138
|
+
queried_columns_by_table[table] << column
|
139
|
+
inferred_table_columns_by_raw_where_clause[raw_where_clause] << [table, column]
|
140
|
+
end
|
141
|
+
end
|
142
|
+
end
|
143
|
+
{:queried_columns_by_table => queried_columns_by_table,
|
144
|
+
:inferred_table_columns_by_raw_where_clause => inferred_table_columns_by_raw_where_clause}
|
145
|
+
end
|
146
|
+
|
147
|
+
def hash_of_arrays
|
148
|
+
Hash.new {|h, k| h[k] = [] }
|
149
|
+
end
|
150
|
+
|
151
|
+
def hash_of_sets
|
152
|
+
Hash.new {|h, k| h[k] = Set.new }
|
153
|
+
end
|
154
|
+
|
155
|
+
def remove_limit_clause s
|
156
|
+
if matches = /(.+)\sLIMIT/.match(s)
|
157
|
+
return matches[1]
|
158
|
+
else
|
159
|
+
return s
|
160
|
+
end
|
161
|
+
end
|
162
|
+
|
163
|
+
def strip_color_codes! file_name, output_path
|
164
|
+
# From: http://serverfault.com/a/154200
|
165
|
+
sh_dbg 'sed "s/${esc}[^m]*m//g" ' + "#{file_name} >> #{output_path}"
|
166
|
+
raise "There was a problem stripping colors" unless $?.success?
|
167
|
+
end
|
168
|
+
|
169
|
+
def sh_dbg cmd
|
170
|
+
puts "Shelling: #{cmd}"
|
171
|
+
`#{cmd}`
|
172
|
+
end
|
173
|
+
end
|
174
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.join File.dirname(__FILE__), 'lib/suggest_db_indices/version'
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Isak Sky"]
|
6
|
+
gem.email = ["isak.sky@gmail.com"]
|
7
|
+
gem.description = %q{A gem for your rails project that suggests indices for you to add in your database. Currently it suggests adding indexes to unindexed foreign keys.}
|
8
|
+
gem.summary = %q{A gem for your rails project that suggests indices for you to add in your database.}
|
9
|
+
gem.homepage = "https://github.com/isaksky/suggest-db-indices"
|
10
|
+
gem.license = "MIT"
|
11
|
+
|
12
|
+
gem.files = `git ls-files`.split($\)
|
13
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
14
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
15
|
+
gem.name = "suggest-db-indices"
|
16
|
+
gem.require_paths = ["lib"]
|
17
|
+
gem.version = SuggestDbIndices::VERSION
|
18
|
+
|
19
|
+
gem.add_dependency('rails', '>= 3.0.0')
|
20
|
+
end
|
metadata
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: suggest-db-indices
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Isak Sky
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-06-11 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rails
|
16
|
+
requirement: &2156238200 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 3.0.0
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *2156238200
|
25
|
+
description: A gem for your rails project that suggests indices for you to add in
|
26
|
+
your database. Currently it suggests adding indexes to unindexed foreign keys.
|
27
|
+
email:
|
28
|
+
- isak.sky@gmail.com
|
29
|
+
executables: []
|
30
|
+
extensions: []
|
31
|
+
extra_rdoc_files: []
|
32
|
+
files:
|
33
|
+
- .gitignore
|
34
|
+
- Gemfile
|
35
|
+
- LICENSE
|
36
|
+
- README.md
|
37
|
+
- Rakefile
|
38
|
+
- lib/suggest_db_indices.rb
|
39
|
+
- lib/suggest_db_indices/core.rb
|
40
|
+
- lib/suggest_db_indices/version.rb
|
41
|
+
- suggest_db_indices.gemspec
|
42
|
+
homepage: https://github.com/isaksky/suggest-db-indices
|
43
|
+
licenses:
|
44
|
+
- MIT
|
45
|
+
post_install_message:
|
46
|
+
rdoc_options: []
|
47
|
+
require_paths:
|
48
|
+
- lib
|
49
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
56
|
+
none: false
|
57
|
+
requirements:
|
58
|
+
- - ! '>='
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0'
|
61
|
+
requirements: []
|
62
|
+
rubyforge_project:
|
63
|
+
rubygems_version: 1.8.11
|
64
|
+
signing_key:
|
65
|
+
specification_version: 3
|
66
|
+
summary: A gem for your rails project that suggests indices for you to add in your
|
67
|
+
database.
|
68
|
+
test_files: []
|