Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find out the tables that take up maximum memory in database?

Hi I am new to databases. I am working on huge database and trying to clear up the mess. I want to start by finding the top ten tables that take up highest memory in the whole database. I cannot go by finding memory of each table since there are too many tables. I need the top 10 or 20 tables that take up the maximum space. Any help would be much appreciated. Thank you.

like image 420
Maddy Avatar asked Mar 29 '12 20:03

Maddy


People also ask

How do I find the memory size of a table?

This can be accomplished easily with the following query: SELECT TABLE_SCHEMA AS `Database`, TABLE_NAME AS `Table`, ROUND((DATA_LENGTH + INDEX_LENGTH) / 1024 / 1024) AS `Size (MB)` FROM information_schema.

How do I find maximum memory for SQL Server?

In Object Explorer, right-click a server and select Properties. Click the Memory node. Under Server Memory Options, enter the amount that you want for Minimum server memory and Maximum server memory.


4 Answers

Maybe something like this:

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;

Reference here

like image 132
Arion Avatar answered Nov 17 '22 13:11

Arion


MyISAM only takes up memory for its indexes

To find the top 10 MyISAM tables that can use the most memory in the worst case try this:

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 takes up memory for its data and indexes

To find the top 10 InnoDB tables that can use the most memory in the worst case try this:

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;

Here is another display of the top 50 tables by size descending

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;

If you are interested, I have queries that give you the whole story on the MySQL Instance

This query shows you the amount of disk space taken by Storage Engine 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;

This query shows you the amount of disk space taken by 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);

This query shows you the amount of disk space taken by Database by Storage Engine 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;

The three previous queries I posted has a common feature : the subquery (SELECT 3 pw)

  • If you use (SELECT 0 pw), report is in Bytes
  • If you use (SELECT 1 pw), report is in KiloBytes
  • If you use (SELECT 2 pw), report is in MegaBytes
  • If you use (SELECT 3 pw), report is in GigaBytes
  • If you use (SELECT 4 pw), report is in TeraBytes
  • If you use (SELECT 5 pw), report is in PetaBytes (If you need this, post that result please !!!)
like image 4
RolandoMySQLDBA Avatar answered Nov 17 '22 13:11

RolandoMySQLDBA


You could use SHOW TABLE STATUS to get the sizes for each table.

like image 2
stewe Avatar answered Nov 17 '22 15:11

stewe


This is the query I used after reading all your answers.

  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;
like image 2
Maddy Avatar answered Nov 17 '22 15:11

Maddy