mysql-warmup 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 528bec2ec40aebd23618e7b565ad1ebc56fecc2e
4
- data.tar.gz: 826ada4963a4bbaeb26915ffe68ece199f994650
3
+ metadata.gz: 260186eb7857d1532a09cb2deb0cadb6affe54f2
4
+ data.tar.gz: 078a46dd9fab6acfa5b05caf7f315653bf3b6163
5
5
  SHA512:
6
- metadata.gz: b372e3fe62daddca167677763348853b0f7067188056fa076e16b3db0caa6e7688cb8dd1552521658fb41d8e965d877005d6466c365a9f83bac8840e97a6587b
7
- data.tar.gz: c5ef0652894e93a7542f0fd349b81aae5f6511ae1a53686a0ea35265f0c08e7e36cdd5d7b6c30ffc4ba6d974e0f7e70a34b89c0f9cffb04fe65f720fb0a99c7a
6
+ metadata.gz: 71e52cf2de14ee80d2479cd1fda6f61a346ae7dfd296d5c3bfdf82c7f5f98681aa7981f7b42c8b2a8ea9cd78fa90680ef4b79cdad3025f8740dbc9e5541314a4
7
+ data.tar.gz: f9cd930ebaa0c9a3dee6d4a96c8bd69687769a80fd933781c4c1e4df4555e222ad64c6f3d66f0d241351163d865dd0086e1c9d04921abcbe1c7257bf2fbc2181
data/CHANGELOG.md CHANGED
@@ -1,4 +1,9 @@
1
1
  # Changelog
2
+ ### v.0.0.3 (2017/01/15)
3
+ * Add default value 'localhost' to -h param
4
+ * Improve touching primary by `count(*) where non_index_file = 0` instead of `sum(id)` <br/>
5
+ [Reason](./CHANGE_SUM_TO_COUNT.md)
6
+
2
7
 
3
8
  ### v.0.0.2 (2017/01/14)
4
9
  * Change check type_primary? condition to regardless to field type
