SQL Server数据库巡检

查询所有表名

select name from sysobjects where xtype='u'

select * from sys.tables

查询所有表名及对应架构

select t.[name] as tablename, s.[name] as [schema] from sys.tables as t,sys.schemas as s where t.schema_id = s.schema_id

查询数据库中所有的表名及行数

SELECT a.name, b.rows

FROM sysobjects AS a INNER JOIN sysindexes AS b ON a.id = b.id

WHERE (a.type = 'u') AND (b.indid IN (0, 1))

ORDER BY b.rows DESC

查询所有的表及空间占用量\行数

select

object_name(id) tablename,

8*reserved/1024 reserved,

rtrim(8*dpages)+'kb' used,

8*(reserved-dpages)/1024 unused,

8*dpages/1024-rows/1024*minlen/1024 free,

rows--,*

from sysindexes
where indid=1
order by tablename,reserved desc

查询数据库中所有表名及行数 --带架构名

SELECT s.name,a.name, b.rows
FROM sys.tables AS a 
INNER JOIN sysindexes AS b ON a.object_id = b.id
LEFT JOIN sys.schemas as s on a.schema_id = s.schema_id
WHERE (a.type = 'u') AND (b.indid IN (0, 1))
ORDER BY b.rows DESC

数据库文件信息

SELECT a.name [文件名称] 
,a.type_desc AS [文件类型] 
    ,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)] 
       , CASE WHEN a.max_size=-1 then -1 ELSE cast(a.[max_size]*1.0/128 as decimal(12,1))  end as [文件增长最大大小(MB)]
    ,a.physical_name 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]

数据库内存情况

SELECT type,
    sum(virtual_memory_reserved_kb)/ 1024  as '保留内存',
    sum(virtual_memory_committed_kb)/ 1024  as '提交的内存', 
    sum(awe_allocated_kb)/ 1024  as '开启AWE后使用的内存', 
    sum(shared_memory_reserved_kb)/ 1024  as '共享的保留内存' ,
    sum(shared_memory_committed_kb)/ 1024  as '共享的提交内存' 
FROM sys.dm_os_memory_clerks 
GROUP BY type
ORDER BY type
 
SELECT  total_physical_memory_kb / 1024 AS [物理内存(MB)] ,  
        available_physical_memory_kb / 1024 AS [可用物理内存(MB)] ,  
        system_cache_kb / 1024 AS [系统缓存内存总量(MB)] ,  
        ( kernel_paged_pool_kb + kernel_nonpaged_pool_kb ) / 1024 AS [内核池内存总量(MB)] ,  
        total_page_file_kb / 1024 AS [操作系统报告的提交限制的大小(MB)] ,  
        available_page_file_kb / 1024 AS [未使用的页文件的总量(MB)] ,  
        system_memory_state_desc AS [内存状态说明]  
FROM    sys.dm_os_sys_memory  

数据库用户

select  sp.name,sp.type,sp.is_disabled,sp.create_date,sp.modify_date,sp.default_database_name,sp.is_fixed_role,sl.is_policy_checked,sl.is_expiration_checked from sys.server_principals sp left join sys.sql_logins sl
on sp.sid=sl.sid

数据库异常检查

WAITFOR TIME '00:10:00' --使SQL Server会话暂停,直到指定的时间。
DBCC CHECKDB() --检查数据库的一致性并修复错误(如果可能)。
posted @ 2024-04-25 14:36  马永猛  阅读(32)  评论(0编辑  收藏  举报