Module: IndexShotgun::Analyzer
- Defined in:
- lib/index_shotgun/analyzer.rb
Class Method Summary collapse
-
.check_indexes(table) ⇒ Array<Hash>
check duplicate indexes of table.
-
.perform ⇒ String
Search duplicate index.
-
.table_indexes(table) ⇒ Object
get indexes of table.
Class Method Details
.check_indexes(table) ⇒ Array<Hash>
check duplicate indexes of table
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/index_shotgun/analyzer.rb', line 53 def check_indexes(table) indexes = table_indexes(table) indexes.permutation(2).each_with_object([]) do |(source_index, target_index), response| next unless source_index.columns.start_with?(target_index.columns) if target_index.unique response << { index: source_index, result: "#{source_index.name} has column(s) on the right side of unique index (#{target_index.name}). You can drop if low cardinality", } else response << { index: target_index, result: "#{target_index.name} is a left-prefix of #{source_index.name}", } end end end |
.perform ⇒ String
Search duplicate index
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/index_shotgun/analyzer.rb', line 10 def perform tables = ActiveRecord::Base.connection.tables duplicate_indexes = tables.each_with_object([]) do |table, array| response = check_indexes(table) array.push(*response) end = duplicate_indexes.each_with_object("") do |info, | << <<-EOS.strip_heredoc # ============================= # #{info[:index].table} # ============================= # #{info[:result]} # To remove this duplicate index, execute: ALTER TABLE `#{info[:index].table}` DROP INDEX `#{info[:index].name}`; EOS end total_index_count = tables.map { |table| table_indexes(table).count }.sum << <<-EOS.strip_heredoc # ######################################################################## # Summary of indexes # ######################################################################## # Total Duplicate Indexes #{duplicate_indexes.count} # Total Indexes #{total_index_count} # Total Tables #{tables.count} EOS end |
.table_indexes(table) ⇒ Object
get indexes of table
76 77 78 |
# File 'lib/index_shotgun/analyzer.rb', line 76 def table_indexes(table) ActiveRecord::Base.connection.indexes(table) end |