Last active
November 24, 2016 13:11
-
-
Save dgoguerra/367c22d889b82dfb09bb to your computer and use it in GitHub Desktop.
MySQL tables size
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- From: http://stackoverflow.com/a/9620273 | |
-- Change 'SCHEMA_NAME_HERE' | |
SELECT table_name AS "Tables", | |
round(((data_length + index_length) / 1024 / 1024), 2) "Size in MB" | |
FROM information_schema.TABLES | |
WHERE table_schema = 'SCHEMA_NAME_HERE' | |
ORDER BY (data_length + index_length) DESC; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- based on http://dba.stackexchange.com/a/46098 | |
-- size of a table | |
SELECT | |
CONCAT(FORMAT(DAT/POWER(1024,pw1),2),' ',SUBSTR(units,pw1*2+1,2)) DATSIZE, | |
CONCAT(FORMAT(NDX/POWER(1024,pw2),2),' ',SUBSTR(units,pw2*2+1,2)) NDXSIZE, | |
CONCAT(FORMAT(TBL/POWER(1024,pw3),2),' ',SUBSTR(units,pw3*2+1,2)) TBLSIZE | |
FROM | |
( | |
SELECT DAT,NDX,TBL,IF(px>4,4,px) pw1,IF(py>4,4,py) pw2,IF(pz>4,4,pz) pw3 | |
FROM | |
( | |
SELECT data_length DAT,index_length NDX,data_length+index_length TBL, | |
FLOOR(LOG(IF(data_length=0,1,data_length))/LOG(1024)) px, | |
FLOOR(LOG(IF(index_length=0,1,index_length))/LOG(1024)) py, | |
FLOOR(LOG(data_length+index_length)/LOG(1024)) pz | |
FROM information_schema.tables | |
WHERE table_schema='SCHEMA_NAME' | |
AND table_name='TABLE_NAME' | |
) AA | |
) A,(SELECT 'B KBMBGBTB' units) B; | |
-- data and index size in all schemas | |
SELECT | |
IF(ISNULL(DB)+ISNULL(ENGINE)=2,'Database Total', | |
CONCAT(DB,' ',IFNULL(ENGINE,'Total'))) "Reported Statistic", | |
LPAD(CONCAT(FORMAT(DAT/POWER(1024,pw1),2),' ', | |
SUBSTR(units,pw1*2+1,2)),17,' ') "Data Size", | |
LPAD(CONCAT(FORMAT(NDX/POWER(1024,pw2),2),' ', | |
SUBSTR(units,pw2*2+1,2)),17,' ') "Index Size", | |
LPAD(CONCAT(FORMAT(TBL/POWER(1024,pw3),2),' ', | |
SUBSTR(units,pw3*2+1,2)),17,' ') "Total Size" | |
FROM | |
( | |
SELECT DB,ENGINE,DAT,NDX,TBL, | |
IF(px>4,4,px) pw1,IF(py>4,4,py) pw2,IF(pz>4,4,pz) pw3 | |
FROM | |
(SELECT *, | |
FLOOR(LOG(IF(DAT=0,1,DAT))/LOG(1024)) px, | |
FLOOR(LOG(IF(NDX=0,1,NDX))/LOG(1024)) py, | |
FLOOR(LOG(IF(TBL=0,1,TBL))/LOG(1024)) pz | |
FROM | |
(SELECT | |
DB,ENGINE, | |
SUM(data_length) DAT, | |
SUM(index_length) NDX, | |
SUM(data_length+index_length) TBL | |
FROM | |
( | |
SELECT table_schema DB,ENGINE,data_length,index_length FROM | |
information_schema.tables WHERE table_schema NOT IN | |
('information_schema','performance_schema','mysql') | |
AND ENGINE IS NOT NULL | |
) AAA GROUP BY DB,ENGINE WITH ROLLUP | |
) AAA) AA) A,(SELECT ' BKBMBGBTB' units) B; | |
-- size of each table of a schema | |
SELECT table_name, | |
CONCAT(FORMAT(DAT/POWER(1024,pw1),2),' ',SUBSTR(units,pw1*2+1,2)) DATSIZE, | |
CONCAT(FORMAT(NDX/POWER(1024,pw2),2),' ',SUBSTR(units,pw2*2+1,2)) NDXSIZE, | |
CONCAT(FORMAT(TBL/POWER(1024,pw3),2),' ',SUBSTR(units,pw3*2+1,2)) TBLSIZE | |
FROM | |
( | |
SELECT table_name,DAT,NDX,TBL,IF(px>4,4,px) pw1,IF(py>4,4,py) pw2,IF(pz>4,4,pz) pw3 | |
FROM | |
( | |
SELECT table_name,data_length DAT,index_length NDX,data_length+index_length TBL, | |
FLOOR(LOG(IF(data_length=0,1,data_length))/LOG(1024)) px, | |
FLOOR(LOG(IF(index_length=0,1,index_length))/LOG(1024)) py, | |
FLOOR(LOG(data_length+index_length)/LOG(1024)) pz | |
FROM information_schema.tables | |
WHERE table_schema='SCHEMA_NAME' | |
) AA | |
) A,(SELECT 'B KBMBGBTB' units) B order by A.TBL desc; | |
-- length of a text field in every row of a specific table | |
select id, CONCAT(FORMAT(DAT/POWER(1024,pw1),2),' ',SUBSTR(units,pw1*2+1,2)) 'data_length' from ( | |
select id, DAT, IF(px>4,4,px) pw1 from ( | |
select id, data_length DAT, FLOOR(LOG(IF(data_length=0,1,data_length))/LOG(1024)) px | |
from (select id, length(FIELD) data_length from TABLE_NAME) AAA | |
) AA order by DAT desc | |
) A, (SELECT 'B KBMBGBTB' units) B limit 100; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment