如何在 SQL 中查找表的大小?

如何在 SQL 中查找表的大小?

298518 次浏览

SQLServer 提供了一个内置存储过程,您可以运行该过程来轻松显示表的大小,包括索引的大小... ... 这可能会让您感到惊讶。

句法:

 sp_spaceused 'Tablename'

参见:

Http://www.howtogeek.com/howto/database/determine-size-of-a-table-in-sql-server/

SQLServer:-

sp_spaceused 'TableName'

Or in management studio: 右键单击表-> 属性-> 存储

MySQL:-

SELECT table_schema, table_name, data_length, index_length FROM information_schema.tables

赛贝斯:-

sp_spaceused 'TableName'

甲骨文:- 如何计算甲骨文中的表格大小

And in PostgreSQL:

SELECT pg_size_pretty(pg_relation_size('tablename'));

Do you by size mean the number of records in the table, by any chance? In that case:

SELECT COUNT(*) FROM your_table_name

结合 Ratty 和 Haim 的帖子(包括评论)的答案,我想出了这个,对于 SQL Server 来说,这似乎是目前为止最优雅的:

-- DROP TABLE #tmpTableSizes
CREATE TABLE #tmpTableSizes
(
tableName varchar(100),
numberofRows varchar(100),
reservedSize varchar(50),
dataSize varchar(50),
indexSize varchar(50),
unusedSize varchar(50)
)
insert #tmpTableSizes
EXEC sp_MSforeachtable @command1="EXEC sp_spaceused '?'"




select  * from #tmpTableSizes
order by cast(LEFT(reservedSize, LEN(reservedSize) - 4) as int)  desc

This gives you a list of all your tables in order of reserved size, ordered from largest to smallest.

我知道在 SQL2012(可能在其他版本中工作)中,您可以执行以下操作:

  1. 在对象资源管理器中右键单击数据库名称。
  2. 选择报表 > 标准报表 > 顶表的磁盘使用情况。

这会给你一个前1000个表的列表,然后你可以按照数据大小等进行排序。

You may refer the answer by Marc_s in another thread, Very useful.

Get size of all tables in database

查询(修改 https://stackoverflow.com/a/7892349/1737819)以查找以 GB 为单位的自定义名称表大小。您可以尝试这样做,将“ YourTableName”替换为表的名称。

SELECT
t.NAME AS TableName,
p.rows AS RowCounts,
CONVERT(DECIMAL,SUM(a.total_pages)) * 8 / 1024 / 1024 AS TotalSpaceGB,
SUM(a.used_pages)  * 8 / 1024 / 1024 AS UsedSpaceGB ,
(SUM(a.total_pages) - SUM(a.used_pages)) * 8 / 1024 / 1024 AS UnusedSpaceGB
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
LEFT OUTER JOIN
sys.schemas s ON t.schema_id = s.schema_id
WHERE
t.NAME = 'YourTable'
AND t.is_ms_shipped = 0
AND i.OBJECT_ID > 255
GROUP BY
t.Name, s.Name, p.Rows
ORDER BY
UsedSpaceGB DESC, t.Name

如果您只是想找到最大的表,这里有一个简单的查询。

  -- Find largest table partitions
SELECT top 20 obj.name, LTRIM (STR ( sz.in_row_data_page_count * 8, 15, 0) + ' KB') as Size, * FROM sys.dm_db_partition_stats sz
inner join sys.objects obj on obj.object_id = sz.object_id
order by sz.in_row_data_page_count desc

SQL Server,所有表的格式都很好,以 KB/MB 为单位:

SELECT
t.NAME AS TableName,
s.Name AS SchemaName,
p.rows AS RowCounts,
SUM(a.total_pages) * 8 AS TotalSpaceKB,
CAST(ROUND(((SUM(a.total_pages) * 8) / 1024.00), 2) AS NUMERIC(36, 2)) AS TotalSpaceMB,
SUM(a.used_pages) * 8 AS UsedSpaceKB,
CAST(ROUND(((SUM(a.used_pages) * 8) / 1024.00), 2) AS NUMERIC(36, 2)) AS UsedSpaceMB,
(SUM(a.total_pages) - SUM(a.used_pages)) * 8 AS UnusedSpaceKB,
CAST(ROUND(((SUM(a.total_pages) - SUM(a.used_pages)) * 8) / 1024.00, 2) AS NUMERIC(36, 2)) AS UnusedSpaceMB
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
LEFT OUTER JOIN
sys.schemas s ON t.schema_id = s.schema_id
WHERE
t.NAME NOT LIKE 'dt%'
AND t.is_ms_shipped = 0
AND i.OBJECT_ID > 255
GROUP BY
t.Name, s.Name, p.Rows
ORDER BY
t.Name

在 SQLServer 中运行下面的查询,您将获得表大小的表。

SELECT
CASE WHEN (GROUPING(sob.name)=1) THEN 'All_Tables'
ELSE ISNULL(sob.name, 'unknown') END AS TableName,
SUM(sys.length) AS ByteLength
FROM sysobjects sob, syscolumns sys
WHERE sob.xtype='u' AND sys.id = sob.id
GROUP BY sob.name
WITH CUBE
ORDER BY SUM(sys.length) DESC

Find Byte Size of All the Tables in Database