日期:2014-05-16  浏览次数:20498 次

计算mysql数据库数据表的大小,不精确,只是大概的数据

SHOW TABLE STATUS dbname
返回数组:
Name: xxx (表名)
Engine: MyISAM (表引擎)
Version: 10 (版本)
Row_format: Dynamic (行格式)
Rows: (表内总行数)
Avg_row_length: (平均每行大小,这里是4.7K)
Data_length: (该表总大小,单位字节)
Max_data_length: (该表可存储上限)
Index_length: (索引大小)
Data_free: (数据多余)
Auto_increment: (自动累加ID 6W9,而前面的行数只有5W6,说明我有删掉了1W3笔数据)
Create_time:
Update_time:
Check_time:
Collation: 编码
Checksum:
Create_options: row_format=DYNAMIC
Comment: (注释)

数据库的大小=表结构+表数据+索引 实际情况大多采用Data_length+Index_length

实例代码:test数据库名,可以接 like 表。SHOW TABLE STATUS FROM `test` like biao
(注:+9000的意思,数据库文件中有个.frm无法通过代码取得其大小,而此文件的大约是9k,或者更大)

<?php
include ("comment.php");

$sql="SHOW TABLE STATUS FROM `cx_fcd` ";
$result = mysql_query($sql);
$array=mysql_fetch_array($result,MYSQL_ASSOC);

$t=0;
$result1 = $db->sql_query($sql);
while ($row = $db->sql_fetchrow($result))
{
$lycs[]=$row;
$t+=$row['Index_length']+$row['Data_length']+9000;
}//取得新闻列表

echo "表名:".$array['Name'].",Data_length大 小:".$array['Data_length'].",Index_length大小:".$array['Index_length'].",总大 小:".($array['Data_length']+$array['Index_length'])."<br>";
echo "数据库大小:".round($t/1024)."KB<br>";
print_r($lycs);

?>

下面是显示部分结果,(实际数据库大小为:258 KB 相差16KB)
表名:cx_admin_nav,Data_length大小:208,Index_length大小:2048,总大小:2256
数据库大小:242KB
Array ( [0] => Array ( [Name] => cx_ejcontent [Engine] => MyISAM [Version] => 10 [Row_format] => Dynamic [Rows] => 0 [Avg_row_length] => 0 [Data_length] => 0 [Max_data_length] => 281474976710655 [Index_length] => 1024 [Data_free] => 0 [Auto_increment] => 1 [Create_time] => 2010-06-24 08:30:24 [Update_time] => 2010-06-24 08:30:26 [Check_time] => [Collation] => utf8_general_ci [Checksum] => [Create_options] => [Comment] => ) [1] => Array ( [Name] => cx_ejsub_details [Engine] => MyISAM [Version] => 10 [Row_format] => Dynamic [Rows] => 0 [Avg_row_length] => 0 [Data_length] => 0 [Max_data_length] => 281474976710655 [Index_length] => 1024 [Data_free] => 0 [Auto_increment] => 1 [Create_time] => 2010-06-24 08:31:18 [Update_time] => 2010-06-24 08:31:20 [Check_time] => [Collation] => utf8_general_ci [Checksum] => [Create_options] => [Comment] => ) )

?

?