154

I would like to know how much space does my MySQL database use, in order to select a web host. I found the command SHOW TABLE STATUS LIKE 'table_name' so when I do the query, I get something like this:

Name       | Rows | Avg. Row Length | Data_Length | Index Length
----------   ----   ---------------   -----------   ------------
table_name   400          55            362000        66560
  • numbers are rounded.

So do I have 362000 or 400*362000 = 144800000 bytes of data for this table? And what does Index Length mean? Thanks !

marvin
  • 1,847
  • 4
  • 15
  • 20

10 Answers10

301

From S. Prakash, found at the MySQL forum:

SELECT table_schema "database name",
    sum( data_length + index_length ) / 1024 / 1024 "database size in MB",
    sum( data_free )/ 1024 / 1024 "free space in MB"
FROM information_schema.TABLES
GROUP BY table_schema; 

Or in a single line for easier copy-pasting:

SELECT table_schema "database name", sum( data_length + index_length ) / 1024 / 1024 "database size in MB", sum( data_free )/ 1024 / 1024 "free space in MB" FROM information_schema.TABLES GROUP BY table_schema; 
sjas
  • 18,644
  • 14
  • 87
  • 92
ron_dobley
  • 3,029
  • 1
  • 14
  • 2
  • 7
    What does the Free Space in MB means ? I have 100s of GB free but it reports less than 1 GB free. – Barth Feb 26 '15 at 10:57
  • 1
    @Barth as per the [documentation](https://dev.mysql.com/doc/refman/5.7/en/tables-table.html "The INFORMATION_SCHEMA TABLES Table") it means "free space in bytes for InnoDB tables". Unfortunately there's no explanation what *that* means :-\ – ckujau May 04 '15 at 19:08
  • 15
    Free space in bytes means exactly that - it means that amount of space could be reclaimed if you OPTIMIZE the table or rebuild the table from scratch. When databases store information on disks, they allocate it in blocks; deleting a record often frees a block between used blocks, and it's very expensive to shift all of the other blocks around to use that space up so it is marked as "free". The space may be used later by the database engine itself, or can be reclaimed using `OPTIMIZE TABLE foo` (MyISAM), or recreate + analyze for InnoDB tables. – razzed Dec 05 '15 at 16:35
97

You can get the size of your Mysql database by running the following command in Mysql client

SELECT  sum(round(((data_length + index_length) / 1024 / 1024 / 1024), 2))  as "Size in GB"
FROM information_schema.TABLES
WHERE table_schema = "<database_name>"
Jin Kwon
  • 20,295
  • 14
  • 115
  • 184
minhas23
  • 9,291
  • 3
  • 58
  • 40
33

If you use phpMyAdmin, it can tell you this information.

Just go to "Databases" (menu on top) and click "Enable Statistics".

You will see something like this:

phpMyAdmin screenshot

This will probably lose some accuracy as the sizes go up, but it should be accurate enough for your purposes.

Radu Murzea
  • 10,724
  • 10
  • 47
  • 69
  • I am currently using 000webhost. It has phpMyAdmin but I couldn't find an "enable stats" checkbox. Is there (maybe) a query for that? – marvin Feb 05 '13 at 19:10
  • 1
    @marvin Well, it's possible that the webhost has an older version of phpMyAdmin installed. I don't know in which version this feature was introduced. – Radu Murzea Feb 05 '13 at 19:52
  • In your sidebar, click the Home icon at the top which takes you to the home page. There in the right window, click 'Databases'. It displays a one column table of databases, below that is a link called 'Enable Statistics' – Whip Jul 28 '16 at 11:49
  • I see a Note: Enabling the database statistics here might cause heavy traffic between the web server and the MySQL server. Enabling stats may impact performance of your applications. I would rather run the query suggested by minhas23 – ryadavalli Jun 07 '22 at 01:53
11

if you want to find it in MB do this

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; 
jakson
  • 255
  • 3
  • 10
4

Basically there are two ways: query DB (data length + index length) or check files size. Index length is related to data stored in indexes.

Everything is described here:

http://www.mkyong.com/mysql/how-to-calculate-the-mysql-database-size/

Anders
  • 8,307
  • 9
  • 56
  • 88
MiGro
  • 1,471
  • 10
  • 8
  • Well, this page is the source of my question. But according to this query, I have 0.5 MB, which leaves me pretty safe. I hope it is true :) Thanks ! – marvin Feb 05 '13 at 19:14
2

If you want to find the size of all MySQL databases, us this command, it will show their respective sizes in megabytes;

SELECT table_schema "database", sum(data_length + index_length)/1024/1024 "size in MB" FROM information_schema.TABLES GROUP BY table_schema;

If you have large databases, you can use the following command to show the result in gigabytes;

SELECT table_schema "database", sum(data_length + index_length)/1024/1024/1024 "size in GB" FROM information_schema.TABLES GROUP BY table_schema;

If you want to show the size of only a specific database, for example YOUR_DATABASE_NAME, you could use the following query;

SELECT table_schema "database", sum(data_length + index_length)/1024/1024/1024 "size in GB" FROM information_schema.TABLES WHERE table_schema='YOUR_DATABASE_NAME' GROUP BY table_schema;
Arbab Nazar
  • 22,378
  • 10
  • 76
  • 82
1

None of the answers include the overhead size and the metadata sizes of tables.

Here is a more accurate estimation of the "disk space" allocated by a database.

SELECT ROUND((SUM(data_length+index_length+data_free) + (COUNT(*) * 300 * 1024))/1048576+150, 2) AS MegaBytes FROM information_schema.TABLES WHERE table_schema = 'DATABASE-NAME'
Semra
  • 2,787
  • 28
  • 26
0

SUM(Data_free) may or may not be valid. It depends on the history of innodb_file_per_table. More discussion is found here.

Community
  • 1
  • 1
Rick James
  • 135,179
  • 13
  • 127
  • 222
0

MySQL Utilities by Oracle have a command called mysqldiskusage that displays the disk usage of every database: https://dev.mysql.com/doc/mysql-utilities/1.6/en/mysqldiskusage.html

ofrommel
  • 2,129
  • 14
  • 19
0

If you are using MySql Workbench, its very easy to get all details of Database size, each table size, index size etc.

  1. Right Click on Schema
  2. Select Schema Inspector option

    enter image description here

  3. It Shows all details of Schema size

  4. Select Tables Tab to see size of each table.

    enter image description here

  5. Table size diplayed in Data Lenght column

Viraj Dhamal
  • 5,165
  • 10
  • 32
  • 41