697

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

Sandeep
  • 1,510
  • 7
  • 21
  • 32
Newbie
  • 7,255
  • 4
  • 19
  • 13
  • For specific table / specific database size the script provided here will help, the information is calculated from information_schema.tables table, see the detailed answer here http://www.rathishkumar.in/2017/12/how-to-find-database-and-table-size-in-mysql.html – Rathish Kumar B Dec 29 '17 at 12:42

9 Answers9

1519

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.

rationalboss
  • 5,292
  • 3
  • 29
  • 48
Brian Willis
  • 21,082
  • 9
  • 45
  • 50
  • 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? – ceinmart Oct 08 '13 at 14:36
  • 4
    Even after I delete most of the data from the tables in the database, the size remains the same – Vidz Aug 08 '14 at 09:05
  • 2
    @Vidz are you using InnoDB engine. If you do, you can free space unless you use file_per_table and alter tables. – mandza Nov 01 '14 at 09:32
  • 5
    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. – v010dya Dec 29 '14 at 19:39
  • I found that the statement failed in MySQL (v5.7) without the "as" before the "DB Name" and "DB Size in MB" – thebtm May 05 '16 at 18:07
  • 17
    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. – KJ Price Nov 09 '16 at 13:37
  • 4
    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`. – KareemElashmawy Jul 17 '17 at 22:15
  • Add this at the end if you want to filter by size `ORDER BY ROUND(SUM(data_length + index_length) / 1024 / 1024, 1) DESC` – Dexter May 13 '19 at 05:28
  • Works perfectly as of May 17th, 2022 - executed in DBeaver on MacOS 12.3.1. – Tom Hood May 17 '22 at 17:16
148

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
Nadeem0035
  • 3,267
  • 1
  • 22
  • 35
36

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

Sumon Sarker
  • 2,526
  • 1
  • 22
  • 33
Joel
  • 2,547
  • 6
  • 30
  • 44
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).

24

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";
GreenRaccoon23
  • 3,333
  • 6
  • 28
  • 45
williambarau
  • 441
  • 4
  • 4
18
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.

Evan Carroll
  • 71,692
  • 44
  • 234
  • 400
Rick James
  • 122,779
  • 10
  • 116
  • 195
16

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;
Hiren Parghi
  • 1,775
  • 1
  • 19
  • 29
7

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; 
4

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

baikho
  • 4,937
  • 4
  • 42
  • 46
Evan Haston
  • 65
  • 1
  • 6
  • 4
    ok. but for cloud database servers like RDS, GCP we don't have access to server file system. – Akhil Sep 21 '21 at 06: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. – Apuleius Apr 09 '22 at 17:18