Created
April 15, 2015 14:12
-
-
Save agramajo/8e618b3b1072e13ea3cb to your computer and use it in GitHub Desktop.
show mysql database size
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use DBI; | |
my $dbname = $ARGV[0] || "mysql"; | |
my $dbuser = $ARGV[1] || "root"; | |
my $dbpass = $ARGV[2] || "pass"; | |
my $dbhost = $ARGV[3] || "localhost"; | |
my $socket = ""; | |
$dsn = "DBI:mysql:database=$dbname;host=$dbhost;mysql_socket=$socket"; | |
$dbh = DBI->connect($dsn, $dbuser, $dbpass); | |
$sth = $dbh->prepare("SHOW TABLE STATUS"); | |
$sth->execute; | |
format STDOUT_TOP = | |
Tabla Total DataLen IdxLen Rows AvgLen Engine | |
------------------------------------------------------------------------------ | |
. | |
format STDOUT = | |
@<<<<<<<<<<<<<<<<<<<<<<< @>>>>>>> @>>>>>>> @>>>>>>> @>>>>>>> @>>>>>>> @>>>>>>> | |
$fname, $fndata, $fnindex, $ftotal, $frows, $favg_rows, $fengine | |
. | |
while ( my $r = $sth->fetchrow_hashref ) { | |
my $total = $r->{'Data_length'} + $r->{'Index_length'}; | |
$hash{ $r->{'Name'} }->{TOTAL} = $total; | |
$hash{ $r->{'Name'} }->{DATA} = $r->{'Data_length'}; | |
$hash{ $r->{'Name'} }->{INDEX} = $r->{'Index_length'}; | |
$hash{ $r->{'Name'} }->{ROWS} = $r->{'Rows'}; | |
$hash{ $r->{'Name'} }->{AVGROWS} = $r->{'Avg_row_length'}; | |
$hash{ $r->{'Name'} }->{ENGINE} = $r->{'Engine'}; | |
} | |
foreach $n (sort { $hash{$b}->{DATA} <=> $hash{$a}->{DATA} } keys %hash) { | |
$fname = $n; | |
$fndata = size2human( $hash{$n}->{DATA} ); | |
$fnindex = size2human( $hash{$n}->{INDEX} ); | |
$ftotal = size2human( $hash{$n}->{TOTAL} ); | |
$frows = $hash{$n}->{ROWS}; | |
$favg_rows = size2human( $hash{$n}->{AVGROWS} ); | |
$fengine = $hash{$n}->{ENGINE}; | |
write; | |
} | |
sub size2human { | |
my $data = shift; | |
my $tmp; | |
if ($data > 1024**3) { $tmp = sprintf "%0.2fG", $data / 1024**3; } | |
elsif ($data > 1024**2) { $tmp = sprintf "%0.2fM", $data / 1024**2; } | |
elsif ($data > 1024) { $tmp = sprintf "%0.2fK", $data / 1024; } | |
else { $tmp = $data; } | |
return $tmp; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment