如何查询数据库中每个表的大小
- 编程技术
- 2025-01-30 15:29:23
- 1
查询数据库中每个表的大小可以通过不同的数据库管理系统(DBMS)来实现。以下是一些常见数据库系统中查询表大小的常用方法: MySQL```sqlSELECT table...
查询数据库中每个表的大小可以通过不同的数据库管理系统(DBMS)来实现。以下是一些常见数据库系统中查询表大小的常用方法:
MySQL
```sql
SELECT table_schema, table_name, round(((data_length + index_length) / 1024 / 1024), 2) AS `Size (MB)`
FROM information_schema.TABLES
WHERE table_schema NOT IN ('information_schema', 'mysql', 'performance_schema', 'sys')
ORDER BY `Size (MB)` DESC;
```
PostgreSQL
```sql
SELECT
nspname AS "schema",
relname AS "table",
pg_size_pretty(pg_total_relation_size(C.oid)) AS "total_size"
FROM
pg_catalog.pg_class C
LEFT JOIN
pg_catalog.pg_namespace N ON N.oid = C.relnamespace
WHERE
nspname NOT IN ('pg_catalog', 'information_schema')
AND C.relkind <> 'i'
AND c.relname !~ 'pg_'
ORDER BY
pg_total_relation_size(C.oid) DESC;
```
SQL Server
```sql
SELECT
SCHEMA_NAME(t.schema_id) AS SchemaName,
t.name AS TableName,
SUM(a.total_pages) 8 AS TotalSpaceKB
FROM
sys.tables t
INNER JOIN
sys.indexes i ON t.object_id = i.object_id
INNER JOIN
sys.partitions p ON i.object_id = p.object_id AND i.index_id = p.index_id
INNER JOIN
sys.allocation_units a ON p.partition_id = a.container_id
WHERE
t.type = 'U'
GROUP BY
t.schema_id, t.name
ORDER BY
SUM(a.total_pages) 8 DESC;
```
Oracle
```sql
SELECT
table_name,
round(sum(bytes)/1024/1024, 2) as "Size MB"
FROM
user_tables
GROUP BY
table_name
ORDER BY
"Size MB" DESC;
```
请注意,以上SQL查询仅适用于特定数据库的版本,并且可能需要根据你的数据库版本和配置进行调整。在执行这些查询之前,请确保你有足够的权限来访问`information_schema`或`sys`等系统表,因为这些表包含了数据库的元数据信息。
本文链接:http://www.xinin56.com/bian/399332.html
上一篇:初级会计证怎么抵当年继续教育