在SQL Server 2014中列出数据库和表空间

时间:2016-01-08 13:29:47

标签: sql-server tsql sql-server-2014

如何列出SQL Server 2014中的所有数据库以及每个数据库的表空间大小?

我测试了这个查询

EXEC sp_spaceused null, false

但我只得到一个数据库的结果。

9 个答案:

答案 0 :(得分:8)

The undocumented sp_MSforeachdb procedure可以循环所有数据库,但有一些限制。

EXECUTE master.sys.sp_MSforeachdb 'USE [?]; EXEC sp_spaceused null, false'

答案 1 :(得分:3)

是否可以使用一个SQL查询执行所有操作?

IF OBJECT_ID('tempdb.dbo.#space') IS NOT NULL
    DROP TABLE #space

CREATE TABLE #space (
      database_id INT PRIMARY KEY
    , data_used_size DECIMAL(18,2)
    , log_used_size DECIMAL(18,2)
)

DECLARE @SQL NVARCHAR(MAX)

SELECT @SQL = STUFF((
    SELECT '
    USE [' + d.name + ']
    INSERT INTO #space (database_id, data_used_size, log_used_size)
    SELECT
          DB_ID()
        , SUM(CASE WHEN [type] = 0 THEN space_used END)
        , SUM(CASE WHEN [type] = 1 THEN space_used END)
    FROM (
        SELECT s.[type], space_used = SUM(FILEPROPERTY(s.name, ''SpaceUsed'') * 8. / 1024)
        FROM sys.database_files s
        GROUP BY s.[type]
    ) t;'
    FROM sys.databases d
    WHERE d.[state] = 0
    FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 2, '')

EXEC sys.sp_executesql @SQL

IF OBJECT_ID('tempdb.dbo.#table') IS NOT NULL
    DROP TABLE #table

CREATE TABLE #table (
      database_id INT
    , obj_name NVARCHAR(1000)
    , total_rows INT
    , total_space DECIMAL(18,2)
    , used_space DECIMAL(18,2)
    , unused_space DECIMAL(18,2)
    , index_space DECIMAL(18,2)
    , data_space DECIMAL(18,2)
)

