sql server log文件

确定 SQL Server 实例中具有大量 VDF 的数据库

sql 复制代码
SELECT [name], COUNT(l.database_id) AS 'vlf_count' 
FROM sys.databases AS s
CROSS APPLY sys.dm_db_log_info(s.database_id) AS l
GROUP BY [name]
HAVING COUNT(l.database_id) > 100;

在收缩日志文件之前确定事务日志中最后一个 VLF 的位置

在对事务日志运行 SHRINK FILE 之前,可以使用以下查询来确定最后一个活动 VLF 的位置,以确定事务日志是否可以收缩。

sql 复制代码
 

;WITH cte_vlf AS (
SELECT ROW_NUMBER() OVER(ORDER BY vlf_begin_offset) AS vlfid, DB_NAME(database_id) AS [Database Name], vlf_sequence_number, vlf_active, vlf_begin_offset, vlf_size_mb
    FROM sys.dm_db_log_info(DEFAULT)),
cte_vlf_cnt AS (SELECT [Database Name], COUNT(vlf_sequence_number) AS vlf_count,
    (SELECT COUNT(vlf_sequence_number) FROM cte_vlf WHERE vlf_active = 0) AS vlf_count_inactive,
    (SELECT COUNT(vlf_sequence_number) FROM cte_vlf WHERE vlf_active = 1) AS vlf_count_active,
    (SELECT MIN(vlfid) FROM cte_vlf WHERE vlf_active = 1) AS ordinal_min_vlf_active,
    (SELECT MIN(vlf_sequence_number) FROM cte_vlf WHERE vlf_active = 1) AS min_vlf_active,
    (SELECT MAX(vlfid) FROM cte_vlf WHERE vlf_active = 1) AS ordinal_max_vlf_active,
    (SELECT MAX(vlf_sequence_number) FROM cte_vlf WHERE vlf_active = 1) AS max_vlf_active
    FROM cte_vlf
    GROUP BY [Database Name])
SELECT [Database Name], vlf_count, min_vlf_active, ordinal_min_vlf_active, max_vlf_active, ordinal_max_vlf_active,
((ordinal_min_vlf_active-1)*100.00/vlf_count) AS free_log_pct_before_active_log,
((ordinal_max_vlf_active-(ordinal_min_vlf_active-1))*100.00/vlf_count) AS active_log_pct,
((vlf_count-ordinal_max_vlf_active)*100.00/vlf_count) AS free_log_pct_after_active_log
FROM cte_vlf_cnt;
GO

查看log 大小

sql 复制代码
select mf.name,
mf.type_desc,
mf.state_desc,
mf.size*8.0/1024 as 'SIZE_MB',
max_size/128 as 'MAXSIZE_MB' 
from sys.master_files mf
where   mf.type_desc='LOG' ;

参考:

sys.dm_db_log_info (Transact-SQL) - SQL Server | Microsoft Learn

相关推荐
合作小小程序员小小店21 分钟前
桌面开发,下午茶甜品管理系统开发,基于C#,winform,sql server数据库
开发语言·数据库·sql·microsoft·c#
q***01771 小时前
SQL美化器:sql-beautify安装与配置完全指南
数据库·sql
秋邱1 小时前
高等教育 AI 智能体的 “导学诊践” 闭环
开发语言·网络·数据库·人工智能·python·docker
F36_9_1 小时前
在线协作工具十款测评
网络·数据库
云雾J视界1 小时前
从位运算到状态机:用纯C重构红外/DS18B20/I²C协议,告别“黑盒库“的嵌入式实战指南
c语言·数据库·重构
l***061 小时前
Ubuntu 系统下安装 Nginx
数据库·nginx·ubuntu
6***S2221 小时前
SQL Server Management Studio的使用
数据库·oracle·性能优化
合作小小程序员小小店2 小时前
桌面开发,拼车管理系统开发,基于C#,winform,sql server数据库
开发语言·数据库·sql·microsoft·c#
百***49009 小时前
SQL Server查看数据库中每张表的数据量和总数据量
数据库·sql·oracle
代码or搬砖10 小时前
MyBatisPlus中的常用注解
数据库·oracle·mybatis