How to get size of a mysql database?
Suppose the target database is called "v3".
10 Answers
Run this query and you'll probably get what you're looking for:
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;
This query comes from the mysql forums, where there are more comprehensive instructions available.
-
4Even after I delete most of the data from the tables in the database, the size remains the same– VidzAug 8, 2014 at 9:05
-
2@Vidz are you using InnoDB engine. If you do, you can free space unless you use file_per_table and alter tables.– mandzaNov 1, 2014 at 9:32
-
5Please keep in mind that this method will not return any of the databases that are completely empty, at least a single table must exist for the database to appear in the result.– v010dyaDec 29, 2014 at 19:39
-
17To select from a single database, add this between the
FROMandGROUPline:where table_schema='DATABASE_NAME'- replacingDATABASE_NAMEwith your database.– KJ PriceNov 9, 2016 at 13:37 -
4Note: MySQL Workbench will spit out a
Syntax error: {column title} (double quoted text) is not valid input here.error. The column titles should be wrapped in tick marks. I.e.Database Name. Jul 17, 2017 at 22:15
It can be determined by using following MySQL command
SELECT table_schema AS "Database", SUM(data_length + index_length) / 1024 / 1024 AS "Size (MB)" FROM information_schema.TABLES GROUP BY table_schema
Result
Database Size (MB)
db1 11.75678253
db2 9.53125000
test 50.78547382
Get result in GB
SELECT table_schema AS "Database", SUM(data_length + index_length) / 1024 / 1024 / 1024 AS "Size (GB)" FROM information_schema.TABLES GROUP BY table_schema
-
Better Performance : SELECT table_schema AS "Database", (SUM(data_length)+SUM(index_length)) / 1024 / 1024 / 1024 AS "Size (GB)" FROM information_schema.TABLES GROUP BY table_schema– sonicliJun 10 at 4:42
Alternatively, if you are using phpMyAdmin, you can take a look at the sum of the table sizes in the footer of your database structure tab. The actual database size may be slightly over this size, however it appears to be consistent with the table_schema method mentioned above.
Screen-shot :
Alternatively you can directly jump into data directory and check for combined size of v3.myd, v3. myi and v3. frm files (for myisam) or v3.idb & v3.frm (for innodb).
-
7
-
2This answer is very specific to storage engine. Answer by @brian-willis is more appropriate. Jun 23, 2017 at 9:03
-
This will not work if you don't have access (using a cloud service), for example, you are using AWS RDS... Apr 8 at 14:38
To get a result in MB:
SELECT
SUM(ROUND(((DATA_LENGTH + INDEX_LENGTH) / 1024 / 1024), 2)) AS "SIZE IN MB"
FROM INFORMATION_SCHEMA.TABLES
WHERE
TABLE_SCHEMA = "SCHEMA-NAME";
To get a result in GB:
SELECT
SUM(ROUND(((DATA_LENGTH + INDEX_LENGTH) / 1024 / 1024 / 1024), 2)) AS "SIZE IN GB"
FROM INFORMATION_SCHEMA.TABLES
WHERE
TABLE_SCHEMA = "SCHEMA-NAME";
mysqldiskusage --server=root:MyPassword@localhost pics
+----------+----------------+
| db_name | total |
+----------+----------------+
| pics | 1,179,131,029 |
+----------+----------------+
If not installed, this can be installed by installing the mysql-utils package which should be packaged by most major distributions.
-
2
-
@angristan - Thanks. I think Oracle is tossing the package. Even an old copy of the utilities will be useful in most situations. Jul 27, 2020 at 16:19
-
1CAUTION:
mysqldiskusagerequires use of unencripted password in command line. Make sure to delete it from history after use. Nov 3, 2020 at 16:51 -
In some shells, putting a space in front of the command avoids saving the line in 'history'. Nov 3, 2020 at 17:35
First login to MySQL using
mysql -u username -p
Command to Display the size of a single Database along with its table in MB.
SELECT table_name AS "Table",
ROUND(((data_length + index_length) / 1024 / 1024), 2) AS "Size (MB)"
FROM information_schema.TABLES
WHERE table_schema = "database_name"
ORDER BY (data_length + index_length) DESC;
Change database_name to your Database
Command to Display all the Databases with its size in MB.
SELECT table_schema AS "Database",
ROUND(SUM(data_length + index_length) / 1024 / 1024, 2) AS "Size (MB)"
FROM information_schema.TABLES
GROUP BY table_schema;
If you want the list of all database sizes sorted, you can use :
SELECT *
FROM (SELECT table_schema AS `DB Name`,
ROUND(SUM(data_length + index_length) / 1024 / 1024, 1) AS `DB Size in MB`
FROM information_schema.tables
GROUP BY `DB Name`) AS tmp_table
ORDER BY `DB Size in MB` DESC;
Go into the mysql data directory and run du -h --max-depth=1 | grep databasename
-
5ok. but for cloud database servers like RDS, GCP we don't have access to server file system.– AkhilSep 21, 2021 at 6:12
-
The file size does not reflect the real database size. In fact, after deleting entries from a table, the file is not shrunk; instead, it contains unallocated space that the engine will reuse by the next occasion.– ApuleiusApr 9 at 17:18
-
In addition: If someone wants to get the size of a single table please use the following codes:
SELECT
TABLE_NAME AS `Table Name`,
ROUND((DATA_LENGTH + INDEX_LENGTH) / 1024 / 1024) AS `Size ( in MB)`
FROM
information_schema.TABLES
WHERE
TABLE_SCHEMA = "your_db_name"
AND
TABLE_NAME = "your_single_table_name"
ORDER BY
(DATA_LENGTH + INDEX_LENGTH)
DESC;
Note: It won't show the fraction numbers for using the ROUND() method.
Hope this will help many of us.
-
1Hi, the question is about how to calculate the whole size of a db, not a single table.. Please edit your answer in order to reach that result, or explain what your answer is doing more than the accepted answer. Cheers– funder7Jun 30 at 11:36
