当前位置:网站首页>Sqlserver regularly backup database and regularly kill database deadlock solution
Sqlserver regularly backup database and regularly kill database deadlock solution
2022-07-05 10:38:00 【Full stack programmer webmaster】
Last Friday, the team leader told me to kill data The deadlock process of the Library , Sometimes writing to the database at the same time will cause this situation , Because I'm not very familiar with databases , Suddenly, the team leader said, and I decided to do something about it , How else can I improve myself ? Not now , Maybe we should study it next time , After pouring it out, it can be used next time , Later, the group leader added :” There is also the problem of regularly backing up the database ”, Do as you say
PS:Sqlserver 2008 R2,windows 8 64 position
1. Backup database
Because you need to back up , We're going to use Sqlserver Agent for , The agent of the default database is not enabled . We need to start it manually .
Execute the backup database script , Now publish the script , In fact, the backup can be realized by replacing the file path and database name that need to be saved in this code . But the purpose of scheduled backup has not been achieved
-- Automatically back up and save the latest 5 Days of SQL Database job script
DECLARE @filename VARCHAR(255)
DECLARE @date DATETIME
SELECT @date=GETDATE()
SELECT @filename = 'G:\ Storage location \ Database name -'+CAST(DATEPART(yyyy,@date) as varchar)+'-'+CAST(DATEPART(mm,@date) as varchar)+'-'+CAST(DATEPART(dd,@date) as varchar)+'.bak'
BACKUP DATABASE [ Database name ] TO DISK = @filename WITH INIT
GO
DECLARE @OLDDATE DATETIME
SELECT @OLDDATE=GETDATE()-5
EXECUTE master.dbo.xp_delete_file 0,N'G:\ Storage location ',N'bak',@olddate,12. Regularly back up the specified database
Just opened Sqlserver Agency service , In fact, my own understanding is a timer , Keep performing some tasks assigned by the operator , It feels like an alarm clock , Look at my demonstration steps
First step
The second step
The third step
Step four
Step five
The above steps are to complete the function of regularly backing up the specified database !
**************************************************************************
1. Kill the database deadlock process
Next, I will introduce some methods to kill the database deadlock process
I spent a long time in the afternoon looking for many articles to read , I found that many of them were used master Medium sys.sysprocesses surface (http://msdn.microsoft.com/zh-cn/library/ms179881(SQL.90).aspx) Here attached msdn Explanation of this table , If you don't understand it, please refer to the meaning of each table field .
Refer to the opinions on the Internet , Most of them write a stored procedure in master In the database , Then use the way of homework Kill the deadlock process regularly Of , I think this method is feasible !
Here are stored procedures SQL sentence
-- Database deadlock resolution , Combined with homework ( Baidu ) Realize the process of regularly clearing database deadlock , Stored procedures are placed in master In the database
USE master
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
CREATE PROCEDURE sp_who_lock
AS
BEGIN
DECLARE @spid INT ,
@bl INT ,
@intTransactionCountOnEntry INT ,
@intRowcount INT ,
@intCountProperties INT ,
@intCounter INT
CREATE TABLE #tmp_lock_who
(
id INT IDENTITY(1, 1) ,
spid SMALLINT ,
bl SMALLINT
)
IF @@ERROR <> 0
RETURN @@ERROR
INSERT INTO #tmp_lock_who ( spid, bl )
SELECT 0, blocked
FROM ( SELECT *
FROM sys.sysprocesses
WHERE blocked > 0
) a
WHERE NOT EXISTS ( SELECT *
FROM ( SELECT *
FROM sys.sysprocesses
WHERE blocked > 0
) b
WHERE a.blocked = spid )
UNION
SELECT spid, blocked
FROM sys.sysprocesses
WHERE blocked > 0
IF @@ERROR <> 0
RETURN @@ERROR
-- Find the number of records in the temporary table
SELECT @intCountProperties = COUNT(*), @intCounter = 1
FROM #tmp_lock_who
IF @@ERROR <> 0
RETURN @@ERROR
IF @intCountProperties = 0
SELECT N' There is no blocking or deadlock information ' AS message
-- Loop start
WHILE @intCounter <= @intCountProperties
BEGIN
-- Take the first record
SELECT @spid = spid, @bl = bl
FROM #tmp_lock_who
WHERE Id = @intCounter
BEGIN
IF @spid = 0
SELECT N' The cause of database deadlock is : ' + CAST(@bl AS VARCHAR(10))
+ N' Process number , It carries out SQL The grammar is as follows '
ELSE
SELECT N' Process number SPID:' + CAST(@spid AS VARCHAR(10))
+ N' By process number SPID:' + CAST(@bl AS VARCHAR(10)) N' Blocking , The execution of its current process SQL The grammar is as follows '
DBCC INPUTBUFFER (@bl )
END
-- Loop pointer down
SET @intCounter = @intCounter + 1
END
DROP TABLE #tmp_lock_who
RETURN 0
END
goabove sql The execution of the statement is completed in master The database generates stored procedures , The calling code is simple
-- perform
EXEC sp_who_lock
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GOAfter the call is completed, all deadlock processes in the database can be killed
Digression : How to determine whether those processes are deadlock processes , This is the key point , stay msdn Chinese vs sys.sysprocesses There is a field in the table blocked This field , Remove all larger than 0 Is the deadlock process data of the database , And then use KILL+SPID( process ID) Execution can kill the deadlock process , These are the main ideas .
2. Kill the database deadlock process regularly
For regularly killing database deadlock processes , Here are two points to note
1. Order of execution , Now? master Database creation stored procedure , Then create the job
2. The execution code of the job , The executing code is to call the stored procedure that kills the deadlock process ( Of the calling stored procedure sql sentence , Cannot include delete stored procedures (DROP xxx) Such a statement is not good , When the job is first executed , The second time, you will report a mistake , It will never succeed
First step
You need to execute the above stored procedure to kill the deadlock process , Only master This stored procedure exists in the database , Can call this stored procedure in the form of a job , Otherwise, the call fails *
The second step
Create a new job , Will just call the... Of the stored procedure SQL Put the statement into New job => Step options => command , Medium will do . The last step Implementation plan Set it according to the actual situation
About Sqlserver The two difficulties of the database were also absorbed by myself this afternoon . In the future, you can also easily deal with similar problems
copyright , If you break the law, you will be prosecuted .
Please explain the original link
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/109840.html Link to the original text :https://javaforall.cn
边栏推荐
- 九度 1480:最大上升子序列和(动态规划思想求最值)
- 风控模型启用前的最后一道工序,80%的童鞋在这都踩坑
- C#实现获取DevExpress中GridView表格进行过滤或排序后的数据
- 括号匹配问题(STL)
- 跨页面通讯
- [dark horse morning post] Luo Yonghao responded to ridicule Oriental selection; Dong Qing's husband Mi Chunlei was executed for more than 700million; Geely officially acquired Meizu; Huawei releases M
- 各位大佬,我测试起了3条线程同时往3个mysql表中写入,每条线程分别写入100000条数据,用了f
- Learning Note 6 - satellite positioning technology (Part 1)
- Idea create a new sprintboot project
- Singleton mode encapsulates activity management class
猜你喜欢

Blockbuster: the domestic IDE is released, developed by Alibaba, and is completely open source!

WorkManager学习一
![[vite] 1371 - develop vite plug-ins by hand](/img/7f/84bba39965b4116f20b1cf8211f70a.png)
[vite] 1371 - develop vite plug-ins by hand

微信核酸检测预约小程序系统毕业设计毕设(8)毕业设计论文模板

Ad20 make logo

Apple 5g chip research and development failure? It's too early to get rid of Qualcomm

Idea create a new sprintboot project

【观察】跨境电商“独立站”模式崛起,如何抓住下一个红利爆发时代?

Have you learned to make money in Dingding, enterprise micro and Feishu?

2022年T电梯修理操作证考试题及答案
随机推荐
BOM//
csdn软件测试入门的测试基本流程
Solution to the length of flex4 and Flex3 combox drop-down box
Have you learned to make money in Dingding, enterprise micro and Feishu?
ModuleNotFoundError: No module named ‘scrapy‘ 终极解决方式
图片懒加载的方案
vscode的快捷键
C function returns multiple value methods
括号匹配问题(STL)
Universal double button or single button pop-up
How do programmers live as they like?
Atcoder beginer contest 254 "e BFS" f st table maintenance differential array GCD "
Activity jump encapsulation
Secteur non technique, comment participer à devops?
AD20 制作 Logo
小程序框架Taro
风控模型启用前的最后一道工序,80%的童鞋在这都踩坑
C语言活期储蓄账户管理系统
【tcp】服务器上tcp连接状态json形式输出
Error: module not found: error: can't resolve 'xxx' in 'XXXX‘