2012-03-29 91 views
8

嗨,我是数据库的新手。我正在处理庞大的数据库,并试图清理混乱。我想从查找整个数据库中占用最高内存的前十个表开始。由于表格太多,我无法找到每个表格的内存。我需要占据最大空间的前10或20个桌子。任何帮助将非常感激。谢谢。如何找出占用数据库最大内存的表?

+0

你要消耗内存或磁盘空间??? – RolandoMySQLDBA 2012-03-29 20:22:19

+0

我想要磁盘空间和内存消耗。 – Maddy 2012-03-29 20:45:15

回答

4

的MyISAM只占用存储器,用于其指标

要找到能在最坏的情况下使用的内存最多的前10 MyISAM表试试这个:

SELECT * FROM 
(
    SELECT table_schema,table_name,index_length 
    FROM information_schema.tables 
    WHERE engine='MyISAM' AND 
    table_schema NOT IN ('information_schema','mysql','performance_schema') 
    ORDER BY index_length DESC 
) LIMIT 10; 

的InnoDB占用存储器,用于其数据和索引

要找到能在最坏的情况下使用的内存最多的前10 InnoDB表试试这个:

SELECT * FROM 
(
    SELECT table_schema,table_name,data_length+index_length tblsize 
    FROM information_schema.tables 
    WHERE engine='InnoDB' 
    ORDER BY index_length DESC 
) LIMIT 10; 

这里是降

SELECT * FROM 
(SELECT TN TableName,LPAD(REPLACE(FORMAT(TS/POWER(1024,1),2),',',''),Z,' ') KB, 
LPAD(REPLACE(FORMAT(TS/POWER(1024,2),2),',',''),Z,' ') MB, 
LPAD(REPLACE(FORMAT(TS/POWER(1024,3),2),',',''),Z,' ') GB 
FROM (SELECT CONCAT(table_schema,'.',table_name) TN, 
(data_length+index_length) TS FROM information_schema.tables 
WHERE table_schema NOT IN ('information_schema','mysql','performance_schema') 
AND engine IS NOT NULL) A,(SELECT 13 Z) B ORDER BY TS DESC) MMM LIMIT 50; 

如果你有兴趣,我有给你的MySQL实例

原委查询该查询将演示量排名前50位表按大小的另一个显示通过存储引擎在GB

SELECT IFNULL(B.engine,'Total') "Storage Engine", 
CONCAT(LPAD(REPLACE(FORMAT(B.DSize/POWER(1024,pw),3),',',''),17,' '), 
' ',SUBSTR(' KMGTP',pw+1,1),'B') "Data Size", 
CONCAT(LPAD(REPLACE(FORMAT(B.ISize/POWER(1024,pw),3),',',''),17,' '), 
' ',SUBSTR(' KMGTP',pw+1,1),'B') "Index Size", CONCAT(LPAD(REPLACE(
FORMAT(B.TSize/POWER(1024,pw),3),',',''),17,' '),' ', 
SUBSTR(' KMGTP',pw+1,1),'B') "Table Size" 
FROM (SELECT engine,SUM(data_length) DSize,SUM(index_length) ISize, 
SUM(data_length+index_length) TSize FROM information_schema.tables 
WHERE table_schema NOT IN ('mysql','information_schema','performance_schema') 
AND engine IS NOT NULL GROUP BY engine WITH ROLLUP) B,(SELECT 3 pw) A ORDER BY TSize; 

此查询显示您的数据库在GB

采取的磁盘空间量采取的磁盘空间3210
SELECT DBName,CONCAT(LPAD(FORMAT(SDSize/POWER(1024,pw),3),17,' '), 
' ',SUBSTR(' KMGTP',pw+1,1),'B') "Data Size",CONCAT(LPAD(FORMAT(SXSize/ 
POWER(1024,pw),3),17,' '),' ',SUBSTR(' KMGTP',pw+1,1),'B') "Index Size", 
CONCAT(LPAD(FORMAT(STSize/POWER(1024,pw),3),17,' '),' ', 
SUBSTR(' KMGTP',pw+1,1),'B') "Total Size" FROM 
(SELECT IFNULL(DB,'All Databases') DBName,SUM(DSize) SDSize, 
SUM(XSize) SXSize,SUM(TSize) STSize FROM (SELECT table_schema DB, 
data_length DSize,index_length XSize,data_length+index_length TSize 
FROM information_schema.tables WHERE table_schema NOT IN 
('mysql','information_schema','performance_schema')) AAA 
GROUP BY DB WITH ROLLUP) AA,(SELECT 3 pw) BB ORDER BY (SDSize+SXSize); 

