{"id":449,"date":"2025-06-24T13:06:58","date_gmt":"2025-06-24T10:06:58","guid":{"rendered":"https:\/\/kb.astrocroc.com\/?p=449"},"modified":"2025-06-24T13:06:58","modified_gmt":"2025-06-24T10:06:58","slug":"%d0%ba%d0%b0%d0%ba-%d0%b0%d0%b2%d1%82%d0%be%d0%bc%d0%b0%d1%82%d0%b8%d1%87%d0%b5%d1%81%d0%ba%d0%b8-%d0%b7%d0%b0%d0%b2%d0%b5%d1%80%d1%88%d0%b0%d1%82%d1%8c-%d0%b7%d0%b0%d0%b1%d0%bb%d0%be%d0%ba%d0%b8","status":"publish","type":"post","link":"https:\/\/kb.astrocroc.com\/?p=449","title":{"rendered":"\u041a\u0430\u043a \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u0442\u044c \u0437\u0430\u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u0441\u0435\u0441\u0441\u0438\u0438 \u043f\u043e \u0442\u0430\u0439\u043c\u0430\u0443\u0442\u0443 \u0432 MSSQL \u0441 \u043b\u043e\u0433\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435\u043c"},"content":{"rendered":"\n<p>\u0414\u043b\u044f \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f \u0437\u0430\u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u0441\u0435\u0441\u0441\u0438\u0439 \u043f\u043e \u0442\u0430\u0439\u043c\u0430\u0443\u0442\u0443 \u0441 \u043b\u043e\u0433\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0439 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0439\u0442\u0435 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435:<\/p>\n\n\n\n<p>\u0421\u043a\u0440\u0438\u043f\u0442 \u0434\u043b\u044f \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f \u0441\u0435\u0441\u0441\u0438\u0439 \u0438 \u043b\u043e\u0433\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f:<\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-sql\" data-line=\"\">sql\nDECLARE @KillLog TABLE (\n    SPID INT,\n    DatabaseName NVARCHAR(128),\n    LoginName NVARCHAR(128),\n    HostName NVARCHAR(128),\n    ProgramName NVARCHAR(128),\n    WaitTimeMS INT,\n    KillTime DATETIME DEFAULT GETDATE(),\n    SQLText NVARCHAR(MAX)\n)\n\nDECLARE @spid INT, @sql_handle VARBINARY(64), @dbid INT\n\nDECLARE cur CURSOR FOR\nSELECT \n    p.blocked, \n    p.sql_handle, \n    p.dbid\nFROM sys.sysprocesses p\nWHERE \n    p.blocked &gt; 0 \n    AND p.waittime &gt; 300000 -- 5 \u043c\u0438\u043d\u0443\u0442\n\nOPEN cur\nFETCH NEXT FROM cur INTO @spid, @sql_handle, @dbid\n\nWHILE @@FETCH_STATUS = 0\nBEGIN\n    INSERT INTO @KillLog (SPID, DatabaseName, LoginName, HostName, ProgramName, WaitTimeMS, SQLText)\n    SELECT\n        @spid,\n        DB_NAME(@dbid),\n        loginame,\n        hostname,\n        program_name,\n        waittime,\n        (SELECT text FROM sys.dm_exec_sql_text(@sql_handle))\n    FROM sys.sysprocesses WHERE spid = @spid\n\n    EXEC(&#039;KILL &#039; + @spid)\n    \n    FETCH NEXT FROM cur INTO @spid, @sql_handle, @dbid\nEND\n\nCLOSE cur\nDEALLOCATE cur\n\n-- \u0421\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u043b\u043e\u0433\u0430 \u0432 \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u0443\u044e \u0442\u0430\u0431\u043b\u0438\u0446\u0443\nINSERT INTO dbo.BlockedSessionsLog \nSELECT * FROM @KillLog\n<\/code><\/pre>\n\n\n\n<p>\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0434\u043b\u044f \u043b\u043e\u0433\u043e\u0432:<\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-sql\" data-line=\"\">CREATE TABLE dbo.BlockedSessionsLog (\n    LogID INT IDENTITY(1,1) PRIMARY KEY,\n    SPID INT NOT NULL,\n    DatabaseName NVARCHAR(128) NULL,\n    LoginName NVARCHAR(128) NULL,\n    HostName NVARCHAR(128) NULL,\n    ProgramName NVARCHAR(128) NULL,\n    WaitTimeMS INT NULL,\n    KillTime DATETIME NOT NULL DEFAULT GETDATE(),\n    SQLText NVARCHAR(MAX) NULL\n)<\/code><\/pre>\n\n\n\n<p>\u0410\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0437\u0430\u0446\u0438\u044f \u0447\u0435\u0440\u0435\u0437 SQL Server Agent:<\/p>\n\n\n\n<ol start=\"1\" class=\"wp-block-list\">\n<li>\u0421\u043e\u0437\u0434\u0430\u0439\u0442\u0435 \u043d\u043e\u0432\u043e\u0435 \u0437\u0430\u0434\u0430\u043d\u0438\u0435 \u0432 SQL Server Agent<\/li>\n\n\n\n<li>\u0414\u043e\u0431\u0430\u0432\u044c\u0442\u0435 \u0448\u0430\u0433 \u0441 \u0442\u0438\u043f\u043e\u043c &#171;Transact-SQL script&#187; \u0438 \u0432\u0441\u0442\u0430\u0432\u044c\u0442\u0435 \u0441\u043a\u0440\u0438\u043f\u0442 \u0438\u0437 \u043f\u0443\u043d\u043a\u0442\u0430 1<\/li>\n\n\n\n<li>\u041d\u0430\u0441\u0442\u0440\u043e\u0439\u0442\u0435 \u0440\u0430\u0441\u043f\u0438\u0441\u0430\u043d\u0438\u0435 (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043a\u0430\u0436\u0434\u044b\u0435 5 \u043c\u0438\u043d\u0443\u0442)<\/li>\n\n\n\n<li>\u0423\u0431\u0435\u0434\u0438\u0442\u0435\u0441\u044c, \u0447\u0442\u043e \u0443\u0447\u0435\u0442\u043d\u0430\u044f \u0437\u0430\u043f\u0438\u0441\u044c \u0430\u0433\u0435\u043d\u0442\u0430 \u0438\u043c\u0435\u0435\u0442 \u043f\u0440\u0430\u0432\u0430:\n<ul class=\"wp-block-list\">\n<li>VIEW SERVER STATE<\/li>\n\n\n\n<li>KILL \u0440\u0430\u0437\u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u043d\u0430 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u044b<\/li>\n\n\n\n<li>\u0417\u0430\u043f\u0438\u0441\u044c \u0432 \u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u043b\u043e\u0433\u043e\u0432<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<p>\u041c\u043e\u043d\u0438\u0442\u043e\u0440\u0438\u043d\u0433 \u0438 \u0430\u043d\u0430\u043b\u0438\u0437:<\/p>\n\n\n\n<p>\u0414\u043b\u044f \u043f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u0430 \u0441\u043d\u044f\u0442\u044b\u0445 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043e\u043a:<\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-sql\" data-line=\"\">SELECT * FROM dbo.BlockedSessionsLog\nORDER BY KillTime DESC<\/code><\/pre>\n\n\n\n<p>\u041a\u043b\u044e\u0447\u0435\u0432\u044b\u0435 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u044b \u043b\u043e\u0433\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f:<\/p>\n\n\n\n<ol start=\"1\" class=\"wp-block-list\">\n<li><strong>\u0418\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f \u043e \u0441\u0435\u0441\u0441\u0438\u0438<\/strong>:\n<ul class=\"wp-block-list\">\n<li>\u0418\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430 (SPID)<\/li>\n\n\n\n<li>\u0418\u043c\u044f \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445<\/li>\n\n\n\n<li>\u041b\u043e\u0433\u0438\u043d \u0438 \u0445\u043e\u0441\u0442<\/li>\n\n\n\n<li>\u041d\u0430\u0437\u0432\u0430\u043d\u0438\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>\u0414\u0435\u0442\u0430\u043b\u0438 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0438<\/strong>:\n<ul class=\"wp-block-list\">\n<li>\u0412\u0440\u0435\u043c\u044f \u043e\u0436\u0438\u0434\u0430\u043d\u0438\u044f (\u043c\u0441)<\/li>\n\n\n\n<li>\u0422\u0435\u043a\u0441\u0442 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0433\u043e SQL-\u0437\u0430\u043f\u0440\u043e\u0441\u0430<\/li>\n\n\n\n<li>\u0412\u0440\u0435\u043c\u044f \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>\u0418\u0441\u0442\u043e\u0447\u043d\u0438\u043a \u0434\u0430\u043d\u043d\u044b\u0445<\/strong>:\n<ul class=\"wp-block-list\">\n<li>sys.sysprocesses \u0434\u043b\u044f \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 \u0437\u0430\u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u0441\u0435\u0441\u0441\u0438\u0439<\/li>\n\n\n\n<li>sys.dm_exec_sql_text \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0442\u0435\u043a\u0441\u0442\u0430 \u0437\u0430\u043f\u0440\u043e\u0441\u0430<\/li>\n\n\n\n<li>\u0412\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u0434\u043b\u044f \u043f\u0440\u043e\u043c\u0435\u0436\u0443\u0442\u043e\u0447\u043d\u043e\u0433\u043e \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<p>\u0412\u0430\u0436\u043d\u044b\u0435 \u0437\u0430\u043c\u0435\u0447\u0430\u043d\u0438\u044f:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u041d\u0430\u0441\u0442\u0440\u043e\u0439\u0442\u0435 \u043f\u043e\u0440\u043e\u0433 waittime \u0441\u043e\u0433\u043b\u0430\u0441\u043d\u043e \u0442\u0440\u0435\u0431\u043e\u0432\u0430\u043d\u0438\u044f\u043c (\u0432 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 300 000 \u043c\u0441 = 5 \u043c\u0438\u043d\u0443\u0442)<\/li>\n\n\n\n<li>\u0420\u0435\u0433\u0443\u043b\u044f\u0440\u043d\u043e \u0430\u0440\u0445\u0438\u0432\u0438\u0440\u0443\u0439\u0442\u0435 \u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u043b\u043e\u0433\u043e\u0432 \u043f\u0440\u0438 \u0431\u043e\u043b\u044c\u0448\u043e\u043c \u043e\u0431\u044a\u0435\u043c\u0435 \u0434\u0430\u043d\u043d\u044b\u0445<\/li>\n\n\n\n<li>\u0414\u043e\u0431\u0430\u0432\u044c\u0442\u0435 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u043d\u0430 \u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u043b\u043e\u0433\u043e\u0432 \u0434\u043b\u044f \u0443\u0441\u043a\u043e\u0440\u0435\u043d\u0438\u044f \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432:<\/li>\n\n\n\n<li>sql<\/li>\n\n\n\n<li><strong>CREATE<\/strong> <strong>INDEX<\/strong> IX_KillTime <strong>ON<\/strong> dbo.BlockedSessionsLog(KillTime)<\/li>\n\n\n\n<li><strong>CREATE<\/strong> <strong>INDEX<\/strong> IX_SPID <strong>ON<\/strong> dbo.BlockedSessionsLog(SPID)<\/li>\n\n\n\n<li>\u0414\u043b\u044f \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u0430\u043d\u0430\u043b\u0438\u0437\u0430 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u0442\u0435 \u043e\u043f\u043e\u0432\u0435\u0449\u0435\u043d\u0438\u044f \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 \u0442\u0430\u0431\u043b\u0438\u0446\u0435 \u043b\u043e\u0433\u043e\u0432<\/li>\n<\/ul>\n\n\n\n<p>\u042d\u0442\u043e \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0438\u0432\u0430\u0435\u0442 \u043f\u0440\u043e\u0437\u0440\u0430\u0447\u043d\u044b\u0439 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u044c \u043d\u0430\u0434 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u043c \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u0435\u043c \u0437\u0430\u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u0441\u0435\u0441\u0441\u0438\u0439 \u0441 \u0434\u0435\u0442\u0430\u043b\u044c\u043d\u044b\u043c \u0430\u0443\u0434\u0438\u0442\u043e\u043c \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0439 \u0434\u043b\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e \u0430\u043d\u0430\u043b\u0438\u0437\u0430 \u043f\u0440\u043e\u0431\u043b\u0435\u043c \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u0414\u043b\u044f \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f \u0437\u0430\u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u0441\u0435\u0441\u0441\u0438\u0439 \u043f\u043e \u0442\u0430\u0439\u043c\u0430\u0443\u0442\u0443 \u0441 \u043b\u043e\u0433\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0439 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0439\u0442\u0435 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435: \u0421\u043a\u0440\u0438\u043f\u0442 \u0434\u043b\u044f \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f \u0441\u0435\u0441\u0441\u0438\u0439 \u0438 \u043b\u043e\u0433\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f: \u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0434\u043b\u044f \u043b\u043e\u0433\u043e\u0432: \u0410\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0437\u0430\u0446\u0438\u044f \u0447\u0435\u0440\u0435\u0437 SQL Server Agent: \u041c\u043e\u043d\u0438\u0442\u043e\u0440\u0438\u043d\u0433 \u0438 \u0430\u043d\u0430\u043b\u0438\u0437: \u0414\u043b\u044f \u043f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u0430 \u0441\u043d\u044f\u0442\u044b\u0445 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043e\u043a: \u041a\u043b\u044e\u0447\u0435\u0432\u044b\u0435 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u044b \u043b\u043e\u0433\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f: \u0412\u0430\u0436\u043d\u044b\u0435 \u0437\u0430\u043c\u0435\u0447\u0430\u043d\u0438\u044f: \u042d\u0442\u043e \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0438\u0432\u0430\u0435\u0442 \u043f\u0440\u043e\u0437\u0440\u0430\u0447\u043d\u044b\u0439 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u044c \u043d\u0430\u0434 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u043c \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u0435\u043c \u0437\u0430\u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u0441\u0435\u0441\u0441\u0438\u0439 \u0441 \u0434\u0435\u0442\u0430\u043b\u044c\u043d\u044b\u043c \u0430\u0443\u0434\u0438\u0442\u043e\u043c \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0439 \u0434\u043b\u044f [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11,59],"tags":[33,61],"class_list":["post-449","post","type-post","status-publish","format-standard","hentry","category-sharepoint","category-sql","tag-sharepoint","tag-sql"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/kb.astrocroc.com\/index.php?rest_route=\/wp\/v2\/posts\/449","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kb.astrocroc.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kb.astrocroc.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kb.astrocroc.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kb.astrocroc.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=449"}],"version-history":[{"count":1,"href":"https:\/\/kb.astrocroc.com\/index.php?rest_route=\/wp\/v2\/posts\/449\/revisions"}],"predecessor-version":[{"id":450,"href":"https:\/\/kb.astrocroc.com\/index.php?rest_route=\/wp\/v2\/posts\/449\/revisions\/450"}],"wp:attachment":[{"href":"https:\/\/kb.astrocroc.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=449"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kb.astrocroc.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=449"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kb.astrocroc.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=449"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}