mysql_double_index 0.1.3 → 0.1.4
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.
- checksums.yaml +4 -4
- data/README.md +27 -2
- data/lib/mysql_double_index.rb +18 -16
- data/lib/mysql_double_index/db_conn.rb +5 -0
- data/lib/mysql_double_index/version.rb +1 -1
- data/mysql_double_index.gemspec +8 -8
- metadata +23 -23
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9c60eef301a5028df207317f97dade06a0482f1e
|
4
|
+
data.tar.gz: 3c97291b4d56509ce543f5733db3a5a9ce4fa999
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: aba091952ba99b6951de44c2b67eaa160959df9eb082b728f77e5e67bfa14ff74103d47bb4ebd0927ea9c5a49a4d1a9b337b16f0007ec7189dc8c1640e90e6d9
|
7
|
+
data.tar.gz: 3e553951379c738a64861f1dcd0099add122c523cf6ab5044da5566ee7cc9ed6059b309cd4848c4bfe14b45febefada9eb96ef92031e023a803e8af63315447d
|
data/README.md
CHANGED
@@ -4,11 +4,23 @@ Welcome to your new gem! In this directory, you'll find the files you need to be
|
|
4
4
|
|
5
5
|
TODO: Delete this and the text above, and describe your gem
|
6
6
|
|
7
|
+
## Doc
|
8
|
+
冗余和重复索引的概念:
|
9
|
+
|
10
|
+
MySQL允许在相同列上创建多个索引,无论是有意的还是无意的。MySQL需要单独维护重复的索引,并且优化器在优化查询的时候也需要逐个地进行考虑,这会影响性能。
|
11
|
+
|
12
|
+
重复索引:是指在相同的列上按照相同的顺序创建的相同类型的索引。应该避免这样创建重复索引,发现后也应该立即移除。
|
13
|
+
|
14
|
+
冗余索引:冗余索引和重复索引有一些不同,如果创建了索引(A,B),再创建索引(A)就是冗余索引,因为这只是前一个索引的前缀索引。因此索引(A,B)也可以当索引(A)来使用(这种冗余只是对B-Tree索引来说)。冗余索引通常发生在为表添加新索引的时候。例如,有人可能会增加一个新的索引(A,B)而不是扩展已有的索引(A)。还有一种情况是将一个索引扩展为(A,ID),其中ID是主键,对于InnoDB来说主键列已经包含在二级索引中了,索引也是冗余的。
|
15
|
+
|
16
|
+
大多数的情况下都不需要冗余索引,应该尽量扩展已有的索引而不是创建新索引。但也有时候出于性能方面的考虑需要冗余索引,因为扩展已有的索引会导致其变得太大,从而影响其它使用该索引的查询的性能。
|
17
|
+
|
7
18
|
## Installation
|
8
19
|
|
9
20
|
Add this line to your application's Gemfile:
|
10
21
|
|
11
22
|
```ruby
|
23
|
+
|
12
24
|
gem 'mysql_double_index'
|
13
25
|
```
|
14
26
|
|
@@ -22,7 +34,21 @@ Or install it yourself as:
|
|
22
34
|
|
23
35
|
## Usage
|
24
36
|
|
25
|
-
|
37
|
+
And then execute:
|
38
|
+
|
39
|
+
$ rails c
|
40
|
+
|
41
|
+
#查询单个表重复&冗余索引
|
42
|
+
$ > MysqlDoubleIndex.db_double_index("articles")
|
43
|
+
|
44
|
+
#查询所有表重复&冗余索引
|
45
|
+
$ > MysqlDoubleIndex.db_double_index
|
46
|
+
|
47
|
+
#查询单个表磁盘占用大小
|
48
|
+
$ > MysqlDoubleIndex.db_table_size("articles")
|
49
|
+
|
50
|
+
#查询所有表磁盘占用大小
|
51
|
+
$ > MysqlDoubleIndex.db_table_size
|
26
52
|
|
27
53
|
## Development
|
28
54
|
|
@@ -38,4 +64,3 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERN
|
|
38
64
|
## License
|
39
65
|
|
40
66
|
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
41
|
-
|
data/lib/mysql_double_index.rb
CHANGED
@@ -45,10 +45,10 @@ module MysqlDoubleIndex
|
|
45
45
|
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)
|
46
46
|
#重复索引
|
47
47
|
if get_index_columns_sorted(index_columns_inner['columns']) == get_index_columns_sorted(index_columns_outer['columns'])
|
48
|
-
double_index << "#{table}
|
48
|
+
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)}】"
|
49
49
|
elsif has_redundancy_index?(index_columns_inner['columns'],index_columns_outer['columns'])
|
50
50
|
#冗余索引
|
51
|
-
redundancy_index << "#{table}
|
51
|
+
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)}】"
|
52
52
|
end
|
53
53
|
handled_index << index_name_inner.to_s + index_name_outer.to_s
|
54
54
|
end
|
@@ -56,23 +56,30 @@ module MysqlDoubleIndex
|
|
56
56
|
handled_index = []
|
57
57
|
end
|
58
58
|
print_arr = []
|
59
|
-
print_arr << ["重复索引"]
|
60
59
|
double_index.each do |item|
|
61
60
|
print_arr << [item]
|
62
61
|
end
|
63
|
-
table = Terminal::Table.new :rows => print_arr
|
62
|
+
table = Terminal::Table.new :title => "重复索引", :rows => print_arr
|
64
63
|
puts table if double_index.size > 0
|
64
|
+
if double_index.size < 1
|
65
|
+
table = Terminal::Table.new :title => "暂无检索到重复索引"
|
66
|
+
puts table
|
67
|
+
end
|
68
|
+
|
65
69
|
print_arr = []
|
66
|
-
print_arr << ["冗余索引"]
|
67
70
|
redundancy_index.each do |item|
|
68
71
|
print_arr << [item]
|
69
72
|
end
|
70
|
-
table = Terminal::Table.new :rows => print_arr
|
73
|
+
table = Terminal::Table.new :title => "冗余索引", :rows => print_arr
|
71
74
|
puts table if redundancy_index.size > 0
|
75
|
+
if redundancy_index.size < 1
|
76
|
+
table = Terminal::Table.new :title => "暂无检索到冗余索引"
|
77
|
+
puts table
|
78
|
+
end
|
72
79
|
rescue Exception => e
|
73
80
|
puts e.backtrace
|
74
81
|
ensure
|
75
|
-
db_close #释放链接
|
82
|
+
MysqlDoubleIndex.db_close #释放链接
|
76
83
|
end
|
77
84
|
end
|
78
85
|
|
@@ -90,8 +97,7 @@ module MysqlDoubleIndex
|
|
90
97
|
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}'"
|
91
98
|
end
|
92
99
|
tables = ActiveRecord::Base.connection.execute(sql)
|
93
|
-
head = ["
|
94
|
-
print_arr << head
|
100
|
+
head = ["table name","engine","table rows","average row length","data length","index length","sum lemgth","create time"]
|
95
101
|
tables.each do |item|
|
96
102
|
item.each_with_index do |data,index|
|
97
103
|
if [4,5,6].include?(index)
|
@@ -103,13 +109,13 @@ module MysqlDoubleIndex
|
|
103
109
|
end
|
104
110
|
print_arr << item
|
105
111
|
end
|
106
|
-
table = Terminal::Table.new :rows => print_arr
|
112
|
+
table = Terminal::Table.new :title => "表占用磁盘详情", :headings => head, :rows => print_arr
|
107
113
|
puts table
|
108
114
|
rescue Exception => e
|
109
115
|
ensure
|
110
116
|
sql="use #{database}"
|
111
117
|
ActiveRecord::Base.connection.execute(sql)
|
112
|
-
db_close #释放链接
|
118
|
+
MysqlDoubleIndex.db_close #释放链接
|
113
119
|
end
|
114
120
|
end
|
115
121
|
|
@@ -128,11 +134,7 @@ module MysqlDoubleIndex
|
|
128
134
|
return "#{(byte/(1024**4)).round(2)}TB"
|
129
135
|
end
|
130
136
|
end
|
131
|
-
|
132
|
-
if ActiveRecord::Base.connection && ActiveRecord::Base.connection.active?
|
133
|
-
ActiveRecord::Base.connection.close
|
134
|
-
end
|
135
|
-
end
|
137
|
+
|
136
138
|
def get_index_columns_sorted(columns, sub_part = false)
|
137
139
|
if sub_part
|
138
140
|
'(' + columns.join(',') + ')'
|
@@ -9,4 +9,9 @@ module MysqlDoubleIndex
|
|
9
9
|
dbconfig = YAML::load(MysqlDoubleIndex.load_config)
|
10
10
|
ActiveRecord::Base.establish_connection(dbconfig[Rails::env.to_s])
|
11
11
|
end
|
12
|
+
def db_close
|
13
|
+
if ActiveRecord::Base.connection && ActiveRecord::Base.connection.active?
|
14
|
+
ActiveRecord::Base.connection.close
|
15
|
+
end
|
16
|
+
end
|
12
17
|
end
|
data/mysql_double_index.gemspec
CHANGED
@@ -11,7 +11,7 @@ Gem::Specification.new do |spec|
|
|
11
11
|
|
12
12
|
spec.summary = %q{查找mysql数据库中重复和冗余索引}
|
13
13
|
spec.description = %q{查找mysql数据库中重复和冗余索引}
|
14
|
-
spec.homepage = "https://github.com/sun1752709589"
|
14
|
+
spec.homepage = "https://github.com/sun1752709589/mysql_double_index"
|
15
15
|
spec.license = "MIT"
|
16
16
|
|
17
17
|
# Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
|
@@ -27,11 +27,11 @@ Gem::Specification.new do |spec|
|
|
27
27
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
28
28
|
spec.require_paths = ["lib","lib/mysql_double_index"]
|
29
29
|
|
30
|
-
spec.
|
31
|
-
spec.
|
32
|
-
spec.
|
33
|
-
spec.
|
34
|
-
spec.
|
35
|
-
spec.
|
36
|
-
spec.
|
30
|
+
spec.add_dependency "rspec", "~> 3.2"
|
31
|
+
spec.add_dependency "bundler", "~> 1.10"
|
32
|
+
spec.add_dependency "rake", "~> 10.0"
|
33
|
+
spec.add_dependency "activerecord"
|
34
|
+
spec.add_dependency "terminal-table"
|
35
|
+
spec.add_dependency "mysql2", "~> 0.3"
|
36
|
+
spec.add_dependency "rails"
|
37
37
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mysql_double_index
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- sunyafei
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-08-
|
11
|
+
date: 2015-08-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|
@@ -17,7 +17,7 @@ dependencies:
|
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '3.2'
|
20
|
-
type: :
|
20
|
+
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
@@ -31,7 +31,7 @@ dependencies:
|
|
31
31
|
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '1.10'
|
34
|
-
type: :
|
34
|
+
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
@@ -45,7 +45,7 @@ dependencies:
|
|
45
45
|
- - "~>"
|
46
46
|
- !ruby/object:Gem::Version
|
47
47
|
version: '10.0'
|
48
|
-
type: :
|
48
|
+
type: :runtime
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
@@ -56,30 +56,30 @@ dependencies:
|
|
56
56
|
name: activerecord
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
|
-
- - "
|
59
|
+
- - ">="
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version: '
|
62
|
-
type: :
|
61
|
+
version: '0'
|
62
|
+
type: :runtime
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
|
-
- - "
|
66
|
+
- - ">="
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version: '
|
68
|
+
version: '0'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: terminal-table
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
|
-
- - "
|
73
|
+
- - ">="
|
74
74
|
- !ruby/object:Gem::Version
|
75
|
-
version: '
|
76
|
-
type: :
|
75
|
+
version: '0'
|
76
|
+
type: :runtime
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
|
-
- - "
|
80
|
+
- - ">="
|
81
81
|
- !ruby/object:Gem::Version
|
82
|
-
version: '
|
82
|
+
version: '0'
|
83
83
|
- !ruby/object:Gem::Dependency
|
84
84
|
name: mysql2
|
85
85
|
requirement: !ruby/object:Gem::Requirement
|
@@ -87,7 +87,7 @@ dependencies:
|
|
87
87
|
- - "~>"
|
88
88
|
- !ruby/object:Gem::Version
|
89
89
|
version: '0.3'
|
90
|
-
type: :
|
90
|
+
type: :runtime
|
91
91
|
prerelease: false
|
92
92
|
version_requirements: !ruby/object:Gem::Requirement
|
93
93
|
requirements:
|
@@ -98,16 +98,16 @@ dependencies:
|
|
98
98
|
name: rails
|
99
99
|
requirement: !ruby/object:Gem::Requirement
|
100
100
|
requirements:
|
101
|
-
- - "
|
101
|
+
- - ">="
|
102
102
|
- !ruby/object:Gem::Version
|
103
|
-
version: '
|
104
|
-
type: :
|
103
|
+
version: '0'
|
104
|
+
type: :runtime
|
105
105
|
prerelease: false
|
106
106
|
version_requirements: !ruby/object:Gem::Requirement
|
107
107
|
requirements:
|
108
|
-
- - "
|
108
|
+
- - ">="
|
109
109
|
- !ruby/object:Gem::Version
|
110
|
-
version: '
|
110
|
+
version: '0'
|
111
111
|
description: "查找mysql数据库中重复和冗余索引"
|
112
112
|
email:
|
113
113
|
- 1752709589@qq.com
|
@@ -127,7 +127,7 @@ files:
|
|
127
127
|
- lib/mysql_double_index/db_conn.rb
|
128
128
|
- lib/mysql_double_index/version.rb
|
129
129
|
- mysql_double_index.gemspec
|
130
|
-
homepage: https://github.com/sun1752709589
|
130
|
+
homepage: https://github.com/sun1752709589/mysql_double_index
|
131
131
|
licenses:
|
132
132
|
- MIT
|
133
133
|
metadata:
|
@@ -149,7 +149,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
149
149
|
version: '0'
|
150
150
|
requirements: []
|
151
151
|
rubyforge_project:
|
152
|
-
rubygems_version: 2.
|
152
|
+
rubygems_version: 2.2.2
|
153
153
|
signing_key:
|
154
154
|
specification_version: 4
|
155
155
|
summary: "查找mysql数据库中重复和冗余索引"
|