I have Service Broker queue infrastructure that I am using in my database for a few months. I realized that my Initiator
queue has reached to 2 million records which are EndDialog
messages. So I re-designed it thanks to this link .
My problem is that I couldn't delete those 2 million records. I used the approach below as indicated in this link , and left the query executing. It executed for 20 hours until I canceled it.
declare @c uniqueidentifier
while(1=1)
begin
select top 1 @c = conversation_handle from dbo.queuename
if (@@ROWCOUNT = 0)
break
end conversation @c with cleanup
end
Now I am trying to drop Service and Queue but it seems it is gonna take lots of time again.
drop service initiatorService
drop queue initiatorQueue
Is there another way to delete immediately?
There is no shortcut to achieve your goal that I know of, unless you are willing to:
ALTER DATABASE ... SET NEW_BROKER WITH ROLLBACK IMMEDIATE;
) Regarding your code, however, I would suggest using receive
instead of select
; otherwise you might get the same dialog multiple times. Also, you might want to distinguish EndDialog
messages from any others if they occur in the queue.
I had a similar issue where removing the services took really long. So long my SQL server ran out of memory and crashed. I first tried ending the dialog with the query you posted at the top, but that didn't help. In my case there were a lot of conversations created that were never closed. So although the queues were empty, there were still a lot conversations when I queryed sys.conversation_endpoints. I used the following script to clean up the conversations and after that I was able to delete it instantly.
Note that below query will close all conversations, no matter the queue so you need to filter if you only want to delete a specific one
declare @c uniqueidentifier
while(1=1)
begin
select top 1 @c = conversation_handle from sys.conversation_endpoints
if (@@ROWCOUNT = 0)
break
end conversation @c with cleanup
end
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.