查看数据库及表的大小和所占用空间大小

25 篇文章 0 订阅

查看数据库及表的大小和所占用空间大小

Sqlserver

exec sp_spaceused  '表名'  --取得表占用空間 
exec sp_spaceused ”  --数据库所有空間

 

下面的sql也可以通过看表的记录行数来估算大小

SELECT a.name, b.rows 
FROM sysobjects AS a INNER JOIN 
sysindexes AS b ON a.id = b.id 
WHERE (a.type = 'u') AND (b.indid IN (0, 1)) 
ORDER BY b.rows DESC
 

也可以写成函数,方便调用

CREATE FUNCTION [dbo].[fnGetTableRows](@TableName varchar(50)) RETURNS int 
   
  AS 
 BEGIN 
   --距离(千米) 
  DECLARE @Rows int 
      SELECT @Rows=b.rows 
FROM sysobjects AS a INNER JOIN 
sysindexes AS b ON a.id = b.id 
WHERE (a.type = 'u') AND (b.indid IN (0, 1))  and a.name=@TableName  

  RETURN @Rows 
 END

调用: 

declare @i int
set @i=dbo.fnGetTableRows('T_User')
print @i

 

Mysql

# check table rows

select table_name,table_rows from  information_schema.`TABLES` where TABLE_SCHEMA='数据库名' order by table_rows desc limit 10;

# check database size  这里的单位是 Mb,如果要看 GB多除一个1024即可
#SELECT table_schema as "DB Name",Round(Sum(data_length + index_length) / 1024 / 1024 , 1) as "DB Size in MB"  FROM   information_schema.tables  GROUP  BY table_schema;

 

# check table size 这里的单位是 Mb,如果要看 GB多除一个1024即可

SELECT table_schema as `Database`, table_name AS `Table`, round(((data_length + index_length) / 1024 / 1024), 2) `Size in MB` FROM information_schema.TABLES where table_schema = '数据库名' ORDER BY (data_length + index_length) DESC ;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值