脚本汇总

Posted gered

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了脚本汇总相关的知识,希望对你有一定的参考价值。

【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

 

以上是关于脚本汇总的主要内容,如果未能解决你的问题,请参考以下文章

html 将以编程方式附加外部脚本文件的javascript代码片段,并按顺序排列。用于响应式网站,其中ma

;~ 小部分AutoHotkey源代码片段测试模板2019年10月9日.ahk

如何在 Toad for Oracle 中使用自定义代码片段?

Golang实践录:反射reflect的一些研究及代码汇总

RexNet片段记录

RexNet片段记录