question

mooriginal avatar image
mooriginal asked

Insert causing blocking SQL2012

We have this procedure - which inserts a single row on very regular basis - called from various other procedures.

The table is 93 million rows - however it being regularly caught and the instigator for many other blocking.

****************************************************************************/

ALTER PROCEDURE [taurus].[InsertAuditLog]

(

@auditActionId uniqueidentifier,

@paymentRequestId uniqueidentifier,

@event nvarchar(max),

@eventDate datetime,

@stateBefore varchar(max),

@stateAfter varchar(max),

@ipAddress varchar(50),

@result bit,

@updatedBy varchar(250)

)

AS


BEGIN

-- SET NOCOUNT ON added to prevent extra result sets from

-- interfering with SELECT statements.

SET NOCOUNT ON;

BEGIN

BEGIN TRY

INSERT INTO dbo.AuditLog (

AuditActionId,

PaymentRequestId,

[Event],

EventDate,

StateBefore,

StateAfter,

IPAddress,

Result,

UpdatedBy

)

OUTPUT INSERTED.AuditLogId

VALUES (

@auditActionId,

@paymentRequestId,

@event,

@eventDate,

@stateBefore,

@stateAfter,

@ipAddress,

@result,

@updatedBy

)

END TRY

BEGIN CATCH

THROW;

END CATCH

END


END

GO


tsqlsqlserver2012blocking
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Jeff Moden avatar image
Jeff Moden answered

@mooriginal


Post the CREATE TABLE script Include any indexes and constraints. Also, since it's an audit table, it's likely to have a fairly high insert rate. You might want to collect the inserts in a separate staging table and collect a bunch. Then do a "mass insert" into the final table, say, once per minute or two and then delete them from the staging table.

We had a similar problem as what you describe and the fix I mention above was very effective in alleviating the problem. Of course, the staging table should be devoid of all but a Clustered Index based on an ever-increasing IDENTITY column, which I recommend be a BIGINT.


10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

mooriginal avatar image
mooriginal answered

thanks that seems like a very reasonable idea actually.

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Jeff Moden avatar image
Jeff Moden answered

Thanks for accepting my answer but I only have a bit of the information I need to help you help yourself, especially with the staging table I recommend. Please see my previous post for what I need to truly help.

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

mooriginal avatar image
mooriginal answered

Thanks it was enough to spark my direction which was enough to pull the issue in the correct direction.

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.