2012-03-29 15 views
8

Hola, soy nuevo en las bases de datos. Estoy trabajando en una enorme base de datos y tratando de resolver el problema. Quiero comenzar encontrando las diez tablas principales que ocupan la memoria más alta en toda la base de datos. No puedo encontrar el recuerdo de cada tabla porque hay demasiadas tablas. Necesito las 10 o 20 tablas que ocupan el máximo espacio. Cualquier ayuda sería muy apreciada. Gracias.¿Cómo encontrar las tablas que ocupan la memoria máxima en la base de datos?

+0

¿Desea el consumo de memoria o espacio en disco ??? – RolandoMySQLDBA

+0

Quiero espacio en disco y consumo de memoria. – Maddy

Respuesta

4

MyISAM sólo ocupa memoria para sus índices

Para encontrar las 10 tablas MyISAM superiores que pueden utilizar la mayor cantidad de memoria en el peor de los casos intente esto:

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 lleva a la memoria para poner sus datos y los índices

Para encontrar las 10 tablas InnoDB superiores que pueden utilizar la mayor cantidad de memoria en el peor de los casos intente esto:

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; 

Aquí es otra muestra de las 50 tablas por tamaño descendente

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; 

Si está interesado, no tengo dudas que le dan toda la historia de la instancias de MySQL

Esta consulta muestra el importe de espacio en disco tomada por motor de almacenamiento en 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; 

Esta consulta muestra la cantidad de espacio en disco que por la base de datos en 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); 

Esta consulta muestra la cantidad de espacio de disco ocupado por la base de datos de motor de almacenamiento en 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; 

Los tres consultas anteriores he publicado tiene una característica común: la subconsulta (SELECT 3 pw)

  • Si usted usa (SELECT 0 pw), el informe está en Bytes
  • Si usa (SELECT 1 pw), el informe está en KiloBytes
  • Si utiliza (SELECT 2 pw), el informe es en megabytes
  • Si utiliza (SELECT 3 pw), el informe es en gigabytes
  • Si utiliza (SELECT 4 pw), informe está en terabytes
  • Si utiliza (SELECT 5 pw), el informe está en PetaBytes (Si necesita esto, publique ese resultado por favor !!!)
+0

¡Esto es genial! ¡Usted es maravilloso! Me ayudó mucho ... :) – Maddy

+1

Deberías publicar preguntas como esta en dba.stackexchange.com. Estoy en ese foro también. – RolandoMySQLDBA

+0

De nada! – RolandoMySQLDBA

5

Tal vez algo como esto:

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; 

Referencia here

+0

Muchas gracias. – Maddy

+1

No hay problema. Me alegro de ayudarte – Arion

2

Esta es la consulta que utilicé después de leer todas sus respuestas.

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 por usar mi respuesta para formular tu propia – RolandoMySQLDBA

Cuestiones relacionadas