如何清理sys.conversation_endpoints

时间:2009-10-12 10:06:31

标签: sql-server-2005 msmq

我有一个表,使用服务代理实现的表上的触发器。每天有超过五十万条记录插入到表格中。

异步SP用于通过使用插入的数据检查几个条件并更新其他表。它在过去的1个月里运行良好,SP在执行记录的2-3秒内被执行。但现在需要90多分钟。

目前sys.conversation_endpoints记录太多。

(请注意,所有表每天都会被截断,因为我不需要那些记录)

其他数据库活动正常(平均60%的CPU利用率)。

现在我需要看看?

我可以毫无问题地重新创建数据库,但我不认为这是解决问题的好方法

3 个答案:

答案 0 :(得分:2)

你可以'结束对话@handle'或'结束对话@handle with cleanup'

答案 1 :(得分:0)

用于结束特定服务的所有对话的脚本:

DECLARE @sql NVARCHAR(MAX)
       ,@far_service NVARCHAR(256) = N'far_service_name';

WHILE EXISTS (
                 SELECT COUNT(*)
                 FROM   sys.conversation_endpoints
                 WHERE  far_service = @far_service
             )
BEGIN
    SET @sql = N'';
    SELECT TOP 1000 @sql = @sql + N'END CONVERSATION ''' + CAST(conversation_handle AS NVARCHAR(50)) + N''' WITH CLEANUP;
'
    FROM   sys.conversation_endpoints
    WHERE  far_service = @far_service;

    --PRINT @sql;
    EXEC sys.sp_executesql @stmt = @sql;

    --RETURN;
END;
GO

答案 2 :(得分:0)

在修改@thesqldev中的代码后,这是我的解决方案

USE [MY_DATABASE_NAME_HERE]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[mqConversationsClearAll]

AS

-- Note: you can check the queue by running this query
-- SELECT * FROM sys.conversation_endpoints GO


DECLARE @getid CURSOR
        ,@sql NVARCHAR(MAX)
        ,@conv_id NVARCHAR(100)
        ,@conv_handle NVARCHAR(100)
        ,@conv_service NVARCHAR(100)
        -- ,@far_service NVARCHAR(256) = N'One_Specific_Service_Target';

-- want to create and execute a chain of statements like this, one per conversation
-- END CONVERSATION 'FE851F37-218C-EA11-B698-4CCC6AD00AE9' WITH CLEANUP;
-- END CONVERSATION 'A4B4F603-208C-EA11-B698-4CCC6AD00AE9' WITH CLEANUP;    

SET @getid = CURSOR FOR
                SELECT [conversation_id], [conversation_handle], [far_service]
                FROM   sys.conversation_endpoints
                WHERE NOT([State] = 'CO') 
                -- CO = CONVERSING [State]
                -- alternatively, might like one service name only
                -- WHERE  far_service = @far_service

OPEN @getid

    FETCH NEXT
    FROM @getid INTO @conv_id, @conv_handle, @conv_service
    WHILE @@FETCH_STATUS = 0
        BEGIN
            SET @sql = 'END CONVERSATION ' + char(39) + @conv_handle + char(39) + ' WITH CLEANUP;'
            EXEC sys.sp_executesql @stmt = @sql;
            FETCH NEXT
            FROM @getid INTO @conv_id, @conv_handle, @conv_service
        END

CLOSE @getid
DEALLOCATE @getid

然后,每当您要清除sys.conversation_endpoints时,都可以执行新创建的存储过程“ mqConversationsClearAll”