File Coverage

blib/lib/MySQL/Warmer.pm
Criterion Covered Total %
statement 18 52 34.6
branch 0 8 0.0
condition 0 2 0.0
subroutine 6 8 75.0
pod 0 2 0.0
total 24 72 33.3


line stmt bran cond sub pod time code
1             package MySQL::Warmer;
2 1     1   501 use 5.008001;
  1         4  
  1         42  
3 1     1   4 use strict;
  1         1  
  1         29  
4 1     1   13 use warnings;
  1         1  
  1         37  
5              
6             our $VERSION = "0.01";
7              
8 1     1   1347 use DBIx::Inspector;
  1         9428  
  1         24  
9              
10 1     1   1720 use Moo;
  1         12108  
  1         4  
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   1380 no Moo;
  1         9  
  1         6  
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 0           for my $col (@$cols) {
61 0           my $index_column = $table->column($col);
62 0           my $data_type_name = uc $index_column->type_name;
63 0 0         if ($data_type_name =~ /(?:INT(?:EGER)?|FLOAT|DOUBLE|DECI(?:MAL)?)$/) {
    0          
64 0           push @selectee, sprintf "SUM(%s)", $index_column->name;
65             }
66             elsif ($data_type_name =~ /(?:DATE|TIME)/) {
67 0           push @selectee, sprintf "SUM(UNIX_TIMESTAMP(%s))", $index_column->name;
68             }
69             else {
70 0           push @selectee, sprintf "SUM(LENGTH(%s))", $index_column->name;
71             }
72             }
73              
74 0           my $query = sprintf 'SELECT %s FROM (SELECT %s FROM %s ORDER BY %s) as t1;',
75             join(', ', @selectee), join(', ', @$cols), $table->name, join(', ', @$cols);
76              
77 0           print "$query";
78 0 0         unless ($self->dry_run) {
79 0           $self->dbh->do($query);
80 0           print " ...done!";
81             }
82 0           print "\n";
83             }
84             }
85             }
86              
87             sub statistics_info {
88 0     0 0   my ($self, %args) = @_;
89 0           my $table = $args{table};
90 0           my $schema = $args{schema};
91 0           my $dbh = $self->dbh;
92              
93 0           my $sql = <<'...';
94             SELECT
95             index_name,
96             column_name
97             FROM Information_schema.STATISTICS
98             WHERE
99             table_schema = ? AND
100             table_name = ?
101             ORDER BY table_schema, table_name, seq_in_index;
102             ...
103              
104 0           my $sth = $dbh->prepare($sql);
105 0           $sth->execute($schema, $table);
106 0           $sth->fetchall_arrayref(+{});
107             }
108              
109             1;
110             __END__