mysql_double_index 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6b165cde37d643247ed3ed53b7b0841c5e7767d6
4
+ data.tar.gz: 8a78dc14909104337e90a19cb5239ef2c2472e92
5
+ SHA512:
6
+ metadata.gz: c1972d9dac9eccf58cf0c72a8da70fe734fa0e61ef80cca038344fc6fe62af19c0f080a5c1a9fc80ba38bae6cda72b60a9503c186db675f2702361a757beb3f9
7
+ data.tar.gz: 8087206e0a54bf05d506a71aac1af5e49307ba0ce1d327406d90bdc6a3305a4841f7c414b1a1d74f2f367a0eaed56e0acfc8d5c0c4b4a0899f32305d981f86b0
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://ruby.taobao.org'
2
+
3
+ # Specify your gem's dependencies in mysql_double_index.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 sunyafei
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
13
+ all 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
21
+ THE SOFTWARE.
@@ -0,0 +1,41 @@
1
+ # MysqlDoubleIndex
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/mysql_double_index`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'mysql_double_index'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install mysql_double_index
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/mysql_double_index. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "mysql_double_index"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,177 @@
1
+ require "mysql_double_index/version"
2
+ require "mysql_double_index/db_conn"
3
+ require 'active_record'
4
+ require 'rails'
5
+ require 'terminal-table'
6
+
7
+ module MysqlDoubleIndex
8
+
9
+ module_function
10
+ def test
11
+ end
12
+
13
+ def db_double_index
14
+ begin
15
+ MysqlDoubleIndex.db_connection #连接数据库
16
+ result = {}
17
+ double_index = []
18
+ redundancy_index = []
19
+ sql="show tables"
20
+ tables=ActiveRecord::Base.connection.execute(sql)
21
+ tables.each do |item|
22
+ tmp_hash = {}
23
+ keys = ActiveRecord::Base.connection.execute("show index from #{item[0]}")
24
+ keys.each do |item|
25
+ tmp_hash["#{item[2]}"] = {} if tmp_hash["#{item[2]}"].nil?
26
+ item[4] += "(#{item[7]})" if !item[7].nil?
27
+ if tmp_hash["#{item[2]}"]['columns'].nil?
28
+ tmp_hash["#{item[2]}"]['columns'] = [item[4]]
29
+ else
30
+ tmp_hash["#{item[2]}"]['columns'] << item[4]
31
+ end
32
+ tmp_hash["#{item[2]}"]['index_type'] = item[10]
33
+ end
34
+ result.merge!({item[0] => tmp_hash})
35
+ end
36
+ result.each do |table,indexs|
37
+ handled_index = []
38
+ indexs.each do |index_name_outer,index_columns_outer|
39
+ indexs.each do |index_name_inner,index_columns_inner|
40
+ next if index_name_inner == index_name_outer || index_columns_inner['index_type'] != index_columns_outer['index_type'] || handled_index.include?(index_name_inner.to_s + index_name_outer.to_s) || handled_index.include?(index_name_outer.to_s + index_name_inner.to_s)
41
+ #重复索引
42
+ if get_index_columns_sorted(index_columns_inner['columns']) == get_index_columns_sorted(index_columns_outer['columns'])
43
+ double_index << "#{table}上存在重复的索引:#{index_name_outer.to_s + get_index_columns_sorted(index_columns_outer['columns'],true)}&#{index_name_inner.to_s + get_index_columns_sorted(index_columns_inner['columns'],true)}"
44
+ elsif has_redundancy_index?(index_columns_inner['columns'],index_columns_outer['columns'])
45
+ #冗余索引
46
+ redundancy_index << "#{table}上存在冗余的索引:#{index_name_outer.to_s + get_index_columns_sorted(index_columns_outer['columns'],true)}&#{index_name_inner.to_s + get_index_columns_sorted(index_columns_inner['columns'],true)}"
47
+ end
48
+ handled_index << index_name_inner.to_s + index_name_outer.to_s
49
+ end
50
+ end
51
+ handled_index = []
52
+ end
53
+ puts "~"*50
54
+ print_arr = []
55
+ print_arr << ["重复索引"]
56
+ double_index.each do |item|
57
+ print_arr << [item]
58
+ end
59
+ table = Terminal::Table.new :rows => print_arr
60
+ puts table
61
+ print_arr = []
62
+ print_arr << ["冗余索引"]
63
+ redundancy_index.each do |item|
64
+ print_arr << [item]
65
+ end
66
+ table = Terminal::Table.new :rows => print_arr
67
+ puts table
68
+ rescue Exception => e
69
+ puts e.backtrace
70
+ ensure
71
+ db_close #释放链接
72
+ end
73
+ end
74
+
75
+ def db_table_size(table = nil)
76
+ begin
77
+ MysqlDoubleIndex.db_connection #连接数据库
78
+ print_arr = []
79
+ sql = "select database()"
80
+ database = ActiveRecord::Base.connection.execute(sql).first[0]
81
+ sql="use information_schema"
82
+ ActiveRecord::Base.connection.execute(sql)
83
+ if table.nil?
84
+ sql = "select TABLE_NAME,ENGINE,TABLE_ROWS,AVG_ROW_LENGTH,DATA_LENGTH,INDEX_LENGTH,(DATA_LENGTH+INDEX_LENGTH) as TABLE_LENGTH,CREATE_TIME from information_schema.tables where table_schema = '#{database}'"
85
+ else
86
+ sql = "select TABLE_NAME,ENGINE,TABLE_ROWS,AVG_ROW_LENGTH,DATA_LENGTH,INDEX_LENGTH,(DATA_LENGTH+INDEX_LENGTH) as TABLE_LENGTH,CREATE_TIME from information_schema.tables where table_schema = '#{database}' and table_name = '#{table}'"
87
+ end
88
+ tables = ActiveRecord::Base.connection.execute(sql)
89
+ head = ["TABLE_NAME","ENGINE","TABLE_ROWS","AVG_ROW_LENGTH","DATA_LENGTH","INDEX_LENGTH","SUM_LENGTH","CREATE_TIME"]
90
+ print_arr << head
91
+ tables.each do |item|
92
+ item.each_with_index do |data,index|
93
+ if [4,5,6].include?(index)
94
+ item[index] = byte2mb(data)
95
+ end
96
+ if 7 == index
97
+ item[index] = item[index].to_s[0...20]
98
+ end
99
+ end
100
+ print_arr << item
101
+ end
102
+ table = Terminal::Table.new :rows => print_arr
103
+ puts table
104
+ rescue Exception => e
105
+ ensure
106
+ sql="use #{database}"
107
+ ActiveRecord::Base.connection.execute(sql)
108
+ db_close #释放链接
109
+ end
110
+ end
111
+
112
+ #change byte to MB
113
+ def byte2mb(byte)
114
+ byte = byte.to_f
115
+ if byte < 1024
116
+ return "#{byte}Byte"
117
+ elsif byte/1024 < 1024
118
+ return "#{(byte/1024).round(2)}KB"
119
+ elsif byte/(1024**2) < 1024
120
+ return "#{(byte/(1024**2)).round(2)}MB"
121
+ elsif byte/(1024**3) < 1024
122
+ return "#{(byte/(1024**3)).round(2)}GB"
123
+ elsif byte/(1024**4) < 1024
124
+ return "#{(byte/(1024**4)).round(2)}TB"
125
+ end
126
+ end
127
+ def db_close
128
+ if ActiveRecord::Base.connection && ActiveRecord::Base.connection.active?
129
+ ActiveRecord::Base.connection.close
130
+ end
131
+ end
132
+ def get_index_columns_sorted(columns, sub_part = false)
133
+ if sub_part
134
+ '(' + columns.join(',') + ')'
135
+ else
136
+ '(' + columns.map do |item|
137
+ if item.include?('(')
138
+ item[0...item.index('(')]
139
+ else
140
+ item
141
+ end
142
+ end.join(',') + ')'
143
+ end
144
+ end
145
+ def get_index_columns_join(columns)
146
+ columns.map do |item|
147
+ if item.include?('(')
148
+ item[0...item.index('(')]
149
+ else
150
+ item
151
+ end
152
+ end.join(',')
153
+ end
154
+ def has_redundancy_index?(columns1, columns2)
155
+ columns1 = columns1.map do |item|
156
+ if item.include?('(')
157
+ item[0...item.index('(')]
158
+ else
159
+ item
160
+ end
161
+ end
162
+ columns2 = columns2.map do |item|
163
+ if item.include?('(')
164
+ item[0...item.index('(')]
165
+ else
166
+ item
167
+ end
168
+ end
169
+ if columns1.size > columns2.size
170
+ columns1,columns2 = columns2,columns1
171
+ end
172
+ columns1.each_with_index do |item,index|
173
+ return false if item != columns2[index]
174
+ end
175
+ return true
176
+ end
177
+ end
@@ -0,0 +1,3 @@
1
+ module MysqlDoubleIndex
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,37 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'mysql_double_index/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "mysql_double_index"
8
+ spec.version = MysqlDoubleIndex::VERSION
9
+ spec.authors = ["sunyafei"]
10
+ spec.email = ["1752709589@qq.com"]
11
+
12
+ spec.summary = %q{查找mysql数据库中重复和冗余索引}
13
+ spec.description = %q{查找mysql数据库中重复和冗余索引}
14
+ spec.homepage = "https://github.com/sun1752709589"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
18
+ # delete this section to allow pushing this gem to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
21
+ else
22
+ raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
23
+ end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_development_dependency "rspec", "~> 3.2"
31
+ spec.add_development_dependency "bundler", "~> 1.10"
32
+ spec.add_development_dependency "rake", "~> 10.0"
33
+ spec.add_development_dependency "activerecord", "~> 4.2"
34
+ spec.add_development_dependency "terminal-table", "~> 1.5"
35
+ spec.add_development_dependency "mysql2", "~> 0.3"
36
+ spec.add_development_dependency "rails", "~> 4.2"
37
+ end
metadata ADDED
@@ -0,0 +1,154 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mysql_double_index
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - sunyafei
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-08-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.2'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.10'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.10'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: activerecord
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '4.2'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '4.2'
69
+ - !ruby/object:Gem::Dependency
70
+ name: terminal-table
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.5'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.5'
83
+ - !ruby/object:Gem::Dependency
84
+ name: mysql2
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.3'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.3'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rails
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '4.2'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '4.2'
111
+ description: "查找mysql数据库中重复和冗余索引"
112
+ email:
113
+ - 1752709589@qq.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - CODE_OF_CONDUCT.md
120
+ - Gemfile
121
+ - LICENSE.txt
122
+ - README.md
123
+ - Rakefile
124
+ - bin/console
125
+ - bin/setup
126
+ - lib/mysql_double_index.rb
127
+ - lib/mysql_double_index/version.rb
128
+ - mysql_double_index.gemspec
129
+ homepage: https://github.com/sun1752709589
130
+ licenses:
131
+ - MIT
132
+ metadata:
133
+ allowed_push_host: https://rubygems.org
134
+ post_install_message:
135
+ rdoc_options: []
136
+ require_paths:
137
+ - lib
138
+ required_ruby_version: !ruby/object:Gem::Requirement
139
+ requirements:
140
+ - - ">="
141
+ - !ruby/object:Gem::Version
142
+ version: '0'
143
+ required_rubygems_version: !ruby/object:Gem::Requirement
144
+ requirements:
145
+ - - ">="
146
+ - !ruby/object:Gem::Version
147
+ version: '0'
148
+ requirements: []
149
+ rubyforge_project:
150
+ rubygems_version: 2.4.8
151
+ signing_key:
152
+ specification_version: 4
153
+ summary: "查找mysql数据库中重复和冗余索引"
154
+ test_files: []