line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package MySQL::Warmer; |
2
|
1
|
|
|
1
|
|
500
|
use 5.008001; |
|
1
|
|
|
|
|
2
|
|
3
|
1
|
|
|
1
|
|
3
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
16
|
|
4
|
1
|
|
|
1
|
|
10
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
36
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $VERSION = "0.02"; |
7
|
|
|
|
|
|
|
|
8
|
1
|
|
|
1
|
|
385
|
use DBIx::Inspector; |
|
1
|
|
|
|
|
7038
|
|
|
1
|
|
|
|
|
22
|
|
9
|
|
|
|
|
|
|
|
10
|
1
|
|
|
1
|
|
460
|
use Moo; |
|
1
|
|
|
|
|
11184
|
|
|
1
|
|
|
|
|
3
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
has dbh => ( |
13
|
|
|
|
|
|
|
is => 'ro', |
14
|
|
|
|
|
|
|
isa => sub { shift->isa('DBI::db') }, |
15
|
|
|
|
|
|
|
lazy => 1, |
16
|
|
|
|
|
|
|
default => sub { |
17
|
|
|
|
|
|
|
require DBI; |
18
|
|
|
|
|
|
|
DBI->connect(@{ shift->dsn }); |
19
|
|
|
|
|
|
|
}, |
20
|
|
|
|
|
|
|
); |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
has dsn => ( |
23
|
|
|
|
|
|
|
is => 'ro', |
24
|
|
|
|
|
|
|
isa => sub { ref $_[0] eq 'ARRAY' }, |
25
|
|
|
|
|
|
|
); |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
has dry_run => ( |
28
|
|
|
|
|
|
|
is => 'ro', |
29
|
|
|
|
|
|
|
default => sub { 0 }, |
30
|
|
|
|
|
|
|
); |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
has _inspector => ( |
33
|
|
|
|
|
|
|
is => 'ro', |
34
|
|
|
|
|
|
|
lazy => 1, |
35
|
|
|
|
|
|
|
default => sub { |
36
|
|
|
|
|
|
|
DBIx::Inspector->new(dbh => shift->dbh) |
37
|
|
|
|
|
|
|
}, |
38
|
|
|
|
|
|
|
); |
39
|
|
|
|
|
|
|
|
40
|
1
|
|
|
1
|
|
1183
|
no Moo; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
5
|
|
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
sub run { |
43
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
44
|
|
|
|
|
|
|
|
45
|
0
|
|
|
|
|
|
my $inspector = $self->_inspector; |
46
|
0
|
|
|
|
|
|
for my $table ($inspector->tables) { |
47
|
|
|
|
|
|
|
|
48
|
0
|
|
|
|
|
|
my $indexes = $self->statistics_info(table => $table->name, schema => $table->schema); |
49
|
0
|
|
|
|
|
|
my %indexes; |
50
|
|
|
|
|
|
|
|
51
|
0
|
|
|
|
|
|
for my $index_column (@$indexes) { |
52
|
0
|
|
0
|
|
|
|
$indexes{ $index_column->{index_name} } ||= []; |
53
|
0
|
|
|
|
|
|
push @{ $indexes{ $index_column->{index_name} } }, $index_column->{column_name}; |
|
0
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
0
|
0
|
|
|
|
|
my @indexes = exists $indexes{PRIMARY} ? delete $indexes{PRIMARY} : (); |
57
|
0
|
|
|
|
|
|
push @indexes, values(%indexes); |
58
|
0
|
|
|
|
|
|
for my $cols (@indexes) { |
59
|
0
|
|
|
|
|
|
my @selectee; |
60
|
|
|
|
|
|
|
my @quoted_cols; |
61
|
0
|
|
|
|
|
|
for my $col (@$cols) { |
62
|
0
|
|
|
|
|
|
my $index_column = $table->column($col); |
63
|
|
|
|
|
|
|
|
64
|
0
|
|
|
|
|
|
my $data_type_name = uc $index_column->type_name; |
65
|
0
|
0
|
|
|
|
|
if ($data_type_name =~ /(?:INT(?:EGER)?|FLOAT|DOUBLE|DECI(?:MAL)?)$/) { |
|
|
0
|
|
|
|
|
|
66
|
0
|
|
|
|
|
|
push @selectee, sprintf "SUM(`%s`)", $index_column->name; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
elsif ($data_type_name =~ /(?:DATE|TIME)/) { |
69
|
0
|
|
|
|
|
|
push @selectee, sprintf "SUM(UNIX_TIMESTAMP(`%s`))", $index_column->name; |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
else { |
72
|
0
|
|
|
|
|
|
push @selectee, sprintf "SUM(LENGTH(`%s`))", $index_column->name; |
73
|
|
|
|
|
|
|
} |
74
|
0
|
|
|
|
|
|
push @quoted_cols, sprintf "`%s`", $col; |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
|
77
|
0
|
|
|
|
|
|
my $query = sprintf 'SELECT %s FROM (SELECT %s FROM `%s` ORDER BY %s) as t1;', |
78
|
|
|
|
|
|
|
join(', ', @selectee), join(', ', @quoted_cols), $table->name, join(', ', @quoted_cols); |
79
|
|
|
|
|
|
|
|
80
|
0
|
|
|
|
|
|
print "$query"; |
81
|
0
|
0
|
|
|
|
|
unless ($self->dry_run) { |
82
|
0
|
|
|
|
|
|
$self->dbh->do($query); |
83
|
0
|
|
|
|
|
|
print " ...done!"; |
84
|
|
|
|
|
|
|
} |
85
|
0
|
|
|
|
|
|
print "\n"; |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
sub statistics_info { |
91
|
0
|
|
|
0
|
0
|
|
my ($self, %args) = @_; |
92
|
0
|
|
|
|
|
|
my $table = $args{table}; |
93
|
0
|
|
|
|
|
|
my $schema = $args{schema}; |
94
|
0
|
|
|
|
|
|
my $dbh = $self->dbh; |
95
|
|
|
|
|
|
|
|
96
|
0
|
|
|
|
|
|
my $sql = <<'...'; |
97
|
|
|
|
|
|
|
SELECT |
98
|
|
|
|
|
|
|
index_name, |
99
|
|
|
|
|
|
|
column_name |
100
|
|
|
|
|
|
|
FROM Information_schema.STATISTICS |
101
|
|
|
|
|
|
|
WHERE |
102
|
|
|
|
|
|
|
table_schema = ? AND |
103
|
|
|
|
|
|
|
table_name = ? |
104
|
|
|
|
|
|
|
ORDER BY table_schema, table_name, seq_in_index; |
105
|
|
|
|
|
|
|
... |
106
|
|
|
|
|
|
|
|
107
|
0
|
|
|
|
|
|
my $sth = $dbh->prepare($sql); |
108
|
0
|
|
|
|
|
|
$sth->execute($schema, $table); |
109
|
0
|
|
|
|
|
|
$sth->fetchall_arrayref(+{}); |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
1; |
113
|
|
|
|
|
|
|
__END__ |