Can I query how much disk space certain rows or columns are taking up in MySQL?


Yes, using the below syntax −

select * from information_schema.tables where table_name=yourTableName;

Let us first create a table −

mysql> create table DemoTable1600
   -> (
   -> StudentId int,
   -> StudentFirstName varchar(20)
   -> );
Query OK, 0 rows affected (0.51 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1600 values(100,'Bob');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable1600 values(101,'David');
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable1600 values(102,'Carol');
Query OK, 1 row affected (0.19 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable1600;

This will produce the following output −

+-----------+------------------+
| StudentId | StudentFirstName |
+-----------+------------------+
|       100 | Bob              |
|       101 | David            |
|       102 | Carol            |
+-----------+------------------+
3 rows in set (0.00 sec)

Fetch how much disk space certain rows or columns are taking up −

mysql> select * from information_schema.tables where table_name='DemoTable1600';

This will produce the following output

+---------------+--------------+---------------+------------+--------+---------+------------+------------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-----------------+----------+----------------+---------------+
| TABLE_CATALOG | TABLE_SCHEMA | TABLE_NAME    | TABLE_TYPE | ENGINE | VERSION | ROW_FORMAT | TABLE_ROWS | AVG_ROW_LENGTH | DATA_LENGTH | MAX_DATA_LENGTH | INDEX_LENGTH | DATA_FREE | AUTO_INCREMENT | CREATE_TIME         | UPDATE_TIME | CHECK_TIME | TABLE_COLLATION | CHECKSUM | CREATE_OPTIONS | TABLE_COMMENT |
+---------------+--------------+---------------+------------+--------+---------+------------+------------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-----------------+----------+----------------+---------------+
| def           | web          | demotable1600 | BASE TABLE | InnoDB |      10 | Dynamic    | 0          | 0               | 16384       |              0 |           0 |         0 |         NULL    | 2019-10-19 14:34:46 | NULL       | NULL          | utf8_unicode_ci | NULL    |                |                |
+---------------+--------------+---------------+------------+--------+---------+------------+------------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-----------------+----------+----------------+---------------+
1 row in set (0.00 sec)

Updated on: 10-Jul-2020

393 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements