MySQL
查看单个表 大小
SELECT
TABLE_NAME AS `Table`,
ROUND((DATA_LENGTH + INDEX_LENGTH) / 1024 / 1024) AS `Size (MB)`
FROM
information_schema.TABLES
WHERE
TABLE_SCHEMA = "$DB_NAME"
AND
TABLE_NAME = "$TABLE_NAME"
ORDER BY
(DATA_LENGTH + INDEX_LENGTH)
DESC;查看前十大的表, size单位是MB
select table_schema as database_name, table_name, round( (data_length + index_length) / 1024 / 1024, 2) as total_size, round( (data_length) / 1024 / 1024, 2) as data_size, round( (index_length) / 1024 / 1024, 2) as index_size from information_schema.tables where table_schema not in ('information_schema', 'mysql', 'performance_schema' ,'sys') and table_type = 'BASE TABLE' order by total_size desc limit 10;Postgres
// 查看表数据占用空间
SELECT pg_size_pretty(pg_relation_size('sms_push_log'));
// 查看表索引占用空间
SELECT pg_size_pretty(pg_indexes_size('sms_push_log'));
// 查看表的总占用空间
SELECT pg_size_pretty(pg_total_relation_size('sms_push_log'));查看当前数据库 索引利用率
SELECT
relname,
100 * idx_scan / (seq_scan + idx_scan) percent_of_times_index_used,
n_live_tup rows_in_table
FROM
pg_stat_user_tables
WHERE
seq_scan + idx_scan > 0
ORDER BY
n_live_tup DESC;