How to Get True Size of MySQL Database?

前端 未结 10 779
南笙
南笙 2020-12-02 04:35

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

相关标签:
10条回答
  • 2020-12-02 04:36

    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; 
    
    0 讨论(0)
  • 2020-12-02 04:36

    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.

    0 讨论(0)
  • 2020-12-02 04:36

    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'
    
    0 讨论(0)
  • 2020-12-02 04:38

    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/

    0 讨论(0)
  • 2020-12-02 04:39

    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;
    
    0 讨论(0)
  • 2020-12-02 04:39

    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

    0 讨论(0)
提交回复
热议问题