Created
December 20, 2014 23:10
-
-
Save jaymecd/268c79a10dbda19db3f9 to your computer and use it in GitHub Desktop.
SQL simple php stats
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
#!/usr/bin/env php | |
<?php | |
try { | |
$host = '{HOST}'; | |
$t1 = microtime(true); | |
$dbh = new PDO('mysql:dbname={DBNAME};host='. $host, '{USER}', '{PASS}'); | |
$t2 = microtime(true); | |
$sth = $dbh->query('SHOW TABLE STATUS'); | |
$stats = $sth->fetchAll(PDO::FETCH_ASSOC); | |
$tables = []; | |
foreach ($stats as $stat) { | |
$tables[$stat['Name']] = $stat['Rows']; | |
} | |
ksort($tables); | |
echo sprintf('MySQL [%s] connected (%.05f secs)', $host, ($t2-$t1)), PHP_EOL; | |
echo PHP_EOL; | |
$len = array_reduce(array_keys($tables), function ($carry, $key) { return max($carry, strlen($key)); }, 0) + 2; | |
foreach ($tables as $name => $count) { | |
echo sprintf('%\'.-'. $len .'s : %s', $name.' ', number_format($count, 0, ',', '.')), PHP_EOL; | |
} | |
echo PHP_EOL; | |
} catch (PDOException $e) { | |
echo $e, PHP_EOL; | |
exit(1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment