concept queue in category sql server

appears as: queues, queue, queue, queues
SQL Server MVP Deep Dives Vol. 2

This is an excerpt from Manning's book SQL Server MVP Deep Dives Vol. 2.

You’ll have two instances: DataSender (the initiator) and DataReceiver (the target). Data will be sent from the initiator to the target. There are two parts of the Service Broker set up on each instance. The first step is to enable the Service Broker functionality at the instance level (endpoint, certificate-based security, and so forth) in the master database. The second step is to create Service Broker objects (for example, message types, contracts, queues, and routes) and functionality (such as activation stored procedures and programming) at the database level.

Listing 3. Creating support objects in DBReceiver on the DataReceiver instance
USE DBReceiver GO -- enable service broker for our database ALTER DATABASE DBReceiver SET ENABLE_BROKER -- get the Service broker ID to use later SELECT service_broker_guid FROM sys.databases WHERE name = 'DBReceiver' -- returns 962FF4B4-428B-482D-A0A7-748F62D3C41C on my machine GO -- create the schema that holds service broker support objects CREATE SCHEMA DataTransfer -- create table to hold errors when receiving messages CREATE TABLE DataTransfer.ReceiveErrors ( ID BIGINT IDENTITY(1, 1) PRIMARY KEY, ErrorProcedure NVARCHAR(126) NOT NULL, ErrorLine INT NOT NULL, ErrorNumber INT NOT NULL, ErrorMessage NVARCHAR(MAX) NOT NULL, ErrorSeverity INT NOT NULL, ErrorState INT NOT NULL, TransferData XML NOT NULL, ErrorDate DATETIME NOT NULL DEFAULT GETDATE() ) -- create table to hold received messages CREATE TABLE DataTransfer.ReceivedMessages ( ID INT IDENTITY(1, 1) PRIMARY KEY, TableSchema NVARCHAR(128) not null, TableName NVARCHAR(128) NOT NULL, TransferedRows XML NOT NULL, DialogID UNIQUEIDENTIFIER NOT NULL, MsgSeqNum BIGINT NOT NULL, CreatedOn DATETIME NOT NULL DEFAULT GETDATE() ) -- stored procedure that writes the transferred data -- from the queue to the DataTransfer.ReceivedMessages table -- when the conversation ends those messages can be processed -- with another stored procedure GO CREATE PROCEDURE DataTransfer.spReceiveTransferredData AS BEGIN DECLARE @msgBody XML, @dlgId uniqueidentifier, @msgSeqNum bigint, @msgTypeName SYSNAME WHILE(1=1) BEGIN BEGIN TRANSACTION BEGIN TRY -- receive one message at a time ;RECEIVE top(1) @msgBody = message_body, @dlgId = conversation_handle, @msgSeqNum = message_sequence_number, @msgTypeName = message_type_name FROM DataTransfer.QueueDataTransferReceive -- exit when the whole queue has been processed IF @@ROWCOUNT = 0 BEGIN IF @@TRANCOUNT > 0 ROLLBACK; BREAK; END -- if we get the EndOfTransfer message we're closing -- the conversation IF @msgTypeName = N'//DataTransfer/EndOfTransfer' BEGIN END CONVERSATION @dlgId; /* Here a stored procedure can be ran that processes messages stored in the DataTransfer.ReceivedMessages table */ END ELSE BEGIN -- insert the message into the -- DataTransfer.ReceivedMessages table -- with a little XML querying magic INSERT INTO DataTransfer.ReceivedMessages (TableSchema, TableName, TransferedRows,DialogID, MsgSeqNum) SELECT T.c.value('@TableSchema', 'nvarchar(128)') as TableSchema, T.c.value('@TableName', 'nvarchar(128)') as TableName, T.c.query('./rows') as rowsXml, @dlgId, @msgSeqNum FROM @msgBody.nodes('/*') T(c) END IF @@TRANCOUNT > 0 COMMIT; END TRY BEGIN CATCH IF @@TRANCOUNT > 0 ROLLBACK; -- insert error into the ReceiveErrors table INSERT INTO DataTransfer.ReceiveErrors( ErrorProcedure, ErrorLine, ErrorNumber, ErrorMessage, ErrorSeverity, ErrorState, TransferData) SELECT ERROR_PROCEDURE(), ERROR_LINE(), ERROR_NUMBER(), ERROR_MESSAGE(), ERROR_SEVERITY(), ERROR_STATE(), @msgBody -- here you can add extra logging to the error log -- or email notifications, etc... END CATCH; END END

The purpose of this chapter is to help fellow DBAs, developers, and other database professionals by spreading the word about waits and queues. This performance-tuning methodology became significant with the release of SQL Server 2005, with all its dynamic management views and functions, collectively known as dynamic management objects (DMOs). With waits and queues, you can quickly identify the cause of a performance bottleneck on your SQL Server systems. Although this methodology was introduced without much fanfare, there wasn’t a substantial amount of early adoption by those who would benefit from it the most: database administrators.

SQL Server 2008 Administration in Action

This is an excerpt from Manning's book SQL Server 2008 Administration in Action.

  • Amount of log in the principal’s send queue and the mirror’s redo queue
  • Tom Davidson’s excellent whitepaper SQL Server 2005 Waits and Queues is a must read for anyone interested in SQL Server performance tuning. Equally applicable to SQL Server 2008, it argues the case for a targeted approach to performance tuning by determining the resources that processes are spending the most time waiting on. Knowing this enables the analysis of a smaller subset of queues, therefore eliminating inconsequential data from the analysis-and-troubleshooting process.