MS SQL收缩数据库日志文件(db具有MEMORY_OPTIMIZED_DATA)

时间:2019-04-18 09:56:44

标签: sql-server shrink

请参阅示例。有2个测试数据库。唯一的区别是,第二个文件对MEMORY_OPTIMIZED_DATA使用FILEGROUP,并且创建了一个带有(MEMORY_OPTIMIZED = ON)的表。但我什至不将数据粘贴到该表中

下面列出了用于创建数据库的完整脚本。所有2个测试数据库都具有完全恢复模式。我正在执行完整的数据库备份,将一些数据粘贴到测试表中,然后,我正在执行BACKUP LOG并看到log_reuse_wait_desc = Nothing,但是从dm_db_log_info中我看到在第一个数据库中几乎所有VLF文件都被截断了,但是对于第二个仍然有vlf_active = 1

逐步

1)

select @@VERSION
--returns
--Microsoft SQL Server 2017 (RTM) - 14.0.1000.169 (X64)   Aug 22 2017 17:04:49   Copyright (C) 2017 Microsoft Corporation  Enterprise Edition: Core-based Licensing (64-bit) on Windows 10 Enterprise 10.0 <X64> (Build 17134: ) 

2)创建2个测试数据库 创建数据库LogShrinkTest1

CREATE TABLE LogShrinkTest1.[dbo].[Table_1](
    [id] [int] IDENTITY(1,1) NOT NULL,
    [val] [varchar](50) NULL,
    [val2] [varchar](50) NULL,
 CONSTRAINT [PK_Table_1] PRIMARY KEY CLUSTERED 
(
    [id] ASC
)
)

CREATE NONCLUSTERED INDEX [ix_1] ON LogShrinkTest1.[dbo].[Table_1]([val2])



create database LogShrinkTest2

CREATE TABLE LogShrinkTest2.[dbo].[Table_1](
    [id] [int] IDENTITY(1,1) NOT NULL,
    [val] [varchar](50) NULL,
    [val2] [varchar](50) NULL,
 CONSTRAINT [PK_Table_1] PRIMARY KEY CLUSTERED 
(
    [id] ASC
)
)
GO
CREATE NONCLUSTERED INDEX [ix_1] ON LogShrinkTest2.[dbo].[Table_1]([val2])

3)进行完整备份

BACKUP DATABASE LogShrinkTest1 TO  DISK = N'C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQLSERVER\MSSQL\Backup\LogShrinkTest1_FULL.bak' WITH NOFORMAT, NOINIT,  NAME = N'LogShrinkTest1-Full Database Backup', SKIP, NOREWIND, NOUNLOAD,  STATS = 10
go
BACKUP DATABASE LogShrinkTest2 TO  DISK = N'C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQLSERVER\MSSQL\Backup\LogShrinkTest2_FULL.bak' WITH NOFORMAT, NOINIT,  NAME = N'LogShrinkTest2-Full Database Backup', SKIP, NOREWIND, NOUNLOAD,  STATS = 10

4)在2 db上创建内存优化表

ALTER DATABASE LogShrinkTest2 ADD FILEGROUP LogShrinkTest2_MO CONTAINS MEMORY_OPTIMIZED_DATA   
    go
ALTER DATABASE LogShrinkTest2 ADD FILE (name='LogShrinkTest2_mo_data', filename='E:\SQL SERV Backups\LogShrinkTest2_mo_data') TO FILEGROUP LogShrinkTest2_MO  



CREATE TABLE LogShrinkTest2.dbo.Table_2 (   
    id INT IDENTITY(1,1) PRIMARY KEY NONCLUSTERED,  
    UserId INT  , 
    CreatedDate DATETIME2  NULL,   
    TotalPrice MONEY  
    ) WITH (MEMORY_OPTIMIZED=ON)    

5)将一些数据插入表_1

insert into LogShrinkTest1.dbo.table_1 default values
go 100000

update LogShrinkTest1.dbo.table_1 set val='xxx1' , val2 ='zzz1' where val is null


insert into LogShrinkTest2.dbo.table_1 default values
go 100000

update LogShrinkTest2.dbo.table_1 set val='xxx1' , val2 ='zzz1' where val is null

6)检查VLF截断是否需要日志备份,因为我们处于完全重新协商模式

select recovery_model_desc
          ,log_reuse_wait_desc 
          ,log_reuse_wait
          ,* 
from master.sys.databases
where name in ( 'LogShrinkTest1','LogShrinkTest2')

结果是log_reuse_wait_desc = LOG_BACKUP

7)注意,大多数VLF现在都处于活动状态。

declare @curDb int=db_id()
    select * ,sum(vlf_size_mb) over ()
    from sys.dm_db_log_info ( @curDb ) 

到目前为止,两个数据库的图片相同

8)日志备份

BACKUP LOG LogShrinkTest1 TO  DISK = N'C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQLSERVER\MSSQL\Backup\log_for_LogShrinkTest1.bak' WITH NOFORMAT, NOINIT,  NAME = N'LogShrinkTest1-Log Backup', SKIP, NOREWIND, NOUNLOAD,  STATS = 10

go

BACKUP LOG LogShrinkTest2 TO  DISK = N'C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQLSERVER\MSSQL\Backup\log_for_LogShrinkTest2.bak' WITH NOFORMAT, NOINIT,  NAME = N'LogShrinkTest2-Log Backup', SKIP, NOREWIND, NOUNLOAD,  STATS = 10

9)检查VLF

declare @curDb int=db_id()
select * ,sum(vlf_size_mb) over ()
from sys.dm_db_log_info ( @curDb ) 

对于1 db,我们几乎所有VLF文件都被截断了! 但对于第二个数据库,它们仍然为vlf_active = 1!

log_reuse_wait_desc =所有测试数据库均不执行任何操作 并且DBCC OPENTRAN不返回任何有效的未清交易。

此问题的原因是什么?导致日志文件增长,直到它填满所有磁盘空间。

0 个答案:

没有答案