根据SQL Server数据库的许可和配置,您可能具有非常不同的大小限制。例如:
是否可以查询数据库服务器以找出最大大小?
我可以查询以找出当前的大小:
name DataFileSizeMB LogFileSizeMB
--------- -------------- -------------
master 4.875000 1.750000
tempdb 8.000000 0.500000
model 4.062500 1.000000
msdb 16.687500 4.562500
Harrison 21.937500 18.187500
Lennon 122.750000 425.625000
McCartney 19.937500 49.687500
Starr 19.937500 18.187500
我现在所追求的是基于许可而不是可用硬盘空间来找出最大值的方法。
答案 0 :(得分:2)
我知道Devart的ansatz,但是有点紧凑的东西:
SELECT max_db_size_in_gb =
CASE WHEN serverproperty('EngineEdition') = 4 THEN
CASE WHEN SERVERPROPERTY('productversion') between '10.50' and '5' THEN 10 ELSE 4 END
ELSE -1
END
(而不是解析@@ VERSION的输出,检查SERVERPROPERTY('productversion')肯定更容易?而且,当发布SQL Server 2014等新版本时,这会更安全。)
(编辑:旧版本只是检查“< '10 .50'”,这当然是错误的,因为例如'9.00'(SQL Server 2005)在字典上更大!)
答案 1 :(得分:1)
试试这个 -
SELECT
d.server_name
, d.sversion_name
, d.edition
, max_db_size_in_gb =
CASE WHEN engine_edition = 4
THEN
CASE
WHEN d.sversion_name LIKE '%2012%' THEN 10
WHEN d.sversion_name LIKE '%2008 R2%' THEN 10
WHEN d.sversion_name LIKE '%2008%' THEN 4
WHEN d.sversion_name LIKE '%2005%' THEN 4
END
ELSE -1
END
FROM (
SELECT
sversion_name = SUBSTRING(v.ver, 0, CHARINDEX('-', v.ver) - 1)
, engine_edition = SERVERPROPERTY('EngineEdition')
, edition = SERVERPROPERTY('Edition')
, server_name = SERVERPROPERTY('ServerName')
FROM (SELECT ver = @@VERSION) v
) d
另外,请尝试此查询以获取有关DB的扩展信息 -
IF EXISTS(
SELECT 1
FROM tempdb.dbo.sysobjects
WHERE id = OBJECT_ID('tempdb.dbo.#DBObj')
) DROP TABLE #DBObj
CREATE TABLE #DBObj
(
[DB] SYSNAME
, reservedpages INT
, usedpages INT
, pages INT
)
EXEC sys.sp_MSforeachdb '
USE [?]
INSERT INTO #DBObj
(
[DB]
, reservedpages
, usedpages
, pages
)
SELECT
DB_NAME()
, pg.reservedpages
, pg.usedpages
, pg.pages
FROM (
SELECT
reservedpages = SUM(a.total_pages)
, usedpages = SUM(a.used_pages)
, pages = SUM(
CASE
WHEN it.internal_type IN (202, 204, 207, 211, 212, 213, 214, 215, 216, 221, 222) THEN 0
WHEN a.[type] != 1 AND p.index_id < 2 THEN a.used_pages
WHEN p.index_id < 2 THEN a.data_pages ELSE 0
END
)
FROM sys.partitions p
JOIN sys.allocation_units a ON p.[partition_id] = a.container_id
LEFT JOIN sys.internal_tables it ON p.[object_id] = it.[object_id]
) pg'
SELECT
d.name
, total_size_mb = data.row_size_mb + data.log_size_mb
, data.log_size_mb
, data.row_size_mb
, reserved_space_mb = CAST(do.reservedpages * 8. / 1024 AS DECIMAL(10,2))
, data_size_mb = CAST(do.pages * 8. / 1024 AS DECIMAL(10,2))
, index_size_mb = CAST((do.usedpages - do.pages) * 8. / 1024 AS DECIMAL(10,2))
, unused_size_mb = CAST((do.reservedpages - do.usedpages) * 8. / 1024 AS DECIMAL(10,2))
, unallocated_space_mb =
CAST(CASE WHEN data.row_size >= do.reservedpages
THEN (data.row_size - do.reservedpages) * 8. / 1024
ELSE 0
END AS DECIMAL(10,2))
FROM (
SELECT
df2.database_id
, log_size_mb = CAST(df2.log_size * 8. / 1024 AS DECIMAL(10,2))
, row_size_mb = CAST(df2.row_size * 8. / 1024 AS DECIMAL(10,2))
, df2.log_size
, df2.row_size
FROM (
SELECT
df.database_id
, log_size = SUM(CASE WHEN df.type_desc = 'LOG' THEN df.size END)
, row_size = SUM(CASE WHEN df.type_desc = 'ROWS' THEN df.size END)
FROM sys.master_files df
GROUP BY df.database_id
) df2
) data
JOIN sys.databases d ON data.database_id = d.database_id
JOIN #DBObj do ON do.DB = d.name
ORDER BY data.row_size_mb + data.log_size_mb DESC
答案 2 :(得分:0)
我不确定如何在“给我数据库的最大大小”-level上执行此操作,但您可以查询sys.database_files
并将其组成数据库中的文件。< / p>
我认为相关列将是max_size
,这是8KB页面中的最大文件大小。
PS。 sys.database_files
是一个每个数据库的视图,如果你想在整个SQL Server实例中查看它,那么sys.master_files
可能会更好.... < / p>
答案 3 :(得分:-1)
报告的所有尺寸均为 MB
注意 - 有更好的方法可以检查文件是日志还是数据文件;检查“.ldf”后缀适用于99%的安装。
if object_id('tempdb..#sizes') is not null
drop table #sizes;
GO
create table #sizes (dbname sysname, cursize int, maxsize int, isunlimited int, curlogsize int, maxlogsize int, islogunlimited int);
exec sp_msforeachdb '
insert #sizes
select ''?'',
sum(case when filename like ''%.ldf'' then 0 else size end),
sum(case when filename like ''%.ldf'' then 0 else maxsize end),
min(case when filename like ''%.ldf'' then 0 else maxsize end),
sum(case when filename like ''%.ldf'' then size else 0 end),
sum(case when filename like ''%.ldf'' then maxsize else 0 end),
min(case when filename like ''%.ldf'' then maxsize else 0 end)
from [?].sys.sysfiles';
select dbname, cursize / 128.0,
case when isunlimited = -1 then 'unlimited' else str(maxsize/128.0) end, curlogsize/128.0,
case when islogunlimited = -1 then 'unlimited' else str(maxlogsize/128.0) end
from #sizes
order by dbname;