SELECT @SQL = STUFF((
    SELECT '
    USE [' + d.name + ']
    INSERT INTO #table
    SELECT 
          DB_ID()
        , s.name + ''.'' + o.name
        , i.total_rows
        , total_space = i.total_pages * 8. / 1024
        , used_space = i.used_pages * 8. / 1024
        , unused_space = (i.total_pages - i.used_pages) * 8. / 1024
        , index_space = i.index_pages * 8. / 1024
        , data_space = data_pages * 8. / 1024
    FROM sys.objects o
    JOIN sys.schemas s ON o.[schema_id] = s.[schema_id]
    JOIN (
        SELECT
              i.[object_id]
            , total_pages = SUM(a.total_pages)
            , used_pages = SUM(a.used_pages)
            , index_pages = SUM(a.used_pages - CASE WHEN a.[type] != 1 THEN a.used_pages WHEN p.index_id IN (0, 1) THEN a.data_pages ELSE 0 END) 
            , data_pages = SUM(CASE WHEN a.[type] != 1 THEN a.used_pages WHEN p.index_id IN (0, 1) THEN a.data_pages END)
            , total_rows = SUM(CASE WHEN i.index_id IN (0, 1) AND a.[type] = 1 THEN p.[rows] END)
        FROM sys.indexes i
        JOIN sys.partitions p ON i.[object_id] = p.[object_id] AND i.index_id = p.index_id
        JOIN sys.allocation_units a ON p.[partition_id] = a.container_id
        WHERE i.is_disabled = 0
            AND i.is_hypothetical = 0
        GROUP BY i.[object_id]
    ) i ON o.[object_id] = i.[object_id]
    WHERE o.[type] = ''U''
        AND o.is_ms_shipped = 0;'
    FROM sys.databases d
    WHERE d.[state] = 0
    FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 2, '')

EXEC sys.sp_executesql @SQL

SELECT
      d.name
    , t.total_size
    , t.data_size
    , s.data_used_size
    , t.log_size
    , s.log_used_size
    , tt.*
FROM (
    SELECT
          database_id
        , log_size = CAST(SUM(CASE WHEN [type] = 1 THEN size END) * 8. / 1024 AS DECIMAL(18,2))
        , data_size = CAST(SUM(CASE WHEN [type] = 0 THEN size END) * 8. / 1024 AS DECIMAL(18,2))
        , total_size = CAST(SUM(size) * 8. / 1024 AS DECIMAL(18,2))
    FROM sys.master_files
    GROUP BY database_id
) t
JOIN sys.databases d ON d.database_id = t.database_id
LEFT JOIN #space s ON d.database_id = s.database_id
LEFT JOIN #table tt ON t.database_id = tt.database_id
ORDER BY t.total_size DESC, tt.total_space DESC

输出 -

name             total_size    data_size   data_used_size   log_size    log_used_size   database_id obj_name               total_rows  total_space     used_space    unused_space   index_space   data_space
---------------- ------------- ----------- ---------------- ----------- --------------- ----------- ---------------------- ----------- --------------- ------------- -------------- ------------- -----------------
StackOverflow    66339.88      65840.00    65102.06         499.88      4.94            24          dbo.Posts              21736594    49695.45        49452.38      243.08         157.95        49294.43
StackOverflow    66339.88      65840.00    65102.06         499.88      4.94            24          dbo.Comments           36585420    11879.19        11877.70      1.48           44.21         11833.49
StackOverflow    66339.88      65840.00    65102.06         499.88      4.94            24          dbo.Votes              67258370    2419.73         2419.38       0.35           9.03          2410.35
StackOverflow    66339.88      65840.00    65102.06         499.88      4.94            24          dbo.Badges             12783309    619.26          619.15        0.11           2.30          616.84
StackOverflow    66339.88      65840.00    65102.06         499.88      4.94            24          dbo.Users              3473095     411.78          410.66        1.12           1.53          409.13
StackOverflow    66339.88      65840.00    65102.06         499.88      4.94            24          dbo.PostLinks          2271053     72.76           72.70         0.05           0.28          72.42
StackOverflow    66339.88      65840.00    65102.06         499.88      4.94            24          dbo.LinkTypes          2           0.02            0.02          0.00           0.01          0.01
StackOverflow    66339.88      65840.00    65102.06         499.88      4.94            24          dbo.VoteTypes          15          0.02            0.02          0.00           0.01          0.01
StackOverflow    66339.88      65840.00    65102.06         499.88      4.94            24          dbo.PostTypes          8           0.02            0.02          0.00           0.01          0.01

答案 2 :(得分:2)

数据库 -

IF OBJECT_ID('tempdb.dbo.#space') IS NOT NULL
    DROP TABLE #space

CREATE TABLE #space (
      database_id INT PRIMARY KEY
    , data_used_size DECIMAL(18,2)
    , log_used_size DECIMAL(18,2)
)

DECLARE @SQL NVARCHAR(MAX)

