How to measure the data size (usage) of single tables or databases in MariaDB or MySQL

Written by - 1 comments

Published on - last updated on February 6th 2023 - Listed in Database MySQL MariaDB


One cannot simply run the du command on the MySQL/MariaDB table files to measure the size of a database or table.

A much better and correct way is to get the information from the information_schema table:

MariaDB [(none)]> SELECT
    ->      table_schema as `Database`,
    ->      table_name AS `Table`,
    ->      round(((data_length + index_length) / 1024 / 1024), 2) `Size in MB`
    -> FROM information_schema.TABLES
    -> ORDER BY (data_length + index_length) DESC LIMIT 0,10;

+----------+-----------------------------+------------+
| Database | Table                       | Size in MB |
+----------+-----------------------------+------------+
| shopware | s_core_snippets             |       5.03 |
| test1    | log_request                 |       4.63 |
| shopware | saferpaycw_transaction      |       4.53 |
| test1    | log_request_parameter       |       3.03 |
| shopware | s_core_sessions_backend     |       2.47 |
| shopware | s_articles_similar_shown_ro |       2.39 |
| test1    | object_container            |       1.59 |
| shopware | s_core_log                  |       1.52 |
| shopware | s_emarketing_lastarticles   |       1.38 |
| shopware | s_core_sessions             |       1.20 |
+----------+-----------------------------+------------+
10 rows in set (0.00 sec)

The query above lists the 10 largest tables over all databases on this MariaDB server.

And as a one-liner:

MariaDB [(none)]> SELECT table_schema as `Database`, table_name AS `Table`, round(((data_length + index_length) / 1024 / 1024), 2) `Size in MB` FROM information_schema.TABLES ORDER BY (data_length + index_length) DESC LIMIT 0,10;

This handy information was found in a Stackoverflow answer.

Back at the beginning I mentioned the du command. What does du think about the size of the largest table:

# du -ksh /var/lib/mysql/shopware/s_core_snippets.*
4.0K    /var/lib/mysql/shopware/s_core_snippets.frm
13M    /var/lib/mysql/shopware/s_core_snippets.ibd

Here we go, that's quite a difference (13MB vs. 5.03 MB).

To measure the size of a full database, you can use the following SQL one-liner query:

MariaDB [(none)]> SELECT table_schema "DB Name", Round(Sum(data_length + index_length) / 1024 / 1024, 1) "DB Size in MB" FROM information_schema.tables GROUP BY table_schema ORDER BY `DB Size in MB` DESC;
+--------------------+---------------+
| DB Name            | DB Size in MB |
+--------------------+---------------+
| prod1              |       42735.3 |
| logs_22            |         318.5 |
| shopware           |         243.6 |
| robbot             |          93.1 |
| shopcenter24       |          37.2 |
| information_schema |           0.2 |
| performance_schema |           0.0 |
| nmonitoring        |           0.0 |
+--------------------+---------------+
8 rows in set (0.568 sec)

This lists all databases on this MySQL/MariaDB server with its data size.


Add a comment

Show form to leave a comment

Comments (newest first)

sutekin from Türkiye wrote on Jul 29th, 2022:

du and sql query sizes are different, because sql shows current active data length, on the other hand du only shows file size. ibd file includes data, indexes and vacuum size. When you delete a line from innodb dataspace is just cleared but not removed from ibd file. If you add a billion and one lines and delete a billion lines from database ibd file will show you same size until this table size exceeds a billion and one line size. For recovering size of vacuum you should optimize table(in innodb it just alters tables and recreates index; but you shouldn't do it), or if you want to delete all lines, you should truncate table. It also delete and recreates table.


RSS feed

Blog Tags:

  AWS   Android   Ansible   Apache   Apple   Atlassian   BSD   Backup   Bash   Bluecoat   CMS   Chef   Cloud   Coding   Consul   Containers   CouchDB   DB   DNS   Database   Databases   Docker   ELK   Elasticsearch   Filebeat   FreeBSD   Galera   Git   GlusterFS   Grafana   Graphics   HAProxy   HTML   Hacks   Hardware   Icinga   Influx   Internet   Java   KVM   Kibana   Kodi   Kubernetes   LVM   LXC   Linux   Logstash   Mac   Macintosh   Mail   MariaDB   Minio   MongoDB   Monitoring   Multimedia   MySQL   NFS   Nagios   Network   Nginx   OSSEC   OTRS   Office   PGSQL   PHP   Perl   Personal   PostgreSQL   Postgres   PowerDNS   Proxmox   Proxy   Python   Rancher   Rant   Redis   Roundcube   SSL   Samba   Seafile   Security   Shell   SmartOS   Solaris   Surveillance   Systemd   TLS   Tomcat   Ubuntu   Unix   VMWare   VMware   Varnish   Virtualization   Windows   Wireless   Wordpress   Wyse   ZFS   Zoneminder