@@ -0,0 +1,262 @@
1
+ # Background
2
+ When execute command to load primary key to buffer pool, what command should we do?
3
+ `select count(*) from table_name` or `select count(*) from table_name where non_index_column = 0` or `select sum(primary_key) from table_name` ?
4
+
5
+ # Testing
6
+ ### Testing environment
7
+ * Mysql version `5.7.17`
8
+ * Buffer pool configuration:
9
+ ```
10
+ mysql> show variables like '%buffer_pool%';
11
+ +-------------------------------------+----------------+
12
+ | Variable_name | Value |
13
+ +-------------------------------------+----------------+
14
+ | innodb_buffer_pool_chunk_size | 134217728 |
15
+ | innodb_buffer_pool_dump_at_shutdown | OFF |
16
+ | innodb_buffer_pool_dump_now | OFF |
17
+ | innodb_buffer_pool_dump_pct | 25 |
18
+ | innodb_buffer_pool_filename | ib_buffer_pool |
19
+ | innodb_buffer_pool_instances | 1 |
20
+ | innodb_buffer_pool_load_abort | OFF |
21
+ | innodb_buffer_pool_load_at_startup | OFF |
22
+ | innodb_buffer_pool_load_now | OFF |
23
+ | innodb_buffer_pool_size | 134217728 |
24
+ +-------------------------------------+----------------+
25
+ 10 rows in set (0.01 sec)
26
+ ```
27
+
28
+ * Database `test_mysql_dump` has only table `test_data` with 10M rows, configurations as below:
29
+
30
+ ```
31
+ mysql> select table_name, engine from information_schema.tables where table_schema = 'test_mysql_warmup';
32
+ +------------+--------+
33
+ | table_name | engine |
34
+ +------------+--------+
35
+ | test_data | InnoDB |
36
+ +------------+--------+
37
+ 1 row in set (0.00 sec)
38
+ ```
39
+
40
+ and
41
+ ```
42
+ mysql> desc test_data;
43
+ +-------------+--------------+------+-----+---------+-------+
44
+ | Field | Type | Null | Key | Default | Extra |
45
+ +-------------+--------------+------+-----+---------+-------+
46
+ | id | int(11) | NO | PRI | NULL | |
47
+ | random_str | varchar(255) | YES | | NULL | |
48
+ | created_at | varchar(255) | YES | | NULL | |
49
+ | random_str1 | varchar(255) | YES | | NULL | |
50
+ | random_str2 | varchar(255) | YES | | NULL | |
51
+ | random_str3 | varchar(255) | YES | | NULL | |
52
+ +-------------+--------------+------+-----+---------+-------+
53
+ 6 rows in set (0.00 sec)
54
+ ```
55
+
56
+ ### Test scenario (Restart mysql before each testing with each command)
57
+ 1. **Compare above 3 command when table `test_data` has only primary key as index**:
58
+ * Testing with command: `select sum(id) from test_data;`:
59
+
60
+ ```
61
+ mysql> select sum(id) from test_data;
62
+ +----------------+
63
+ | sum(id) |
64
+ +----------------+
65
+ | 50000005000000 |
66
+ +----------------+
67
+ 1 row in set (12.46 sec)
68
+ ```
69
+ **Buffer poll infos**
70
+ ```
71
+ mysql> select table_name as Table_Name, index_name as Index_Name, count(*) as Page_Count, sum(data_size)/1024/1024 as Size_in_MB from information_schema.innodb_buffer_page group by table_name, index_name order by Size_in_MB desc limit 5;
72
+ +---------------------------------+------------+------------+--------------+
73
+ | Table_Name | Index_Name | Page_Count | Size_in_MB |
74
+ +---------------------------------+------------+------------+--------------+
75
+ | `test_mysql_warmup`.`test_data` | PRIMARY | 7940 | 106.90351486 |
76
+ | `SYS_TABLES` | CLUST_IND | 86 | 1.20526028 |
77
+ | `SYS_COLUMNS` | CLUST_IND | 3 | 0.02067661 |
78
+ | `mysql`.`innodb_index_stats` | PRIMARY | 2 | 0.01506424 |
79
+ | `SYS_FIELDS` | CLUST_IND | 1 | 0.00902271 |
80
+ +---------------------------------+------------+------------+--------------+
81
+ 5 rows in set (0.07 sec)
82
+ ```
83
+
84
+ => **Primary key** is loaded with **7940 pages**
85
+
86
+ * Testing with command: `select count(*) from test_data;`:
87
+
88
+ ```
89
+ mysql> select count(*) from test_data;
90
+ | count(*) |
91
+ +----------+
92
+ | 10000000 |
93
+ +----------+
94
+ 1 row in set (11.68 sec)
95
+ ```
96
+
97
+ **Buffer poll infos**
98
+ ```
99
+ mysql> select table_name as Table_Name, index_name as Index_Name, count(*) as Page_Count, sum(data_size)/1024/1024 as Size_in_MB from information_schema.innodb_buffer_page group by table_name, index_name order by Size_in_MB desc limit 5;
100
+ +---------------------------------+------------+------------+--------------+
101
+ | Table_Name | Index_Name | Page_Count | Size_in_MB |
102
+ +---------------------------------+------------+------------+--------------+
103
+ | `test_mysql_warmup`.`test_data` | PRIMARY | 7940 | 106.90351486 |
104
+ | `SYS_TABLES` | CLUST_IND | 86 | 1.20526028 |
105
+ | `SYS_COLUMNS` | CLUST_IND | 3 | 0.02067661 |
106
+ | `mysql`.`innodb_index_stats` | PRIMARY | 2 | 0.01506424 |
107
+ | `SYS_FIELDS` | CLUST_IND | 1 | 0.00902271 |
108
+ +---------------------------------+------------+------------+--------------+
109
+ 5 rows in set (0.08 sec)
110
+ ```
111
+ => **Primary key** is loaded with **7940 pages**
112
+
113
+ * Testing with command: `select count(*) from test_data where random_str = 0 or random_str = '0';`:
114
+
115
+ ```
116
+ mysql> select count(*) from test_data where random_str = 0 or random_str = '0';
117
+ +----------+
118
+ | count(*) |
119
+ +----------+
120
+ | 10000000 |
121
+ +----------+
122
+ 1 row in set, 65535 warnings (19.08 sec)
123
+ ```
124
+
125
+ **Buffer pool infos**
126
+ ```
127
+ mysql> select table_name as Table_Name, index_name as Index_Name, count(*) as Page_Count, sum(data_size)/1024/1024 as Size_in_MB from information_schema.innodb_buffer_page group by table_name, index_name order by Size_in_MB desc limit 5;
128
+ +---------------------------------+------------+------------+--------------+
129
+ | Table_Name | Index_Name | Page_Count | Size_in_MB |
130
+ +---------------------------------+------------+------------+--------------+
131
+ | `test_mysql_warmup`.`test_data` | PRIMARY | 8025 | 108.04996300 |
132
+ | `SYS_COLUMNS` | CLUST_IND | 3 | 0.02067661 |
133
+ | `SYS_FIELDS` | CLUST_IND | 1 | 0.00906658 |
134
+ | `SYS_TABLES` | CLUST_IND | 2 | 0.00766659 |
135
+ | `SYS_INDEXES` | CLUST_IND | 1 | 0.00720882 |
136
+ +---------------------------------+------------+------------+--------------+
137
+ ```
138
+ => **Primary key** is loaded with **8025 pages**
139
+
140
+ 2. **Compare above 3 command when table `test_data` has primary key `id` and field `random_str` as index**
141
+ * First, add index to field `random_str`:
142
+
143
+ ```
144
+ mysql> create index test_data_random_str_index on test_data(random_str);
145
+ Query OK, 0 rows affected (52.24 sec)
146
+ Records: 0 Duplicates: 0 Warnings: 0
147
+
148
+ mysql> desc test_data;
149
+ +-------------+--------------+------+-----+---------+-------+
150
+ | Field | Type | Null | Key | Default | Extra |
151
+ +-------------+--------------+------+-----+---------+-------+
152
+ | id | int(11) | NO | PRI | NULL | |
153
+ | random_str | varchar(255) | YES | MUL | NULL | |
154
+ | created_at | varchar(255) | YES | | NULL | |
155
+ | random_str1 | varchar(255) | YES | | NULL | |
156
+ | random_str2 | varchar(255) | YES | | NULL | |
157
+ | random_str3 | varchar(255) | YES | | NULL | |
158
+ +-------------+--------------+------+-----+---------+-------+
159
+ 6 rows in set (0.00 sec)
160
+
161
+ mysql> show index from test_data;
162
+ +-----------+------------+----------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
163
+ | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
164
+ +-----------+------------+----------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
165
+ | test_data | 0 | PRIMARY | 1 | id | A | 9943860 | NULL | NULL | | BTREE | | |
166
+ | test_data | 1 | test_data_random_str_index | 1 | random_str | A | 9943860 | NULL | NULL | YES | BTREE | | |
167
+ +-----------+------------+----------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
168
+ 2 rows in set (0.00 sec)
169
+ ```
170
+
171
+ * Testing with command: `select sum(id) from test_data;`:
172
+
173
+ ```
174
+ mysql> select sum(id) from test_data;
175
+ +----------------+
176
+ | sum(id) |
177
+ +----------------+
178
+ | 50000005000000 |
179
+ +----------------+
180
+ 1 row in set (3.28 sec)
181
+ ```
182
+
183
+ **Buffer pool infos**
184
+ ```
185
+ mysql> select table_name as Table_Name, index_name as Index_Name, count(*) as Page_Count, sum(data_size)/1024/1024 as Size_in_MB from information_schema.innodb_buffer_page group by table_name, index_name order by Size_in_MB desc limit 5;
186
+ +---------------------------------+----------------------------+------------+--------------+
187
+ | Table_Name | Index_Name | Page_Count | Size_in_MB |
188
+ +---------------------------------+----------------------------+------------+--------------+
189
+ | `test_mysql_warmup`.`test_data` | test_data_random_str_index | 7877 | 120.96562290 |
190
+ | `SYS_TABLES` | CLUST_IND | 86 | 1.20540619 |
191
+ | `SYS_COLUMNS` | CLUST_IND | 3 | 0.02067661 |
192
+ | `SYS_FIELDS` | CLUST_IND | 1 | 0.00906658 |
193
+ | `SYS_INDEXES` | CLUST_IND | 1 | 0.00720882 |
194
+ +---------------------------------+----------------------------+------------+--------------+
195
+ 5 rows in set (0.07 sec)
196
+ ```
197
+
198
+ => **test_data_random_str_index** is loaded to buffer pool instead of `primary key`, with **7877 pages**
199
+
200
+ * Testing with command: `select count(*) from test_data;`:
201
+
202
+ ```
203
+ mysql> select count(*) from test_data;
204
+ +----------+
205
+ | count(*) |
206
+ +----------+
207
+ | 10000000 |
208
+ +----------+
209
+ 1 row in set (15.43 sec)
210
+ ```
211
+ **Buffer pool info**
212
+ ```
213
+ mysql> select table_name as Table_Name, index_name as Index_Name, count(*) as Page_Count, sum(data_size)/1024/1024 as Size_in_MB from information_schema.innodb_buffer_page group by table_name, index_name order by Size_in_MB desc limit 5;
214
+ +---------------------------------+------------+------------+--------------+
215
+ | Table_Name | Index_Name | Page_Count | Size_in_MB |
216
+ +---------------------------------+------------+------------+--------------+
217
+ | `test_mysql_warmup`.`test_data` | PRIMARY | 7940 | 106.90351486 |
218
+ | `SYS_TABLES` | CLUST_IND | 86 | 1.20526028 |
219
+ | `SYS_COLUMNS` | CLUST_IND | 3 | 0.02067661 |
220
+ | `SYS_FIELDS` | CLUST_IND | 1 | 0.00906658 |
221
+ | `SYS_INDEXES` | CLUST_IND | 1 | 0.00720882 |
222
+ +---------------------------------+------------+------------+--------------+
223
+ 5 rows in set (0.06 sec)
224
+ ```
225
+ => **Primary key** is loaded into buffer pool with **7940 pages**.
226
+
227
+ * Testing with command: `select count(*) from test_data where random_str2 = 0 or random_str2 = '0'` (with `random_str2` is `non-index column`):
228
+
229
+ ```
230
+ mysql> select count(*) from test_data where random_str2 = '0' or random_str2 = 0;
231
+ | count(*) |
232
+ +----------+
233
+ | 0 |
234
+ +----------+
235
+ 1 row in set (14.42 sec)
236
+ ```
237
+ **Buffer pool infos**
238
+ ```
239
+ mysql> select table_name as Table_Name, index_name as Index_Name, count(*) as Page_Count, sum(data_size)/1024/1024 as Size_in_MB from information_schema.innodb_buffer_page group by table_name, index_name order by Size_in_MB desc limit 5;
240
+ +---------------------------------+------------+------------+--------------+
241
+ | Table_Name | Index_Name | Page_Count | Size_in_MB |
242
+ +---------------------------------+------------+------------+--------------+
243
+ | `test_mysql_warmup`.`test_data` | PRIMARY | 8039 | 108.24720383 |
244
+ | `SYS_TABLES` | CLUST_IND | 147 | 2.08744812 |
245
+ | NULL | NULL | 5 | 0.00000000 |
246
+ +---------------------------------+------------+------------+--------------+
247
+ 3 rows in set (0.06 sec)
248
+ ```
249
+ => **Primary key** is loaded with **8039 pages** (greater than **7940 pages**)
250
+
251
+ # Conclusion:
252
+ 1. Table with only primary key:
253
+ * `select count(*) from table_name where non_index_column = 0 or non_index_column = '0'` has the greatest pages loaded.
254
+ * `select count(*) from table_name` and `select sum(primary_key) from table_name` has the same result, and the loaded pages is less than query has `where non_index_column = 0 or non_index_column = '0'` condition.
255
+
256
+ 2. Table with primary and other index(es):
257
+ * `select count(*) from table_name`: **primary_key** will be loaded.
258
+ * `select sum(primary_key) from table_name`: **other_index** will be loaded.
259
+ * `select count(*) from table_name where non_index_column = 0 or non_index_column = '0'`: **primary_key** will be loaded with the greatest.
260
+
261
+
262
+
data/bin/mysql-warmup CHANGED
@@ -9,10 +9,10 @@ def print_help
9
9
  puts "mysql-warmup, by ManhDV - version #{MysqlWarmup::VERSION}"