SELECT @SQL = STUFF((
    SELECT '
    USE [' + d.name + ']
    INSERT INTO #space (database_id, data_used_size, log_used_size)
    SELECT
          DB_ID()
        , SUM(CASE WHEN [type] = 0 THEN space_used END)
        , SUM(CASE WHEN [type] = 1 THEN space_used END)
    FROM (
        SELECT s.[type], space_used = SUM(FILEPROPERTY(s.name, ''SpaceUsed'') * 8. / 1024)
        FROM sys.database_files s
        GROUP BY s.[type]
    ) t;'
    FROM sys.databases d
    WHERE d.[state] = 0
    FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 2, '')

EXEC sys.sp_executesql @SQL

SELECT
      d.database_id
    , d.name
    , t.total_size
    , t.data_size
    , s.data_used_size
    , t.log_size
    , s.log_used_size
FROM (
    SELECT
          database_id
        , log_size = CAST(SUM(CASE WHEN [type] = 1 THEN size END) * 8. / 1024 AS DECIMAL(18,2))
        , data_size = CAST(SUM(CASE WHEN [type] = 0 THEN size END) * 8. / 1024 AS DECIMAL(18,2))
        , total_size = CAST(SUM(size) * 8. / 1024 AS DECIMAL(18,2))
    FROM sys.master_files
    GROUP BY database_id
) t
JOIN sys.databases d ON d.database_id = t.database_id
LEFT JOIN #space s ON d.database_id = s.database_id
ORDER BY t.total_size DESC

表 -

IF OBJECT_ID('tempdb.dbo.#space') IS NOT NULL
    DROP TABLE #space

CREATE TABLE #space (
      database_id INT
    , obj_name NVARCHAR(1000)
    , total_rows INT
    , total_space DECIMAL(18,2)
    , used_space DECIMAL(18,2)
    , unused_space DECIMAL(18,2)
    , index_space DECIMAL(18,2)
    , data_space DECIMAL(18,2)
)

DECLARE @SQL NVARCHAR(MAX)

