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

时间:2010-08-31 05:54:52

标签: sql

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

11 个答案:

答案 0 :(得分:91)

SQL Server: -

sp_spaceused 'TableName'

或在管理工作室: 右键单击表格 - >属性 - >存储

MySQL的: -

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

的Sybase: -

sp_spaceused 'TableName'

甲骨文: - how-do-i-calculate-tables-size-in-oracle

答案 1 :(得分:52)

结合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

这将按照保留大小的顺序为您提供所有表的列表,从最大到最小排序。

答案 2 :(得分:19)

SQL Server提供了一个内置的存储过程,您可以运行该过程以轻松显示表的大小,包括索引的大小

sp_spaceused ‘Tablename’

答案 3 :(得分:19)

查询(修改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

答案 4 :(得分:14)

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

语法:

 sp_spaceused ‘Tablename’

见:

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

答案 5 :(得分:8)

你是按尺寸表示表格中的记录数量吗?在那种情况下:

SELECT COUNT(*) FROM your_table_name

答案 6 :(得分:7)

我知道在SQL 2012中(可能在其他版本中有效),您可以执行以下操作:

  1. 右键单击对象资源管理器中的数据库名称。
  2. 选择报告>标准报告> Top Table的磁盘使用情况。
  3. 这将为您提供前1000个表的列表,然后您可以按数据大小等订购它。

答案 7 :(得分:6)

在PostgreSQL中:

SELECT pg_size_pretty(pg_relation_size('tablename'));

答案 8 :(得分:2)

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

  -- 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

答案 9 :(得分:1)

你可以在另一个帖子中引用Marc_s的回答,非常有用。

Get size of all tables in database

答案 10 :(得分:1)

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