数据库表空间大小统计

类别:编程语言 点击:0 评论:0 推荐:
<?php
define("CFG_OSTYPE", "linux");          // 操作系统类型,linux | windows
define("CFG_DBTYPE","oracle");          // 数据库类型,mysql | mssql | oracle
define("CFG_SQLSERVER","localhost");    // 数据库服务器名
define("CFG_SQLUSER","root");           // 数据库用户名
define("CFG_SQLPASSWORD","root@sql");   // 数据库用户密码
define("CFG_DATABASE","testdb");        // 数据库名

require_once('./DB.php');

//数据库占用空间
$DbSize = 0;
switch(CFG_DBTYPE)
{
    case "mysql":
        {
            $sql = "SHOW TABLE STATUS FROM ".CFG_DATABASE;
            $res = $DB->sql_query($sql);
            while($row = $DB->sql_fetchrow($res))
            {
                $DbSize += $row['Data_length'];
            }
            $DB->sql_freeresult();
            $DbSize = intval($DbSize/1024);
        }
        break;
    case "mssql":
        {
            $sql = "SELECT SUM(CONVERT(float,size)) * (8192.0/1024.0) AS spaceused FROM [dbo].[sysfiles]";
            $res = $DB->sql_query($sql);
            $row = $DB->sql_fetchrow($res);
            $DbSize = intval($row['spaceused']);
            $DB->sql_freeresult();
        }
        break;
    case "oracle":
        {
            $sql = "SELECT DISTINCT ((c.BYTES-b.BYTES)/1024) as spaceused FROM dba_tables a,dba_free_space b,dba_data_files c WHERE a.OWNER='".strtoupper(CFG_DATABASE)."' and a.TABLESPACE_NAME=b.TABLESPACE_NAME and a.TABLESPACE_NAME=c.TABLESPACE_NAME";
            $res = $DB->sql_query($sql);
            $row = $DB->sql_fetchrow($res);
            $DbSize = intval($row['spaceused']);
            $DB->sql_freeresult();
        }
        break;
}

echo "数据库占用空间:".$DbSize." K";
?>

本文地址:http://com.8s8s.com/it/it25924.htm