此查询显示你的存储引擎在GB

SELECT IF(ISNULL(B.table_schema)+ISNULL(B.engine)=2,"Storage for All Databases", 
IF(ISNULL(B.table_schema)+ISNULL(B.engine)=1,CONCAT("Storage for ",B.table_schema), 
CONCAT(B.engine," Tables for ",B.table_schema))) Statistic,CONCAT(LPAD(REPLACE(
FORMAT(B.DSize/POWER(1024,pw),3),',',''),17,' '),' ',SUBSTR(' KMGTP',pw+1,1),'B') 
"Data Size",CONCAT(LPAD(REPLACE(FORMAT(B.ISize/POWER(1024,pw),3),',',''),17,' '), 
' ',SUBSTR(' KMGTP',pw+1,1),'B') "Index Size",CONCAT(LPAD(REPLACE(FORMAT(B.TSize/ 
POWER(1024,pw),3),',',''),17,' '),' ',SUBSTR(' KMGTP',pw+1,1),'B') "Table Size" 
FROM (SELECT table_schema,engine,SUM(data_length) DSize,SUM(index_length) ISize, 
SUM(data_length+index_length) TSize FROM information_schema.tables WHERE 
table_schema NOT IN ('mysql','information_schema','performance_schema') 
AND engine IS NOT NULL GROUP BY table_schema,engine WITH ROLLUP) B, 
(SELECT 3 pw) A ORDER BY TSize; 

前面的三个查询我贴有一个共同的特点采取数据库的磁盘空间量:子查询(SELECT 3 pw)

  • 如果您使用(SELECT 0 pw),报告在字节
  • 如果使用(SELECT 1 pw),报告以千分之一磅为单位
  • 如果你使用(SELECT 2 pw),报告以MB为单位
  • 如果使用(SELECT 3 pw),报告以GB为
  • 如果使用(SELECT 4 pw),报告以TB为
  • 如果使用(SELECT 5 pw),报告是在PB级(如果您需要这个,请发布那个结果!!!)
+0

这太棒了!你太棒了!它帮助了我很多!! :) – Maddy 2012-03-29 20:49:56

+1

你应该在dba.stackexchange.com发布这样的问题。我也在这个论坛上。 – RolandoMySQLDBA 2012-03-29 20:49:59

+0

不客气! – RolandoMySQLDBA 2012-03-29 20:50:14

5

也许是这样的:

SELECT CONCAT(table_schema, '.', table_name), 
     CONCAT(ROUND(table_rows/1000000, 2), 'M')         rows, 
     CONCAT(ROUND(data_length/(1024 * 1024 * 1024), 2), 'G')     DATA, 
     CONCAT(ROUND(index_length/(1024 * 1024 * 1024), 2), 'G')     idx, 
     CONCAT(ROUND((data_length + index_length)/(1024 * 1024 * 1024), 2), 'G') total_size, 
     ROUND(index_length/data_length, 2)           idxfrac 
FROM information_schema.TABLES 
ORDER BY data_length + index_length DESC 
LIMIT 10; 

参考here

+0

非常感谢。 – Maddy 2012-03-29 20:44:53

+1

没问题。很高兴帮助你 – Arion 2012-03-29 20:45:21

2

这是我在阅读所有答案后使用的查询。

SELECT table_name,round((data_length+index_length)/(1024 * 1024 *1024),2) table_size 
    FROM information_schema.tables 
    ORDER BY data_length + index_length 
    DESC limit 10; 
+1

+1使用我的答案制定你自己的 – RolandoMySQLDBA 2012-03-29 21:04:30