SELECT @SQL = STUFF((
    SELECT '
    USE [' + d.name + ']
    INSERT INTO #space
    SELECT 
          DB_ID()
        , s.name + ''.'' + o.name
        , i.total_rows
        , total_space = i.total_pages * 8. / 1024
        , used_space = i.used_pages * 8. / 1024
        , unused_space = (i.total_pages - i.used_pages) * 8. / 1024
        , index_space = i.index_pages * 8. / 1024
        , data_space = data_pages * 8. / 1024
    FROM sys.objects o
    JOIN sys.schemas s ON o.[schema_id] = s.[schema_id]
    JOIN (
        SELECT
              i.[object_id]
            , total_pages = SUM(a.total_pages)
            , used_pages = SUM(a.used_pages)
            , index_pages = SUM(a.used_pages - CASE WHEN a.[type] != 1 THEN a.used_pages WHEN p.index_id IN (0, 1) THEN a.data_pages ELSE 0 END) 
            , data_pages = SUM(CASE WHEN a.[type] != 1 THEN a.used_pages WHEN p.index_id IN (0, 1) THEN a.data_pages END)
            , total_rows = SUM(CASE WHEN i.index_id IN (0, 1) AND a.[type] = 1 THEN p.[rows] END)
        FROM sys.indexes i
        JOIN sys.partitions p ON i.[object_id] = p.[object_id] AND i.index_id = p.index_id
        JOIN sys.allocation_units a ON p.[partition_id] = a.container_id
        WHERE i.is_disabled = 0
            AND i.is_hypothetical = 0
        GROUP BY i.[object_id]
    ) i ON o.[object_id] = i.[object_id]
    WHERE o.[type] = ''U''
        AND o.is_ms_shipped = 0;'
    FROM sys.databases d
    WHERE d.[state] = 0
    FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 2, '')

EXEC sys.sp_executesql @SQL

SELECT DB_NAME(t.database_id), *
FROM #space t

答案 3 :(得分:2)

我正在使用以下查询为每个数据库列出每个文件组的大小(TABLESPACE)。只有一个命令,没有明确使用tempdb:

DECLARE @database_id int 
DECLARE @database_name sysname 
DECLARE @sql_string nvarchar(2000) 
DECLARE @file_size TABLE 
    ( 
    [database_name] [sysname] NULL, 
    [groupid] [smallint] NULL, 
    [groupname] sysname NULL, 
    [fileid] [smallint] NULL, 
    [file_size] [decimal](12, 2) NULL, 
    [space_used] [decimal](12, 2) NULL, 
    [free_space] [decimal](12, 2) NULL, 
    [name] [sysname] NOT NULL, 
    [filename] [nvarchar](260) NOT NULL 
    )
SELECT TOP 1 @database_id = database_id 
    ,@database_name = name 
FROM sys.databases 
WHERE database_id > 0 
ORDER BY database_id
WHILE @database_name IS NOT NULL 
BEGIN
    SET @sql_string = 'USE ' + QUOTENAME(@database_name) + CHAR(10) 
    SET @sql_string = @sql_string + 'SELECT 
                                        DB_NAME() 
                                        ,sysfilegroups.groupid 
                                        ,sysfilegroups.groupname 
                                        ,fileid 
                                        ,convert(decimal(12,2),round(sysfiles.size/128.000,2)) as file_size 
                                        ,convert(decimal(12,2),round(fileproperty(sysfiles.name,''SpaceUsed'')/128.000,2)) as space_used 
                                        ,convert(decimal(12,2),round((sysfiles.size-fileproperty(sysfiles.name,''SpaceUsed''))/128.000,2)) as free_space 
                                        ,sysfiles.name 
                                        ,sysfiles.filename 
                                    FROM sys.sysfiles 
                                    LEFT OUTER JOIN sys.sysfilegroups 
                                        ON sysfiles.groupid = sysfilegroups.groupid'

    INSERT INTO @file_size 
        EXEC sp_executesql @sql_string   
    SET @database_name = NULL 
    SELECT TOP 1 @database_id = database_id 
        ,@database_name = name 
    FROM sys.databases 
    WHERE database_id > @database_id 
    ORDER BY database_id 
END
SELECT database_name,  ISNULL(groupname,'TLOG') groupname, SUM(file_size) as filegroup_size, SUM(space_used) as space_used, SUM(free_space) as free_space 
FROM @file_size 
GROUP BY database_name, groupid, groupname

输出:

database_name       groupname   file_size   space_used  free_space
AdventureWorksLT    TLOG    18.00   1.03    16.97
AdventureWorksLT    PRIMARY 21.00   5.88    15.13
t_Temp              TLOG    64.94   3.79    61.15
t_Temp              PRIMARY 3867.00 1947.38 1919.63
t_Temp              fg1 3.00    0.31    2.69
t_Temp              fg2 3.00    0.31    2.69
t_Temp              fg3 3.00    0.31    2.69
t_Temp              fg4 3.00    0.31    2.69
CSB                 TLOG    10.00   1.11    8.89
CSB                 PRIMARY 25.00   2.50    22.50
CSB                 DATA    25.00   2.63    22.38

答案 4 :(得分:1)

我们在我们的环境中使用以下查询来查找每个数据库的大小。看看它是否对您的情况有帮助。

SELECT dbfile.NAME          AS DatabaseFileName, 
       dbfile.size / 128    AS FileSizeInMB, 
       sysFG.NAME           AS FileGroupName, 
       dbfile.physical_name AS DatabaseFilePath 
FROM   sys.database_files AS dbfile 
       INNER JOIN sys.filegroups AS sysFG 
               ON dbfile.data_space_id = sysFG.data_space_id

答案 5 :(得分:1)

您可以按如下方式获取数据库名称和总大小:

select db.name, SUM(size) * 8. / 1024 as totalsize
from master..sysdatabases db 
inner join sys.master_files dbf on db.dbid=dbf.database_id 
group by db.name

答案 6 :(得分:0)

您可以尝试此查询以检查Sql server中的表空间 我在Sql server 2012中使用它并且它工作正常,但我认为它适用于任何版本。

请告诉我是否对您有用。

SELECT 
    t.NAME  "Table Name",
    s.Name  "Schema Name",
    p.rows  "RowCounts",
    SUM(a.total_pages) * 8  "Total Space KB", 
    SUM(a.used_pages) * 8  "Used Space KB", 
    (SUM(a.total_pages) - SUM(a.used_pages)) * 8  "UnusedSpaceKB"
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

答案 7 :(得分:0)

sp_spaceused功能的完整副本 -

IF OBJECT_ID('tempdb.dbo.#temp') IS NOT NULL
    DROP TABLE #temp

CREATE TABLE #temp (
    [db_name] SYSNAME PRIMARY KEY,
    database_size_mb DECIMAL(18,2),
    unallocated_space_mb DECIMAL(18,2),
    reserved_mb DECIMAL(18,2),
    data_mb DECIMAL(18,2),
    index_size_mb DECIMAL(18,2),
    unused_mb DECIMAL(18,2),
)

