How to get size per day of a table
I have a database with ~20 tables. Each table has a column "dtLogTime" that records the time that row was inserted. I want to figure out the size (probably kb or mb) each table is recording per day. More specifically, I'm only interested in the last 3 days. Also, these tables keep track of data up to a certain time interval (i.e. 2 weeks, 1 month, etc), meaning I lose a day's worth of data for every new day's data stored.
I came across this code that can show me the size of each table.
<?php
$link = mysql_connect('host', 'username', 'password');
$db_name = "your database name here";
$tables = array();
mysql_select_db($db_name, $link);
$result = mysql_query("SHOW TABLE STATUS");
while($row = mysql_fetch_array($result)) {
/* We return the size in开发者_运维知识库 Kilobytes */
$total_size = ($row[ "Data_length" ] +
$row[ "Index_length" ]) / 1024;
$tables[$row['Name']] = sprintf("%.2f", $total_size);
}
print_r($tables);
?>
When I tried doing
"SHOW TABLE STATUS WHERE dtLogTime < '2011-08-28 00:00:00'
AND dtLogTime >= '2011-08-27 00:00:00'"
it gave me an error. Is there a way to do this?
Thanks
You need to include a LIKE
clause to specify the table. Source: http://dev.mysql.com/doc/refman/5.6/en/show-table-status.html
SHOW TABLE STATUS
LIKE YourTable
WHERE dtLogTime < '2011-08-28 00:00:00'
AND dtLogTime >= '2011-08-27 00:00:00'
The Where
clause applies to the resulting table generated by SHOW TABLE STATUS
, and cannot be actual columns of your various tables. For instance:
SHOW TABLE STATUS where Index_length = 0
Run SHOW TABLE STATUS
by itself to see a list of all the legal columns you can use in the WHERE
clause. Unfortunately, for your situation, you'll have to run SHOW TABLE STATUS
each day and store the result somewhere.
UPDATE
For clarification, SHOW TABLE STATUS
is a convenience method that interrogates the system table, INFORMATION_SCHEMA.TABLES
. It will pare down the results from that system table to just the persistent tables in your current database. It doesn't perform any calculations of its own.
精彩评论