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

相关推荐
涛粒子26 分钟前
Redis 生产问题(重要)
数据库·redis·缓存
勿栀枝31 分钟前
sql多表联查图文
数据库·sql
LaLaLa_OvO1 小时前
ORACLE SQL思路: 多行数据有相同字段就合并成一条数据 分页展示
数据库·sql·oracle
晴天Y282 小时前
源码编译安装MySQL
数据库·mysql
fmc1211042 小时前
【1】数据分析基础(一些概念)
服务器·数据库·oracle
时之彼岸Φ2 小时前
操作系统:文件系统
运维·数据库
With Order @!1472 小时前
如何在项目中使用人大金仓替换mysql
数据库·mysql
散修-小胖子2 小时前
MySQL常用运维操作(一):快速复制一张表
数据库·mysql
CT随3 小时前
MySQL查询缓存详解
数据库·mysql·缓存
PersistJiao4 小时前
使用 Temporal 管理和调度 Couchbase SQL 脚本的实际例子
数据库·sql