码迷,mamicode.com
首页 > 其他好文 > 详细

脚本汇总

时间:2019-12-27 11:56:19      阅读:94      评论:0      收藏:0      [点我收藏+]

标签:表行数   ado   data   名称   The   db_name   查看   char   cas   

【1】文件与表数据查看

【1.1】(当前库)表行数、空间使用情况

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 总共占用空间,
    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  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 > 0
GROUP BY  t.Name, s.Name, p.Rows
ORDER BY  [总共占用空间] desc
--实时数据查看,核心DMV:sys.dm_db_partition_stats

【1.2】查看当前库的文件情况(同步)

SELECT a.name [文件名称] ,cast(a.[size]*1.0/128 as decimal(12,1)) AS [文件设置大小(MB)] ,
    CAST( fileproperty(s.name,SpaceUsed)/(8*16.0) AS DECIMAL(12,1)) AS [文件所占空间(MB)] ,
    CAST( (fileproperty(s.name,SpaceUsed)/(8*16.0))/(s.size/(8*16.0))*100.0 AS DECIMAL(12,1)) AS [所占空间率%] ,
    CASE WHEN A.growth =0 THEN 文件大小固定,不会增长 ELSE 文件将自动增长 end [增长模式] ,CASE WHEN A.growth > 0 AND is_percent_growth = 0 
    THEN 增量为固定大小 WHEN A.growth > 0 AND is_percent_growth = 1 THEN 增量将用整数百分比表示 ELSE 文件大小固定,不会增长 END AS [增量模式] ,
    CASE WHEN A.growth > 0 AND is_percent_growth = 0 THEN cast(cast(a.growth*1.0/128as decimal(12,0)) AS VARCHAR)+MB 
    WHEN A.growth > 0 AND is_percent_growth = 1 THEN cast(cast(a.growth AS decimal(12,0)) AS VARCHAR)+% ELSE 文件大小固定,不会增长 end AS [增长值(%或MB)] ,
    a.physical_name AS [文件所在目录] ,a.type_desc AS [文件类型] 
FROM sys.database_files a 
INNER JOIN sys.sysfiles AS s  ON a.[file_id]=s.fileid 
LEFT JOIN sys.dm_db_file_space_usage b ON a.[file_id]=b.[file_id] ORDER BY a.[type]

【1.3】查看所有数据库文件情况(异步)

SELECT       database_id                                AS DataBaseId 
            ,DB_NAME(database_id)                       AS DataBaseName 
            ,Name                                       AS LogicalName 
            ,type_desc                                  AS FileTypeDesc 
            ,Physical_Name                              AS PhysicalName 
            ,State_Desc                                 AS StateDesc 
            ,CASE WHEN max_size = 0  THEN N不允许增长
                  WHEN max_size = -1 THEN N自动增长
             ELSE LTRIM(STR(max_size * 8.0 / 1024 / 1024, 14, 2))+ G
             END  AS MaxSize 
            ,CASE WHEN is_percent_growth = 1
                 THEN RTRIM(CAST(Growth AS CHAR(10))) + %
                 ELSE RTRIM(CAST(Growth*8.0/1024 AS CHAR(10))) + M
            END                                          AS Growth 
            ,Is_Read_Only                                AS IsReadOnly 
            ,Is_Percent_Growth                           AS IsPercentGrowth 
            ,CAST(size * 8.0 / 1024 / 1024 AS DECIMAL(8, 4)) AS [Size(GB)]
FROM     sys.master_files
--WHERE name =tempdb
ORDER BY 1

【1.4】查看堆表数量超过10W行的

select * from (
SELECT tables.NAME, 
       (SELECT rows 
        FROM   sys.partitions 
        WHERE  object_id = tables.object_id 
               AND index_id = 0 -- 0 is for heap 
               -- 1 is for clustered index 
                And rows >=100000
       )AS numberofrows 
FROM   db_tank.sys.tables tables 
WHERE  Objectproperty(tables.object_id, NTableHasClustIndex) = 0
)t where numberofrows is not null

 

脚本汇总

标签:表行数   ado   data   名称   The   db_name   查看   char   cas   

原文地址:https://www.cnblogs.com/gered/p/12106191.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!