事务(进程ID 62)已与另一个进程在锁资源上死锁,因此已被选择为死锁受害者。重新运行交易

时间:2019-02-01 05:59:20

标签: sql sql-server tsql

当站点非常繁忙时,它将在数据库中返回错误。我提供了正在使用的存储过程。为此,该网站在当时是如此缓慢。

ALTER PROCEDURE [dbo].[sp_getProductCategoriesForTopMenu] 
@Id nvarchar(50)='0',
@CompanyId nvarchar(50)='0',
@StoreId nvarchar(50)='0',
@Keywords nvarchar(200)='',
@Skip nvarchar(100)='0',
@Take nvarchar(100)='20',
@Format nvarchar(50)=''
AS

DECLARE @SQL AS nvarchar(4000)

BEGIN
    SET NOCOUNT ON
    begin try


CREATE TABLE #TopMenuCategoryIdTableForStore
    (
         CategoryId bigint not null
    )

if(@StoreId>0)
    begin
        insert into #TopMenuCategoryIdTableForStore(CategoryId)
        select distinct Id from dbo.Category where IncludeInTopMenu=1 and IncludeInTopMenuAssignStoreIds is not null and IncludeInTopMenuAssignStoreIds!=''
            and @StoreId in (select Item from dbo.fnSplitStringForBigint(IncludeInTopMenuAssignStoreIds,','))
    end

if(@Id!='0')
    begin
        set @SQL='SELECT        dbo.Category.Id, dbo.Category.CompanyId, 

dbo.Category.Name, dbo.Category.ParentCategoryId, 
                         dbo.Category.ShowOnHomePage, dbo.Category.IncludeInTopMenu,dbo.Category.SubCategoryStyle, dbo.Category.Published, dbo.Category.Deleted, dbo.Category.DisplayOrder, dbo.Category.ProductTypeId, dbo.ProductType.TypeName, dbo.Picture.PictureUrl, dbo.Picture.PictureThumbUrl,
                         (case when (select count(pcm.ProductId) from dbo.StoreInfo s inner join dbo.Product_Catalog_Mapping pcm on s.CatalogId=pcm.CatalogId inner join dbo.Product p on pcm.ProductId=p.Id left outer join dbo.Product_Category_Mapping pcmm on pcm.ProductId=pcmm.ProductId  where s.Id='+@StoreId+' and (p.CategoryId='+@Id+' or pcmm.CategoryId='+@Id+'))=0 then 0 else 1 end) as HasProduct 
FROM            dbo.Category INNER JOIN
                         dbo.ProductType ON dbo.Category.ProductTypeId = dbo.ProductType.Id LEFT OUTER JOIN
                         dbo.Picture ON dbo.Category.PictureId = dbo.Picture.Id where dbo.Category.Id='+@Id
        end
    else
        begin
                set @SQL='SELECT       dbo.Category.Id, dbo.Category.CompanyId, dbo.Category.Name, dbo.Category.ParentCategoryId, 
                        dbo.Category.ShowOnHomePage,
                         dbo.Category.IncludeInTopMenu,
                         dbo.Category.SubCategoryStyle, dbo.Category.Published, dbo.Category.Deleted, dbo.Category.DisplayOrder, dbo.Category.ProductTypeId, dbo.ProductType.TypeName, dbo.Picture.PictureUrl, dbo.Picture.PictureThumbUrl,
                         (case when (select count(pcm.ProductId) from dbo.StoreInfo s inner join dbo.Product_Catalog_Mapping pcm on s.CatalogId=pcm.CatalogId inner join dbo.Product p on pcm.ProductId=p.Id left outer join dbo.Product_Category_Mapping pcmm on pcm.ProductId=pcmm.ProductId  where s.Id='+@StoreId+' and (p.CategoryId=dbo.Category.Id or pcmm.CategoryId=dbo.Category.Id))=0 then 0 else 1 end) as HasProduct
FROM            dbo.Category INNER JOIN
                         dbo.ProductType ON dbo.Category.ProductTypeId = dbo.ProductType.Id LEFT OUTER JOIN
                         dbo.Picture ON dbo.Category.PictureId = dbo.Picture.Id where dbo.Category.CompanyId='+@CompanyId
        end

    IF(@Keywords!='')
        BEGIN
            set @SQL=@SQL+N' AND (dbo.Category.[Name] LIKE ''%'+@Keywords+'%'')'
        END         


    SET @SQL = @SQL + ' ORDER BY dbo.Category.Id DESC'
    SET @SQL = @SQL + ' OFFSET ' + @Skip + ' ROWS FETCH NEXT ' + @Take + ' ROWS ONLY' 



    IF(@Format='JSON')
        BEGIN
            SET @SQL=@SQL+' FOR JSON PATH'
        END
    EXEC sp_executesql @SQL 


    if object_id('tempdb..#TopMenuCategoryIdTableForStore','U') is not null
        begin
            drop table #TopMenuCategoryIdTableForStore
        end

end try
begin catch
    insert into dbo.DatabaseErrorLog SELECT ERROR_NUMBER() AS ErrorNumber
     ,ERROR_SEVERITY() AS ErrorSeverity
     ,ERROR_STATE() AS ErrorState
     ,'sp_getProductCategoriesForTopMenu' AS ErrorProcedure
     ,ERROR_LINE() AS ErrorLine
     ,ERROR_MESSAGE() AS ErrorMessage,
     GETUTCDATE() as ErrorTime
end catch

END

1 个答案:

答案 0 :(得分:0)

您可以像这样使用查询提示with nolock

 select * from dbo.StoreInfo with(nolock)

这应该防止死锁。