Researching your MySQL table sizes
I posted a simple INFORMATION_SCHEMA query to find largest tables last month and it got a good response. Today I needed little modifications to that query to look into few more aspects of data sizes so here it goes:
Find total number of tables, rows, total data in index size for given MySQL Instance
-
mysql> SELECT count(*) TABLES,
-
-> concat(round(sum(table_rows)/1000000,2),'M') rows,
-
-> concat(round(sum(data_length)/(1024*1024*1024),2),'G') DATA,
-
-> concat(round(sum(index_length)/(1024*1024*1024),2),'G') idx,
-
-> concat(round(sum(data_length+index_length)/(1024*1024*1024),2),'G') total_size,
-
-> round(sum(index_length)/sum(data_length),2) idxfrac
-
-> FROM information_schema.TABLES;
-
+--------+----------+---------+--------+------------+---------+
-
| TABLES | rows | DATA | idx | total_size | idxfrac |
-
+--------+----------+---------+--------+------------+---------+
-
| 1538 | 1623.91M | 314.00G | 36.86G | 350.85G | 0.12 |
-
+--------+----------+---------+--------+------------+---------+
-
1 row IN SET (52.56 sec)
Find the same data using some filter
I often use similar queries to find space used by particular table "type" in sharded environment when multiple tables with same structure and similar name exists:
-
mysql> SELECT count(*) TABLES,
-
-> concat(round(sum(table_rows)/1000000,2),'M') rows,
-
-> concat(round(sum(data_length)/(1024*1024*1024),2),'G') DATA,
-
-> concat(round(sum(index_length)/(1024*1024*1024),2),'G') idx,
-
-> concat(round(sum(data_length+index_length)/(1024*1024*1024),2),'G') total_size,
-
-> round(sum(index_length)/sum(data_length),2) idxfrac
-
-> FROM information_schema.TABLES
-
-> WHERE table_name LIKE "%performance_log%";
-
+--------+---------+---------+-------+------------+---------+
-
| TABLES | rows | DATA | idx | total_size | idxfrac |
-
+--------+---------+---------+-------+------------+---------+
-
| 120 | 370.29M | 163.97G | 0.00G | 163.97G | 0.00 |
-
+--------+---------+---------+-------+------------+---------+
-
1 row IN SET (0.03 sec)
Find biggest databases
-
mysql> SELECT
-
-> count(*) TABLES,
-
-> table_schema,concat(round(sum(table_rows)/1000000,2),'M') rows,
-
-> concat(round(sum(data_length)/(1024*1024*1024),2),'G') DATA,
-
-> concat(round(sum(index_length)/(1024*1024*1024),2),'G') idx,
-
-> concat(round(sum(data_length+index_length)/(1024*1024*1024),2),'G') total_size,
-
-> round(sum(index_length)/sum(data_length),2) idxfrac
-
-> FROM information_schema.TABLES
-
-> GROUP BY table_schema
-
-> ORDER BY sum(data_length+index_length) DESC LIMIT 10;
-
+--------+--------------------+-------+-------+-------+------------+---------+
-
| TABLES | table_schema | rows | DATA | idx | total_size | idxfrac |
-
+--------+--------------------+-------+-------+-------+------------+---------+
-
| 48 | cacti | 0.01M | 0.00G | 0.00G | 0.00G | 0.72 |
-
| 17 | mysql | 0.00M | 0.00G | 0.00G | 0.00G | 0.18 |
-
| 4 | pdns | 0.00M | 0.00G | 0.00G | 0.00G | 1.00 |
-
| 2 | test | 0.00M | 0.00G | 0.00G | 0.00G | 0.12 |
-
| 16 | information_schema | NULL | 0.00G | 0.00G | 0.00G | NULL |
-
+--------+--------------------+-------+-------+-------+------------+---------+
-
5 rows IN SET (0.32 sec)
Data Distribution by Storage Engines
You can change this query a bit and get most popular storage engines by number of tables or number of rows instead of data stored.
-
mysql> SELECT engine,
-
-> count(*) TABLES,
-
-> concat(round(sum(table_rows)/1000000,2),'M') rows,
-
-> concat(round(sum(data_length)/(1024*1024*1024),2),'G') DATA,
-
-> concat(round(sum(index_length)/(1024*1024*1024),2),'G') idx,
-
-> concat(round(sum(data_length+index_length)/(1024*1024*1024),2),'G') total_size,
-
-> round(sum(index_length)/sum(data_length),2) idxfrac
-
-> FROM information_schema.TABLES
-
-> GROUP BY engine
-
-> ORDER BY sum(data_length+index_length) DESC LIMIT 10;
-
+------------+--------+---------+---------+--------+------------+---------+
-
| engine | TABLES | rows | DATA | idx | total_size | idxfrac |
-
+------------+--------+---------+---------+--------+------------+---------+
-
| MyISAM | 1243 | 941.06M | 244.09G | 4.37G | 248.47G | 0.02 |
-
| InnoDB | 280 | 682.82M | 63.91G | 32.49G | 96.40G | 0.51 |
-
| MRG_MyISAM | 1 | 13.66M | 6.01G | 0.00G | 6.01G | 0.00 |
-
| MEMORY | 14 | 0.00M | 0.00G | 0.00G | 0.00G | NULL |
-
+------------+--------+---------+---------+--------+------------+---------+
-
4 rows IN SET (14.02 sec)
Trivially but handy.
11 Comments
Trackbacks/Pingbacks
- Propiedad Privada » Blog Archive » Consultas MySQL útiles
[...] Via MySQL Performance Blog [...] - Intenta » Blog Archive » Consultas útiles para MySQL
[...] MySQL Performance blog he encontrado algunas consultas MySQL muy útiles a la hora de controlar el tamaño y [...] - Tamaño de tablas en MySQL
[...] Vía: MySQL Performance Blog [...] - daro » Blog Archive » Consultas útiles para MySQL
[...] MySQL Performance blog he encontrado algunas consultas MySQL muy útiles a la hora de controlar el tamaño y [...] - Consultas útiles para MySQL
[...] el artículo Researching your mysql table sizes se explica como controlar el tamaño de nuestras tablas e indices, son consultas bastante útiles [...] - Zeck’s Blog » Archive du blog » MySQL, quelques requêtes pour l’administration…
[...] Researching your MySQL table sizes [...] - The ArcterJournal
Finding MySQL Table Disk Usage Via SQL... A little snippet that's a deep-geek note-to-self. Darren found this from modifying information on this site and I figured I wanted to keep it around as it looks potentially handy to have. Also once google indexes things I'll be able...... - How Percona does a MySQL Performance Audit | MySQL Performance Blog
[...] the server and see what I think about it. If the server is not heavily loaded, I may even do some INFORMATION_SCHEMA queries to help me find the biggest tables and so forth. If the server is heavily loaded or has a lot of data, touching the INFORMATION_SCHEMA [...] - How Percona does a MySQL Performance Audit | Reflexiones IT
[...] the server and see what I think about it. If the server is not heavily loaded, I may even do some INFORMATION_SCHEMA queries to help me find the biggest tables and so forth. If the server is heavily loaded or has a lot of data, touching the INFORMATION_SCHEMA [...] - Pereyrada » Consultas útiles en SQL
[...] Web] MySQL Performance Blog Saludos A. Daniel [...] - My daily readings 07/10/2009 « Strange Kite
[...] Researching your MySQL table sizes | MySQL Performance Blog [...]











