3

根据您的 SQL Server 数据库的许可和配置,您可能有非常不同的大小限制。例如:

  • SQL Server 2012 Express 有 10GB 的限制
  • SQL Server 2008 R2 Express 有 10GB 的限制
  • SQL Server 2008 Express 的限制为 4GB

是否可以查询数据库服务器以找出最大大小是多少?

我可以查询以找出当前的大小:

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  

我现在所追求的是一种基于许可而不是可用硬盘空间来找出最大值的方法。

4

4 回答 4

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)在字典上更大!)

于 2014-06-26T11:37:34.300 回答
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
于 2013-04-12T06:19:10.153 回答
0

我不确定如何在“给我数据库的最大大小”级别执行此操作,但是您可以sys.database_files从构成数据库的文件中查询并将其拼凑在一起。

我认为相关列将是max_size,即 8KB 页面中的最大文件大小。

PS。 sys.database_files是每个数据库的视图,如果您想在整个 SQL Server 实例中查看它,那么sys.master_files可能会更好......

于 2013-04-11T23:01:54.403 回答
-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;
于 2013-04-11T23:47:47.040 回答