2012-03-29 97 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

相關問題