del.icio.us
digg
Some very handy queries here, Peter. Thank you. This is going to my bookmarks.
Comment :: March 17, 2008 @ 11:06 pm
These have defiantly gone into my snippets list, thanks.
Also, BTW…
Your syntax highlighter has a spelling error. When you switch to ‘PLAIN TEXT’, the option to switch back to the highlighted version is tagged ‘HILITED HTML’ – ought to be ‘HIGHLIGHTED HTML’.
Comment :: March 18, 2008 @ 5:09 am
ryan, that’s just an American slang version of the word, not really a misspelling.
Comment :: March 18, 2008 @ 7:18 am
Hum, learned two things today then
Comment :: March 18, 2008 @ 1:36 pm
Anybody can let me know how can i get the total number of tables in database without using a SELECT query but by using a stored procedure?
Comment :: March 27, 2008 @ 11:19 pm
Well what is the problem with select query ?
You can wrap it around by stored procedure if you need to.
Comment :: March 28, 2008 @ 2:48 pm
Could you remove all the “->” in order to allow people copy-paste right from here?
Comment :: September 12, 2008 @ 8:20 am
Why does SUM(table_rows) fluctuate so wildly? I run this query:
SELECT CONCAT(SUM(table_rows)/1000000,’M') AS no_rows FROM information_schema.TABLES WHERE TABLE_SCHEMA = ”;
and no_rows was first 2.73M, then 2.83M, then 2.81M, then 2.78M, then 2.81M…?
Why does this vary?
Comment :: November 6, 2008 @ 10:05 am
It’s a restriction of InnoDB. See http://dev.mysql.com/doc/refman/5.0/en/innodb-restrictions.html. It gives an approximate count, which fluctuates a lot.
Comment :: November 6, 2008 @ 10:37 am
This is really cool. Thanks.
Comment :: January 28, 2009 @ 9:14 am
Another vote for putting the queries on one line for copy and pasting.
Comment :: July 24, 2009 @ 2:56 pm