Skip to content

Instantly share code, notes, and snippets.

@jaymecd
Created December 20, 2014 23:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaymecd/268c79a10dbda19db3f9 to your computer and use it in GitHub Desktop.
Save jaymecd/268c79a10dbda19db3f9 to your computer and use it in GitHub Desktop.
SQL simple php stats
#!/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