{"id":81,"date":"2012-11-30T16:39:12","date_gmt":"2012-11-30T21:39:12","guid":{"rendered":"http:\/\/en.latindevelopers.com\/ivancp\/?p=81"},"modified":"2012-11-30T16:51:08","modified_gmt":"2012-11-30T21:51:08","slug":"mysql-get-disk-usage-of-all-databases","status":"publish","type":"post","link":"http:\/\/en.latindevelopers.com\/ivancp\/2012\/mysql-get-disk-usage-of-all-databases\/","title":{"rendered":"MySQL get disk usage of all databases"},"content":{"rendered":"<p>I was tired to get manually disk space used for all MySQL databases, I just created a stored procedure to get an overview of the database sizes in our MySQL server.<\/p>\n<p>MySQL don&#8217;t have a command that allows us an overall summary of the databases, something like SHOW TABLE STATUS for databases. The &#8220;SHOW DATABASES&#8221; command lists only the current databases without any other information like how many space are using or how many tables there are, etc.<\/p>\n<p>The procedure that I wrote this based on INFORMATION_SCHEMA database, which contains rich information of all existing databases.<\/p>\n<p>I suggest, in a previous post, place all these procedures in a database called tools, but you can still choose different location.<\/p>\n<p>So when call the procedure will have a global view of information that will be useful for maintenance tasks.<\/p>\n<p>&nbsp;<\/p>\n<pre lang=\"text\">mysql> call tools.sp_overview();\r\n+------------------------------+---------+--------+----------+---------+\r\n| Database                     | Charset | Tables | Routines | Size Mb |\r\n+------------------------------+---------+--------+----------+---------+\r\n| database1                    | utf8    |     43 |       28 |     7.0 |\r\n| database2                    | latin1  |     43 |       28 |   205.0 |\r\n| database3                    | utf8    |    116 |        0 |   126.2 |\r\n| database4                    | utf8    |     99 |        0 |     0.3 |\r\n| database5                    | utf8    |    165 |        0 |    77.4 |\r\n| database6                    | utf8    |    121 |        2 |   719.4 |\r\n| database7                    | utf8    |    122 |        0 |    91.3 |\r\n| database8                    | utf8    |    116 |        0 |    89.7 |\r\n| database9                    | utf8    |    124 |        0 |     4.5 |\r\n| database10                   | utf8    |    113 |        0 |   147.7 |\r\n| database11                   | latin1  |    119 |        3 |   436.4 |\r\n| database12                   | latin1  |    122 |        0 |   439.1 |\r\n| database13                   | latin1  |    122 |        4 |   452.7 |\r\n| database14                   | utf8    |    115 |        0 |   273.0 |\r\n| database15                   | utf8    |    122 |        0 |   265.5 |\r\n| database16                   | utf8    |      0 |        0 |    NULL |\r\n| database17                   | latin1  |     26 |        0 |     0.9 |\r\n| database18                   | latin1  |      9 |        0 |     0.0 |\r\n| database19                   | latin1  |     22 |        0 |     0.3 |\r\n+------------------------------+---------+--------+----------+---------+\r\n19 rows in set (3.01 sec)<\/pre>\n<p>&nbsp;<\/p>\n<p>The first time execution maybe you will get results in a few seconds.<\/p>\n<p>There is the source code:<\/p>\n<p>&nbsp; <\/p>\n<pre lang=\"sql\">\r\nDELIMITER $$\r\n\r\nDROP PROCEDURE IF EXISTS tools.sp_overview$$\r\nCREATE PROCEDURE tools.sp_overview()\r\nBEGIN\r\n\r\n    SELECT s.SCHEMA_NAME as `Database`, s.DEFAULT_CHARACTER_SET_NAME as `Charset`,\r\n        count(t.TABLE_NAME) as `Tables`,\r\n\r\n        (SELECT count(*) from information_schema.ROUTINES as r\r\n            WHERE r.routine_schema = s.SCHEMA_NAME) as `Routines`,\r\n\r\n         round(sum(t.DATA_LENGTH + t.INDEX_LENGTH) \/ 1048576 ,1) as `Size Mb`\r\n\r\n        FROM information_schema.SCHEMATA AS s\r\n            LEFT JOIN information_schema.TABLES t on s.schema_name = t.table_schema\r\n        WHERE s.SCHEMA_NAME not in ('information_schema', 'performance_schema')\r\n\r\n    GROUP BY s.SCHEMA_NAME;\r\nEND$$\r\nDELIMITER ;<\/pre>\n<div class=\"sharedaddy sd-sharing-enabled\"><div class=\"robots-nocontent sd-block sd-social sd-social-official sd-sharing\"><h3 class=\"sd-title\">Share this:<\/h3><div class=\"sd-content\"><ul><li class=\"share-twitter\"><a href=\"https:\/\/twitter.com\/share\" class=\"twitter-share-button\" data-url=\"http:\/\/en.latindevelopers.com\/ivancp\/2012\/mysql-get-disk-usage-of-all-databases\/\" data-text=\"MySQL get disk usage of all databases\"  >Tweet<\/a><\/li><li class=\"share-facebook\"><div class=\"fb-share-button\" data-href=\"http:\/\/en.latindevelopers.com\/ivancp\/2012\/mysql-get-disk-usage-of-all-databases\/\" data-layout=\"button_count\"><\/div><\/li><li class=\"share-end\"><\/li><\/ul><\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>I was tired to get manually disk space used for all MySQL databases, I just created a stored procedure to get an overview of the database sizes in our MySQL server. MySQL don&#8217;t have a command that allows us an overall summary of the databases, something like SHOW TABLE STATUS for databases. The &#8220;SHOW DATABASES&#8221; [&hellip;]<\/p>\n<div class=\"sharedaddy sd-sharing-enabled\"><div class=\"robots-nocontent sd-block sd-social sd-social-official sd-sharing\"><h3 class=\"sd-title\">Share this:<\/h3><div class=\"sd-content\"><ul><li class=\"share-twitter\"><a href=\"https:\/\/twitter.com\/share\" class=\"twitter-share-button\" data-url=\"http:\/\/en.latindevelopers.com\/ivancp\/2012\/mysql-get-disk-usage-of-all-databases\/\" data-text=\"MySQL get disk usage of all databases\"  >Tweet<\/a><\/li><li class=\"share-facebook\"><div class=\"fb-share-button\" data-href=\"http:\/\/en.latindevelopers.com\/ivancp\/2012\/mysql-get-disk-usage-of-all-databases\/\" data-layout=\"button_count\"><\/div><\/li><li class=\"share-end\"><\/li><\/ul><\/div><\/div><\/div>","protected":false},"author":1,"featured_media":21,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[4],"tags":[5,6],"jetpack_featured_media_url":"http:\/\/en.latindevelopers.com\/ivancp\/wp-content\/uploads\/2012\/02\/logo-mysql-170x115.png","jetpack_shortlink":"https:\/\/wp.me\/p2f09V-1j","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"http:\/\/en.latindevelopers.com\/ivancp\/wp-json\/wp\/v2\/posts\/81"}],"collection":[{"href":"http:\/\/en.latindevelopers.com\/ivancp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/en.latindevelopers.com\/ivancp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/en.latindevelopers.com\/ivancp\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/en.latindevelopers.com\/ivancp\/wp-json\/wp\/v2\/comments?post=81"}],"version-history":[{"count":4,"href":"http:\/\/en.latindevelopers.com\/ivancp\/wp-json\/wp\/v2\/posts\/81\/revisions"}],"predecessor-version":[{"id":85,"href":"http:\/\/en.latindevelopers.com\/ivancp\/wp-json\/wp\/v2\/posts\/81\/revisions\/85"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/en.latindevelopers.com\/ivancp\/wp-json\/wp\/v2\/media\/21"}],"wp:attachment":[{"href":"http:\/\/en.latindevelopers.com\/ivancp\/wp-json\/wp\/v2\/media?parent=81"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/en.latindevelopers.com\/ivancp\/wp-json\/wp\/v2\/categories?post=81"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/en.latindevelopers.com\/ivancp\/wp-json\/wp\/v2\/tags?post=81"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}