To check a database's allocated space usage, the method depends on the type of database you are using. Below are common approaches for some popular databases:
You can query the information schema to get details about data and index sizes, which reflect how much space is used and allocated.
Example SQL Query:
SELECT
table_schema AS 'Database',
SUM(data_length + index_length) / 1024 / 1024 AS 'Size (MB)',
SUM(data_free) / 1024 / 1024 AS 'Free Space (MB)'
FROM
information_schema.tables
GROUP BY
table_schema;
This gives you the total size (used) and free space per database.
To check space per table:
SELECT
table_name AS 'Table',
ROUND(((data_length + index_length) / 1024 / 1024), 2) AS 'Size (MB)',
ROUND((data_free / 1024 / 1024), 2) AS 'Free (MB)'
FROM
information_schema.tables
WHERE
table_schema = 'your_database_name';
PostgreSQL provides system views like pg_database and pg_total_relation_size.
Check overall database size:
SELECT
datname AS "Database",
pg_size_pretty(pg_database_size(datname)) AS "Size"
FROM
pg_database;
Check size of all tables in a specific database:
SELECT
table_name,
pg_size_pretty(pg_total_relation_size('"' || table_schema || '"."' || table_name || '"')) AS total_size
FROM
information_schema.tables
WHERE
table_schema = 'public'
ORDER BY
pg_total_relation_size('"' || table_schema || '"."' || table_name || '"') DESC;
You can use system views like sys.databases and sys.database_files.
Check database file sizes and space used:
SELECT
name AS 'Database Name',
size * 8.0 / 1024 AS 'Size (MB)',
FILEPROPERTY(name, 'SpaceUsed') * 8.0 / 1024 AS 'Used (MB)',
(size - FILEPROPERTY(name, 'SpaceUsed')) * 8.0 / 1024 AS 'Free (MB)',
physical_name AS 'File Location'
FROM
sys.database_files;
Or for a more summarized view across all DBs:
SELECT
DB_NAME(database_id) AS DatabaseName,
CAST(SUM(size * 8.0 / 1024) AS DECIMAL(10,2)) AS SizeMB,
CAST(SUM(CAST(FILEPROPERTY(name, 'SpaceUsed') AS float) * 8.0 / 1024) AS DECIMAL(10,2)) AS UsedMB
FROM
sys.master_files
GROUP BY
database_id;
If you are using TencentDB for MySQL, TencentDB for PostgreSQL, or other managed database services on Tencent Cloud, you can:
TencentDB also provides automated monitoring and alerts for storage usage, so you can configure thresholds accordingly.
To efficiently monitor and manage your database's space usage, Tencent Cloud offers TencentDB, a fully managed database service that supports MySQL, PostgreSQL, MariaDB, Redis, and more. It provides built-in monitoring dashboards, automated backups, performance optimization, and alerts for storage usage. You can easily view real-time allocated and used space through the Tencent Cloud Console. Additionally, Cloud Monitor and Database Audit services help enhance visibility and security. Explore more at: https://www.tencentcloud.com/product/tencentdb