0% found this document useful (0 votes)
36 views3 pages

Kill Stale Version Store Connection

Uploaded by

Pranab Knj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views3 pages

Kill Stale Version Store Connection

Uploaded by

Pranab Knj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

SQL Server VERSION Store clean up

This job will run as per schedule and kill stale unused
connection that causes version store to grow in tempdb. It
will log the details of killing connection in MSDB for
tracking. By default it checks connection older than 2 hours
, part of active snapshot transactions and
sleeping/suspended state. You can modify the conditions as
per your requirements.
USE msdb

CREATE TABLE [dbo].[trackkillingprocess](


[spid] [int] NULL,
[actualquery] [varchar](2000) NULL,
[cpu] [int] NULL,
[physical_io] [int] NULL,
[login_time] [datetime] NULL,
[last_batch] [datetime] NULL,
[open_tran] [tinyint] NULL,
[status] [varchar](100) NULL,
[hostname] [varchar](100) NULL,
[program_name] [varchar](150) NULL,
[cmd] [varchar](100) NULL,
[loginame] [varchar](100) NULL,
[dateadded] [datetime] NOT NULL,
PRIMARY KEY CLUSTERED
(
[dateadded] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF,
ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO

DECLARE @ReturnCode INT = 0


DECLARE @jobId BINARY(16)
EXEC @ReturnCode = msdb.dbo.sp_add_job @job_name=N'DBA_KillVersionStoreSleepingQuery',
@enabled=1,
@notify_level_eventlog=0,
@notify_level_email=0,
@notify_level_netsend=0,
@notify_level_page=0,
@delete_level=0,
@description=N'No description available.',
@category_name=N'[Uncategorized (Local)]',
@owner_login_name=N'sa', @job_id = @jobId OUTPUT
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback

EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'Kill query',


@step_id=1,
@cmdexec_success_code=0,
@on_success_action=1,
@on_success_step_id=0,
@on_fail_action=2,
@on_fail_step_id=0,
@retry_attempts=2,
@retry_interval=1,
@os_run_priority=0, @subsystem=N'TSQL',
@command=N'While(
(
( select count(1)
from sys.sysprocesses sp
inner join sys.dm_tran_active_snapshot_database_transactions asdt on
asdt.session_id = sp.spid
--cross apply sys.dm_exec_input_buffer ( sp.spid , NULL ) deib
where sp.spid > 50 and sp.dbid = db_id(''YourDBName'') and sp.open_tran= 1
and sp.login_time < dateadd(HH,-2,getdate())
and asdt.elapsed_time_seconds > 1200
and sp.status in (''sleeping'',''suspended '')
)>0
and
(
100 < (select SUM ((version_store_reserved_page_count)*8.0 /1024.0/1024.0)
from [tempdb].[sys].[dm_db_file_space_usage]
)
)
)
)
begin
if 100 < (select SUM ((version_store_reserved_page_count)*8.0 /1024.0/1024.0)
from [tempdb].[sys].[dm_db_file_space_usage]
)
begin
insert into msdb..trackkillingprocess
select top 1 spid , left(deib.event_info,2000)
,sp.cpu,sp.physical_io,sp.login_time,sp.last_batch,sp.open_tran,
left(sp.status,100),left(sp.hostname,100),left(sp.program_name,150),left(sp.cmd,10
0),left(sp.loginame,100),getdate()
from sys.sysprocesses sp
inner join sys.dm_tran_active_snapshot_database_transactions asdt on
asdt.session_id = sp.spid
cross apply sys.dm_exec_input_buffer ( sp.spid , NULL ) deib
where sp.spid > 50 and sp.dbid = db_id(''yourdbname'') and sp.open_tran= 1 and
sp.login_time < dateadd(HH,-2,getdate())
and asdt.elapsed_time_seconds > 1200
and sp.status in (''sleeping'',''suspended '')
order by sp.cpu desc, sp.physical_io desc,sp.last_batch desc

declare @killstmt varchar(30)

select top 1 @killstmt= ''kill ''+cast(spid as varchar(10))


from msdb..[trackkillingprocess]
order by dateadded desc

exec (@killstmt)

WAITFOR DELAY ''00:00:07'';


end

end',
@database_name=N'msdb',
@flags=0
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_update_job @job_id = @jobId, @start_step_id = 1
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_add_jobschedule @job_id=@jobId, @name=N'every 10 mins',
@enabled=1,
@freq_type=4,
@freq_interval=1,
@freq_subday_type=4,
@freq_subday_interval=10,
@freq_relative_interval=0,
@freq_recurrence_factor=0,
@active_start_date=20230406,
@active_end_date=99991231,
@active_start_time=0,
@active_end_time=235959,
@schedule_uid=N'0fc8bf7e-e17f-4c8f-ab27-39d81a8132cf'
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_add_jobserver @job_id = @jobId, @server_name = N'(local)'
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
COMMIT TRANSACTION
GOTO EndSave
QuitWithRollback:
IF (@@TRANCOUNT > 0) ROLLBACK TRANSACTION
EndSave:
GO

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy