| 1 | package MySQL::Warmer; | ||||||||||||||
| 2 | 1 | ▶ | use 5.008001; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | |||||||||||||||
| 6 | 1 | our $VERSION = "0.02"; | |||||||||||||
| 7 | |||||||||||||||
| 8 | 1 | ▶ | use DBIx::Inspector; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | |||||||||||||||
| 10 | 1 | ▶ | use Moo; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | |||||||||||||||
| 12 | has dbh => ( | ||||||||||||||
| 13 | is => 'ro', | ||||||||||||||
| 14 | 0 | ▶ | isa => sub { shift->isa('DBI::db') }, | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 15 | lazy => 1, | ||||||||||||||
| 16 | 0 | ▶ | default => sub { | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 17 | 0 | require DBI; | |||||||||||||
| 18 | 0 | DBI->connect(@{ shift->dsn }); | |||||||||||||
| 19 | }, | ||||||||||||||
| 20 | 1 | ); | |||||||||||||
| 21 | |||||||||||||||
| 22 | has dsn => ( | ||||||||||||||
| 23 | is => 'ro', | ||||||||||||||
| 24 | 0 | ▶ | isa => sub { ref $_[0] eq 'ARRAY' }, | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 25 | 1 | ); | |||||||||||||
| 26 | |||||||||||||||
| 27 | has dry_run => ( | ||||||||||||||
| 28 | is => 'ro', | ||||||||||||||
| 29 | 0 | ▶ | default => sub { 0 }, | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 30 | 1 | ); | |||||||||||||
| 31 | |||||||||||||||
| 32 | has _inspector => ( | ||||||||||||||
| 33 | is => 'ro', | ||||||||||||||
| 34 | lazy => 1, | ||||||||||||||
| 35 | 0 | ▶ | default => sub { | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 36 | 0 | DBIx::Inspector->new(dbh => shift->dbh) | |||||||||||||
| 37 | }, | ||||||||||||||
| 38 | 1 | ); | |||||||||||||
| 39 | |||||||||||||||
| 40 | 1 | ▶ | no Moo; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 41 | |||||||||||||||
| 42 | 0 | ▶ | sub run { | ||||||||||||
|
Subroutine
run: not called CC=10 SCAR=47.0
Pod
undocumented
| |||||||||||||||
| 43 | 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 | ▶ | $indexes{ $index_column->{index_name} } ||= []; | ||||||||||||
|
Condition0%0/2
$indexes{$index_column->{"index_name"}} ||= []
MC/DC0%0/1
$indexes{$index_column->{"index_name"}} ||= []
$indexes{$index_column->{"index_name"}}
Truth table0%0/2
A
A= $indexes{$index_column->{"index_name"}}
| |||||||||||||||
| 53 | 0 | push @{ $indexes{ $index_column->{index_name} } }, $index_column->{column_name}; | |||||||||||||
| 54 | } | ||||||||||||||
| 55 | |||||||||||||||
| 56 | 0 | ▶ | my @indexes = exists $indexes{PRIMARY} ? delete $indexes{PRIMARY} : (); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 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 | ▶ | if ($data_type_name =~ /(?:INT(?:EGER)?|FLOAT|DOUBLE|DECI(?:MAL)?)$/) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 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 | ▶ | unless ($self->dry_run) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 82 | 0 | $self->dbh->do($query); | |||||||||||||
| 83 | 0 | print " ...done!"; | |||||||||||||
| 84 | } | ||||||||||||||
| 85 | 0 | print "\n"; | |||||||||||||
| 86 | } | ||||||||||||||
| 87 | } | ||||||||||||||
| 88 | } | ||||||||||||||
| 89 | |||||||||||||||
| 90 | 0 | ▶ | sub statistics_info { | ||||||||||||
|
Subroutine
statistics_info: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 91 | 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 | 1; | |||||||||||||
| 113 | __END__ | ||||||||||||||