10
10
  puts 'Website: https://github.com/manhdaovan/mysql_warmup'
11
11
  puts
12
- puts 'Usage: mysql-warmup -h host-or-ip -u username <options>'
12
+ puts 'Usage: mysql-warmup -u username <options>'
13
13
  puts
14
14
  puts 'Input options:'
15
- puts '-h host : Host or ip of mysql instance'
15
+ puts '-h host : Host or ip of mysql instance. Default is localhost'
16
16
  puts '-u username : Username to access mysql instance'
17
17
  puts '-d database : Database to warmup.'
18
18
  puts ' Default to all databases exclude information_schema, mysql, performance_schema, sys'
@@ -37,7 +37,7 @@ def params_valid?(options)
37
37
  valid
38
38
  end
39
39
 
40
- options = { show_version: false, help: false, port: 3306, database: 'all' }
40
+ options = { show_version: false, help: false, port: 3306, database: 'all', host: 'localhost' }
41
41
 
42
42
  begin
43
43
  OptionParser.new do |opts|
@@ -3,20 +3,25 @@ module MysqlWarmup
3
3
  QUERY_TEMPLATE = 'select %s from %s where %s'.freeze
4
4
  attr_reader :query_string
5
5
 
6
- def initialize(table_name, column_name, column_type, column_key)
6
+ def initialize(table_name, column_name, column_type, column_key, non_index_field = nil)
7
7
  @table_name = table_name
