我有一个非常大的prod数据库。我需要复制此数据库以创建测试环境,但磁盘空间不足。
但实际上不需要从包含用户数据的表中复制行,只需要那些描述功能的表。对于具有用户数据的表,我只需要复制表“description”(列,索引,触发器......)。
如果没有来自某些表的数据,我如何(使用什么查询)估算数据库的大小?
答案 0 :(得分:5)
SET NOCOUNT ON
DBCC UPDATEUSAGE(0)
-- DB size.
EXEC sp_spaceused
-- Table row counts and sizes.
CREATE TABLE #t
(
[name] NVARCHAR(128),
[rows] CHAR(11),
reserved VARCHAR(18),
data VARCHAR(18),
index_size VARCHAR(18),
unused VARCHAR(18)
)
INSERT #t EXEC sp_msForEachTable 'EXEC sp_spaceused ''?'''
SELECT *
FROM #t
-- # of rows.
SELECT SUM(CAST([rows] AS int)) AS [rows]
FROM #t
DROP TABLE #t
AlexanderGroß*
答案 1 :(得分:4)
SELECT
schema_qualified_table = s.name + '.' + t.name,
KB = SUM(ps.reserved_page_count) * 8192/1024.0
FROM sys.tables AS t
INNER JOIN sys.schemas AS s ON t.[schema_id] = s.[schema_id]
INNER JOIN sys.dm_db_partition_stats AS ps
ON t.[object_id] = ps.[object_id]
WHERE s.name + '.' + t.name IN (N'dbo.table1', N'dbo.table2', ...)
-- list of tables you do care about ^^^^^^^^^^ ^^^^^^^^^^
GROUP BY s.name + '.' + t.name;
有点整洁可能是:
;WITH x AS
(
SELECT o = t.[object_id], n = s.name + '.' + t.name
FROM sys.tables AS t
INNER JOIN sys.schemas AS s
ON t.[schema_id] = s.[schema_id]
)
SELECT
schema_qualified_table = x.n,
KB = SUM(ps.reserved_page_count) * 8192/1024.0
FROM x INNER JOIN sys.dm_db_partition_stats AS ps
ON x.o = ps.[object_id]
WHERE x.n IN (N'dbo.table1', N'dbo.table2', ...);
答案 2 :(得分:3)
将我的扩展发布到Aaron Bertrand的答案,只是为了便于阅读。唯一的区别是将连接添加到SCHEMAS表,并在输出和组中包含模式名称。
SELECT
s.name,
t.name,
KB = SUM(ps.reserved_page_count) * 8192/1024.0
FROM sys.tables AS t
INNER JOIN sys.dm_db_partition_stats AS ps
ON t.[object_id] = ps.[object_id]
INNER JOIN sys.schemas S
ON t.schema_id = s.schema_id
WHERE t.name IN (<YOUR TABLES GO HERE) -- list of tables you do care about
GROUP BY t.name,
s.name;
正如Aaron正确地指出的那样,尝试过滤表格和模式的组合可能会非常快速地变得难看。你很容易得到大量的OR:
(S.SCHEMA = 'SCHEMA1' and T.NAME = 'FOO')
OR
(S.SCHEMA = 'SCHEMA2' and T.NAME = 'FOO')
OR
(S.SCHEMA = 'SCHEMA1' and T.name = 'BAR')
...