1. 程式人生 > >查詢數據庫和表的大小

查詢數據庫和表的大小

sql desc select 數據庫 和表的大小 orm com 空間大小 post

如果想知道MySQL數據庫中每個表占用的空間、表記錄的行數的話,可以打開MySQL的 information_schema 數據庫。在該庫中有一個 TABLES 表,這個表主要字段分別是:

TABLE_SCHEMA : 數據庫名

TABLE_NAME:表名

ENGINE:所使用的存儲引擎

TABLES_ROWS:記錄數

DATA_LENGTH:數據大小

INDEX_LENGTH:索引大小

這幾個字段對我們來說最有用。 一個表占用空間的大小,相當於是 數據大小 + 索引大小,

首先查詢所有數據庫占用磁盤空間大小的SQL語句如下:

select TABLE_SCHEMA, concat(truncate(sum(data_length)/1024
/1024,2), MB) as data_size, concat(truncate(sum(index_length)/1024/1024,2),MB) as index_size from information_schema.tables group by TABLE_SCHEMA order by data_length desc;

然後是查詢單個數據庫裏面各個表所占磁盤空間大小包括其索引的大小的SQL語句如下:

select TABLE_NAME, concat(truncate(data_length/1024/1024,2), MB) as data_size,  
concat(truncate(index_length
/1024/1024,2), MB) as index_size from information_schema.tables where TABLE_SCHEMA = TestDB group by TABLE_NAME order by data_length desc;

註意將SQL語句中的TestDB換成你自己的數據庫名稱即可!

要想知道每個數據庫的大小的話,步驟如下:

1、進入information_schema 數據庫(存放了其他的數據庫的信息)

  use information_schema;
2、查詢所有數據的大小:

  select concat(round(sum(data_length/1024/1024),2),‘MB‘) as data from tables;

3、查看指定數據庫的大小: 比如查看數據庫home的大小

  select concat(round(sum(data_length/1024/1024),2),‘MB‘) as data from tables where table_schema=‘home‘;

4、查看指定數據庫的某個表的大小 比如查看數據庫home中 members 表的大小

  select concat(round(sum(data_length/1024/1024),2),‘MB‘) as data from tables where table_schema=‘home‘ and table_name=‘members‘;

參考:

http://blog.csdn.net/wangpanyang/article/details/46005209

http://www.jb51.net/article/39130.htm

http://www.t086.com/article/5167

查詢數據庫和表的大小