10

Is there a query or function that I can use to determine the size of a database in MySQL? If not what is the typical way to find the size of database in MySQL?

I was googling and found SELECT CONCAT(sum(ROUND(((DATA_LENGTH + INDEX_LENGTH - DATA_FREE) / 1024 / 1024),2))," MB") AS Size FROM INFORMATION_SCHEMA.TABLES where TABLE_SCHEMA like '%YOUR_DB_NAME%' ;

And it returns a database that I know is 400MB to be 474989023196466.25 MB!

Daniel Li
  • 14,976
  • 6
  • 43
  • 60
  • In all of my tables, data_free is much larger than data_length, meaning that your query would return a negative number. Perhaps it is interpreting it as an unsigned positive integer, hence the nonsensical result. When I try pasting in that query, I get the error "BIGINT UNSIGNED value is out of range". – octern Aug 07 '12 at 18:38
  • For size of a specific database/table answer provided in this page will help you http://www.rathishkumar.in/2017/12/how-to-find-database-and-table-size-in-mysql.html – Rathish Kumar B Dec 29 '17 at 12:45
  • 1
    Does this answer your question? [How to get size of mysql database?](https://stackoverflow.com/questions/1733507/how-to-get-size-of-mysql-database) – frederj Jun 06 '21 at 16:27
  • @frederj your link is to question asked AFTER this one – Prophet Jun 06 '21 at 19:56
  • @Prophet The date of the question doesn't really matter. The target question should be the best one, based on the question, and posted answers. – cigien Jun 06 '21 at 23:31
  • I don't think so. There are several somewhat similar questions for each and every subject here and it's OK, we do not need to remove all of them and leave only one question per each subject. Especially when that question is good, gained a lot of upvotes itself and on it's answers. – Prophet Jun 07 '21 at 05:46

3 Answers3

25

Try with this query :

SELECT table_schema AS "Data Base Name", 
sum( data_length + index_length ) / 1024 / 1024 AS "Data Base Size in MB" 
FROM information_schema.TABLES GROUP BY table_schema ;

Or with this, if you want to ROUND :

SELECT table_schema AS "Data Base Name", 
ROUND(SUM( data_length + index_length ) / 1024 / 1024, 2) AS "Data Base Size in MB" 
FROM information_schema.TABLES GROUP BY table_schema ;
aleroot
  • 71,077
  • 30
  • 176
  • 213
3

Try:

SELECT table_schema, sum(data_length + index_length) FROM information_schema.TABLES GROUP BY table_schema;
Daniel Li
  • 14,976
  • 6
  • 43
  • 60
0
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; 
rusllonrails
  • 5,586
  • 3
  • 34
  • 27