8
8
  @column_name = column_name
9
9
  @column_type = column_type
10
10
  @column_key = column_key
11
11
  @query_string = build_query_string(@table_name, @column_name,
12
- @column_type, @column_key)
12
+ @column_type, @column_key, non_index_field)
13
13
  end
14
14
 
15
15
  private
16
16
 
17
- def build_query_string(table_name, column_name, column_type, column_key)
17
+ def build_query_string(table_name, column_name, column_type, column_key, non_index_field = nil)
18
18
  if type_primary?(column_key)
19
- format_query("sum(`#{table_name}`.`#{column_name}`)", "`#{table_name}`", '1')
19
+ if non_index_field.nil?
20
+ format_query('count(*)', "`#{table_name}`", '1')
21
+ else
22
+ field_name = "`#{table_name}`.`#{non_index_field}`"
23
+ format_query('count(*)', "`#{table_name}`", "#{field_name} = 0 or #{field_name} = '0'")
24
+ end
20
25
  elsif type_integer?(column_type)
21
26
  format_query('count(*)', "`#{table_name}`", "`#{table_name}`.`#{column_name}` LIKE '%0%'")
22
27
  elsif type_var_char?(column_type)
@@ -20,13 +20,17 @@ module MysqlWarmup
20
20
  private
21
21
 
