Database Size

What Makes Up The Size of a Database?

The total size of a database is the sum of the sizes of its tables.

The table’s size is not only the size of the rows that can be seen in the table, but it also includes table’s additional functions (different types of keys and indexes) and a unused space left as a result of requests and actions.

The size of the database table consists of three parts:

  • data
  • indexes
  • overhead

The data for each data type is stored on the database server in files that take up free disk space on the server.

Attention!

Even if the count of the rows, size of the data in the table is small or the table is completely empty, it may still have unused space called overhead due to previous actions and thus the files of the table containing this unused space will still take up space on the database server’s disk.

How To View The Size of a Database And Its Tables?

The size of the database and its tables can be viewed via various client programs and phpMyAdmin or directly from the SSH terminal.

We will use phpMyAdmin for this here.

After logging into phpMyAdmin, select the required database from the left column for which all the tables are displayed on the first Structure tab and at the very end, on the last line of the table, the total size of the tables is displayed. However, this does not include the overhead of the tables:

phpMyAdmin - Database Tables

In order to see the actual size of the table (data + indexes + overhead), you need to go to the Structure tab of the table and look for the Space usage table in the Information subsection. The Effective line in it is the actual size of the table on the database server:

phpMyAdmin - Structure of the table

phpMyAdmin - Space usage of the table

What Is Table’s Overhead?

Overhead is the unreleased space of a MySQL / MariaDB database table that is allocated to the table, but not in use.

This can be seen inside of the table information_schema.TABLES in the Data_free column. Size is displayed in bytes.

Data_free is caused by unused and fragmented space due to deletion of rows from the table (DELETE), updating of rows (UPDATE) with larger values, reduction and rearrangement of table space, reservation of space for future queries (INSERT, UPDATE).

Data_free

Although the Data_free space is not in use and is not directly reflected in the database size, it still physically uses disk space on the database server as a database table file.

How To Find Tables With Large Overhead?

If database has a large number of tables and you don’t know which of them might have a large overhead, instead of looking at the Structure tab for each table, a quicker way to find the problematic table, is to use the following SQL query.

To make an SQL query in phpMyAdmin, you need to go to the SQL tab, write or paste the query into the text box and then press the Go button to make the query.

SHOW TABLE STATUS WHERE `Data_free` > 0;

The Data_free column of the query result table shows the overhead of the table, the sizes are displayed in bytes:

phpMyAdmin - SQL query: SHOW TABLE STATUS

A more detailed SQL query that shows the sizes in megabytes of each of the three data types separately and the total actual size of the table (data + indexes + overhead).
Replace [DATABASE_NAME] with the actual database name in the command:

SELECT `TABLE_SCHEMA` AS "Database", `TABLE_NAME` AS "Table", `ENGINE`, `ROW_FORMAT`, `TABLE_ROWS`, ROUND(((`DATA_LENGTH`) / 1024 / 1024), 2) AS "Data Size (MiB)", ROUND(((`INDEX_LENGTH`) / 1024 / 1024), 2) AS "Index Size (MiB)", ROUND(((`DATA_FREE`) / 1024 / 1024), 2) AS "Overhead (MiB)", ROUND(((`DATA_LENGTH` + `INDEX_LENGTH` + `DATA_FREE`) / 1024 / 1024), 2) AS "Total Size (MiB)" FROM `information_schema`.`TABLES` WHERE `TABLE_SCHEMA` = '[DATABASE_NAME]' AND `DATA_FREE` > 0 ORDER BY `DATA_FREE` DESC;

phpMyAdmin - SQL query: SELECT all sizes and total size results

It can be seen that the size of this WordPress’ options table is 113.3 MiB, but under the unreleased space is stuck 8337 MiB or 8.1 GiB – that’s more than 70 times the size of the table’s data!

phpMyAdmin - SQL query: SELECT al sizesl and total size

Freeing Up Unused Space – Optimizing The Table

In order to free up the unused space of the table, an optimization must be performed on the table.

To do the optimization of a table, there is a link named Optimize table in the table Space usage on the Structure tab’s page and also on the Operations tab’s page.

Alternatively a SQL query can be made for optimizing a table (replace [DATABASE_NAME] and [TABLE_NAME] in the command with the name of the database and table):

OPTIMIZE TABLE `[DATABASE_NAME]`.`[TABLE_NAME]`;
Attention!

For very large tables, optimization can take a long time, during which the table may be locked and therefore unusable by the application.
It is therefore recommended that optimization is not performed during peak usage.

Read more in the documentation of the corresponding database:
MariaDB: OPTIMIZE TABLE
MySQL: OPTIMIZE TABLE Statement

 

Updated on 14. Nov 2024
Was this article helpful?

Related Articles