2012-03-29 6 views
8

Ciao, sono nuovo ai database. Sto lavorando su un enorme database e sto cercando di chiarire il caos. Voglio iniziare trovando le prime dieci tabelle che occupano la memoria più alta nell'intero database. Non posso andare trovando memoria di ogni tavolo poiché ci sono troppe tabelle. Ho bisogno dei primi 10 o 20 tavoli che occupino lo spazio massimo. Qualsiasi aiuto sarebbe molto apprezzato. Grazie.Come scoprire le tabelle che occupano la memoria massima nel database?

+0

Vuoi consumo di memoria o spazio su disco ??? – RolandoMySQLDBA

+0

Desidero spazio su disco e consumo di memoria. – Maddy

risposta

4

MyISAM richiede solo la memoria per i suoi indici

Per trovare i primi 10 tabelle MyISAM che possono utilizzare la maggior parte della memoria nel caso peggiore provare questo:

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 occupa memoria per i suoi dati e gli indici

Per trovare i primi 10 tabelle InnoDB che possono utilizzare la maggior parte della memoria nel caso peggiore provare questo:

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; 

Ecco un altro display dei 50 tavoli per dimensione decrescente

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; 

Se siete interessati, ho le query che ti danno tutta la storia sul MySQL Instance

Questa query mostra la quantità di spazio su disco presa dal motore di archiviazione in 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; 

Questa query mostra la quantità di spazio su disco occupato dal database in GB

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); 

Questa query mostra la quantità di spazio su disco occupato dal database dal motore di archiviazione in 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; 

I tre domande precedenti ho postato ha una caratteristica comune: la subquery (SELECT 3 pw)

  • Se si utilizza (SELECT 0 pw), il report è in byte
  • Se si utilizza (SELECT 1 pw), il report è in KiloBytes
  • Se si utilizza (SELECT 2 pw), rapporto è in megabyte
  • Se si utilizza (SELECT 3 pw), report è in gigabyte
  • Se si utilizza (SELECT 4 pw), Relazione è in terabyte
  • Se si utilizza (SELECT 5 pw), rapporto è in petabyte (Se avete bisogno questo, inserisci quel risultato per favore !!!)
+0

Questo è fantastico !! Sei fantastico! Mi ha aiutato molto !! .. :) – Maddy

+1

Dovresti pubblicare domande come questa in dba.stackexchange.com. Anch'io sono in quel forum. – RolandoMySQLDBA

+0

Siete i benvenuti !!! – RolandoMySQLDBA

5

Forse qualcosa di simile:

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; 

Riferimento here

+0

Grazie mille. – Maddy

+1

Nessun problema. Felice di aiutarti – Arion

2

Questa è la query che ho usato dopo aver letto tutte le tue risposte.

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 per usare la mia risposta per formulare la tua – RolandoMySQLDBA

Problemi correlati