22
22
  def build_index
23
- indexes_infos = @field_infos.select { |v| !v[DESC_TABLE_STRUCTURE[:key]].empty? }
24
- indexes = []
23
+ indexes_infos = []
24
+ non_indexes_info = []
25
+ @field_infos.each { |v| v[DESC_TABLE_STRUCTURE[:key]].empty? ? non_indexes_info << v : indexes_infos << v }
26
+ non_index_field = non_indexes_info.empty? ? nil : non_indexes_info[0][DESC_TABLE_STRUCTURE[:field]]
27
+ indexes = []
25
28
  indexes_infos.each do |index_info|
26
29
  indexes << MysqlWarmup::Index.new(@table_name,
27
30
  index_info[DESC_TABLE_STRUCTURE[:field]],
28
31
  index_info[DESC_TABLE_STRUCTURE[:type]],
29
- index_info[DESC_TABLE_STRUCTURE[:key]])
32
+ index_info[DESC_TABLE_STRUCTURE[:key]],
33
+ non_index_field)
30
34
  end
31
35
  indexes
32
36
  end
@@ -1,3 +1,3 @@
1
1
  module MysqlWarmup
2
- VERSION = '0.0.2'.freeze
2
+ VERSION = '0.0.3'.freeze
3
3
  end
Binary file
data/test/test_index.rb CHANGED
@@ -6,9 +6,15 @@ class TestIndex < Test::Unit::TestCase
6
6
  def test_query_string
7
7
  types = %w(VARCHAR TEXT BLOB INT DATE OTHERS)
8
8
  # Index for primary
9
+ # 1. without any non-index field
9
10
  index = MysqlWarmup::Index.new('table_name', 'col_name', 'INT', 'PRI')
10
- assert_equal(build_query('sum(`table_name`.`col_name`)', '`table_name`', '1'),
11
+ assert_equal(build_query('count(*)', '`table_name`', '1'),
11
12
  index.query_string)
13
+ # 2. with non-index field
14
+ index2 = MysqlWarmup::Index.new('table_name', 'col_name', 'INT', 'PRI', 'non_index')
15
+ field_name = '`table_name`.`non_index`'
16
+ assert_equal(build_query('count(*)', '`table_name`', "#{field_name} = 0 or #{field_name} = '0'"),
17
+ index2.query_string)
12
18
 
13
19
  # Index for other fields
14
20
  types.each do |type|
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mysql-warmup
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Manh Dao Van
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-01-14 00:00:00.000000000 Z
11
+ date: 2017-01-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mysql
@@ -51,6 +51,7 @@ extensions: []
51
51
  extra_rdoc_files: []
52
52
  files:
53
53
  - CHANGELOG.md
54
+ - CHANGE_SUM_TO_COUNT.md
54
55
  - README.md
55
56
  - Rakefile
56
57
  - bin/console
@@ -62,6 +63,7 @@ files:
62
63
  - lib/mysql_warmup/version.rb
63
64
  - lib/mysql_warmup/warmer.rb
64
65
  - mysql-warmup-0.0.1.gem
66
+ - mysql-warmup-0.0.2.gem
65
67
  - mysql-warmup.gemspec
66
68
  - test/helper.rb
67
69
  - test/test_index.rb