DECLARE @SQL NVARCHAR(MAX)

SELECT @SQL = STUFF((
    SELECT '
USE [' + d.name + ']

INSERT INTO #temp
SELECT DB_NAME(),
       database_size = (dbsize + logsize) * 8. / 1024,
       unallocated_space = CASE WHEN dbsize >= reservedpages THEN (dbsize - reservedpages) * 8. / 1024 ELSE 0 END,
       reserved = reservedpages * 8. / 1024,
       data = pages * 8. / 1024,
       index_size = (usedpages - pages) * 8. / 1024,
       unused = (reservedpages - usedpages) * 8. / 1024
FROM (
    SELECT dbsize = SUM(CASE WHEN status & 64 = 0 THEN size END),
           logsize = SUM(CASE WHEN status & 64 <> 0 THEN size END)
    FROM dbo.sysfiles
) t
CROSS APPLY (
    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, 236) THEN 0
                   WHEN a.[type] <> 1 AND p.index_id < 2 THEN a.used_pages
                   WHEN p.index_id < 2 AND it.is_ms_shipped = 0 THEN a.data_pages
                   ELSE 0
               END
           )
    FROM sys.partitions p
    JOIN sys.allocation_units a ON p.[partition_id] = a.container_id
    JOIN sys.objects o ON p.[object_id] = o.[object_id]
    LEFT JOIN sys.internal_tables it ON p.[object_id] = it.[object_id]
) t1'
    FROM sys.databases d
    WHERE d.[state] = 0
    FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 2, '')

EXEC sys.sp_executesql @SQL

SELECT * FROM #temp

输出 -

db_name                database_size_mb      unallocated_space_mb   reserved_mb        data_mb       index_size_mb      unused_mb
---------------------- --------------------- ---------------------- ------------------ ------------- ------------------ ----------------
AdventureWorks2012     279.25                47.24                  189.76             3.38          175.56             10.81
distribution           7.00                  1.87                   3.13               0.16          2.53               0.44
locateme               1050.13               588.33                 2.67               0.05          2.17               0.45
mail                   8.00                  2.96                   3.04               0.14          2.41               0.49
master                 554.00                487.78                 4.28               0.44          3.22               0.63
model                  7.13                  1.78                   2.28               0.04          1.88               0.37
msdb                   46.44                 1.20                   19.05              5.80          12.13              1.12
sql-format.com         126.39                1.06                   124.31             116.35        7.25               0.71
Refactoring            494.32                57.90                  308.54             0.17          307.25             1.12
StackOverflow          66339.88              739.06                 65100.94           6996.23       57858.06           246.65
tempdb                 9.50                  5.49                   2.51               0.05          2.07               0.39

输出(EXEC [sql-format.com].sys.sp_spaceused) -

database_name       database_size      unallocated space
------------------- ------------------ ------------------
sql-format.com      126.39 MB          1.06 MB

reserved     data        index_size   unused
------------ ----------- ------------ ------------------
127296 KB    125376 KB   1192 KB      728 KB

答案 8 :(得分:0)

显示数据库的数据和日志空间信息。

检查此链接

https://msdn.microsoft.com/en-us/library/ms190674.aspx#Anchor_1