How to get size of mysql database?
Asked Answered
C

11

814

How to get size of a mysql database?
Suppose the target database is called "v3".

Clam answered 14/11, 2009 at 6:40 Comment(0)
P
1732

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.

Phelan answered 14/11, 2009 at 6:46 Comment(11)
this select don't show me all . I tried to check OTRS database and tables with attachment (probably some BLOB field) appear don't calcule correctly... any workaroud?Gaylegayleen
Even after I delete most of the data from the tables in the database, the size remains the sameAnnalee
@Annalee are you using InnoDB engine. If you do, you can free space unless you use file_per_table and alter tables.Kiki
Please 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.Euratom
I found that the statement failed in MySQL (v5.7) without the "as" before the "DB Name" and "DB Size in MB"Wedge
To select from a single database, add this between the FROM and GROUP line: where table_schema='DATABASE_NAME' - replacing DATABASE_NAME with your database.Defense
Note: 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.Sixgun
Add this at the end if you want to filter by size ORDER BY ROUND(SUM(data_length + index_length) / 1024 / 1024, 1) DESCSvelte
Works perfectly as of May 17th, 2022 - executed in DBeaver on MacOS 12.3.1.Ozone
Why not use "format_bytes"? format_bytes(sum(data_length + index_length))Handedness
For InnoDB, DATA_LENGTH is the approximate amount of space allocated for the clustered index, in bytes. Specifically, it is the clustered index size, in pages, multiplied by the InnoDB page size.Oxygen
B
205

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
Beale answered 4/12, 2017 at 8:50 Comment(1)
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_schemaCyprio
G
44

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 :

enter image description here

Giffin answered 29/3, 2013 at 2:28 Comment(0)
E
31

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";
Emaemaciate answered 11/9, 2017 at 11:48 Comment(0)
G
26

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).

Gromyko answered 14/11, 2009 at 10:12 Comment(3)
Note: ibd files only exist if using innodb_file_per_tableBing
This answer is very specific to storage engine. Answer by @brian-willis is more appropriate.Parisi
This will not work if you don't have access (using a cloud service), for example, you are using AWS RDS...Schaffer
C
24

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; 
Cuellar answered 4/8, 2020 at 21:0 Comment(0)
C
20
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.

Update

Alas, they got rid of that package. If you are on Linux (or similar),

du -m /var/lib/mysql/*

will list the size, in megabytes, for each database you have. (Caveats: You probably need to be root to run the command, and the path may not be what I provided there.) This lists the 20 biggest:

du -m /var/lib/mysql/* | sort -nb | tail
Caesar answered 29/11, 2017 at 20:16 Comment(7)
On Debian 10 this package is called mysql-utilitiesBascomb
@angristan - Thanks. I think Oracle is tossing the package. Even an old copy of the utilities will be useful in most situations.Caesar
CAUTION: mysqldiskusage requires use of unencripted password in command line. Make sure to delete it from history after use.Blankenship
In some shells, putting a space in front of the command avoids saving the line in 'history'.Caesar
@Bascomb mysql-utilities package does not exist anymore on Debian 11. Where can we get the mysqldiskusage command then?Korwun
@Korwun - Alas. So, I added an alternative.Caesar
@RickJames I already saw the du solution in another answer but I was wondering if we can get the mysqldiskusage command somewhere else.Korwun
B
19

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;
Bidarka answered 19/11, 2019 at 7:1 Comment(0)
P
6

Go into the mysql data directory and run du -h --max-depth=1 | grep databasename

Prologue answered 13/6, 2018 at 20:19 Comment(4)
ok. but for cloud database servers like RDS, GCP we don't have access to server file system.Dogvane
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.Emmeram
On many shared hostings this is exactly what they use for calculation.Tomchay
It looks like we don't need to use grep and we don't need the --max-depth=1 parameter. In my case the result was the same with the command du -h databasename.Korwun
C
6

To get the list of all the databases sorted by their size in descending order without using any sub-query, you can use the below query:

SELECT 
    table_schema AS Database_Name,
    SUM(data_length + index_length) Size_In_Bytes,
    ROUND(SUM(data_length + index_length) / 1024 / 1024, 2) Size_In_MB,
    ROUND(SUM(data_length + index_length) / 1024 / 1024/ 1024, 2) Size_In_GB
FROM information_schema.tables 
GROUP BY table_schema ORDER BY Size_In_Bytes DESC;

You will get the size of all the databases in Bytes, MB and GB as shown in the example below:

enter image description here

As asked by OP, to get the size on any particular database like "v3" WHERE can be used in the query as given below:

SELECT 
    table_schema AS Database_Name,
    SUM(data_length + index_length) Size_In_Bytes,
    ROUND(SUM(data_length + index_length) / 1024 / 1024, 2) Size_In_MB,
    ROUND(SUM(data_length + index_length) / 1024 / 1024/ 1024, 2) Size_In_GB
FROM information_schema.tables WHERE table_schema = 'v3'
GROUP BY table_schema ORDER BY Size_In_Bytes DESC;
Cornejo answered 12/1 at 8:48 Comment(0)
K
2

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.

Khanna answered 28/6, 2022 at 11:42 Comment(1)
Hi, 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. CheersVasyuta

© 2022 - 2024 — McMap. All rights reserved.