Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get MYSQL database size in Java

My MYSQL-Table has got 3 tables.

I can't get the given examples working.

In Java, using java.sql.*, I would like to know how much space is left in my entire database, for the space is very limited.

Here is my approach:

// this.conn = DriverManager.getConnection("jdbc:mysql://......");

public long remainingSpace() {
    String sql = "SELECT table_schema AS \"Database\", "
        + "ROUND(SUM(data_length + index_length) / 1024, 2) AS \"Size (KB)\" "
        + "FROM information_schema.TABLES GROUP BY table_schema;";
    ResultSet rs;
    PreparedStatement prpSttm = this.conn.prepareStatement(sql);
    rs = prpSttm.executeQuery();
    rs.next();
    long remainingSpace = rs.getInt("Size (KB)");
    return remainingSpace;
}

This returns 9 (KB).

phpMyAdmin instead tells me there is 64 KiB of data in one of my tables: Table size according to phpMyAdmin

How can I get the correct size in Java?

like image 349
phil294 Avatar asked Dec 27 '14 14:12

phil294


People also ask

What is Information_schema tables in MySQL?

INFORMATION_SCHEMA provides access to database metadata, information about the MySQL server such as the name of a database or table, the data type of a column, or access privileges. Other terms that are sometimes used for this information are data dictionary and system catalog.

How do I determine the size of a SQL Server database?

If you need to check a single database, you can quickly find the SQL Server database sizein SQL Server Management Studio (SSMS): Right-click the database and then click Reports -> Standard Reports -> Disk Usage. Alternatively, you can use stored procedures like exec sp_spaceused to get database size.

How do I find the size of a MySQL database?

Get the Size for a Single Database To get the size of a single database in MySQL Workbench, right-click on the schema you want to check in the left navigation pane. 2. Select Schema Inspector from the list. On the right pane, under the Index tab, check the database size.


2 Answers

You are just showing the size of the first database the ResultSet returns, you probably have more databases in your DBMS (there are some default system DBs). You should use a while loop.

while (rs.next()) {
    System.out.println(rs.getString("Database") " | " + rs.getString("Size (KB)"));
}

*From my experience, ~9KB is usually the size of the default information_schema database.

**As this query returns databases in alphabetical order, I guess the name of your database starts with a letter >=i; thus, surprisingly your code will work if you rename your database with a name starting eg from 'a' :) :)

like image 117
Kostas Chalkias Avatar answered Oct 04 '22 03:10

Kostas Chalkias


You are only reading one row from your ResultSet in your Java program. But your query generates one row for each distinct database (schema) on your server. You may be reporting information for some database other than yours. You'd best read all the rows of that ResultSet.

Beware: Disk space exhaustion for MySQL can be catastrophic. You really don't want to completely run out of disk space. If you're wise you'll leave a margin of at least a few GiB.

like image 39
O. Jones Avatar answered Oct 04 '22 04:10

O. Jones