1. 问题分析
从阿里云日志里捞出死锁排名第5名的死锁,每天出现,
select [CustomerID],[SKU] from [RB_Product_Admin] WHERE ID = 232245
分析sql 发现,查询条件 ID 不是主键,没有索引,全表扫描查询数据,导致更新占用资源长,增加了死锁的概率。
2.解决方案
将ID新建索引,测试后查询从全表扫描优化到索引查找,查询很快
已添加索引:IX_RB_Product_Admin_ID,后续验证效果
3.死锁SQL
UPDATE [RB_Product_Admin]
SET [CustomerID] = @CustomerID_2,
[SKU] = @SKU_2,
[RecommendValue] = @RecommendValue_2,
[AddAdminID] = @AddAdminID_2,
[AddAdminName] = @AddAdminName_2,
[AddTime] = @AddTime_2,
[ToDevelopMemo] = @ToDevelopMemo_2,
[DevelopStatus] = @DevelopStatus_2,
[DevelopAdminID] = @DevelopAdminID_2,
[DevelopAdminName] = @DevelopAdminName_2,
[DevelopTime] = @DevelopTime_2,
[DevelopRefuseReason] = @DevelopRefuseReason_2,
[DevelopRefuseTime] = @DevelopRefuseTime_2,
[AssignDevelopAdminID] = @AssignDevelopAdminID_2,
[AssignDevelopAdminName] = @AssignDevelopAdminName_2,
[ToEditorAttachment] = @ToEditorAttachment_2,
[ToEditorMemo] = @ToEditorMemo_2,
[EditStatus] = @EditStatus_2,
[EditAdminID] = @EditAdminID_2,
[EditAdminName] = @EditAdminName_2,
[EditTime] = @EditTime_2,
[EditRefuseReason] = @EditRefuseReason_2,
[EditRefuseTime] = @EditRefuseTime_2,
[ToImageMemo] = @ToImageMemo_2,
[ImageStatus] = @ImageStatus_2,
[ImageAdminID] = @ImageAdminID_2,
[ImageAdminName] = @ImageAdminName_2,
[ImageTime] = @ImageTime_2,
[ImageRefuseReason] = @ImageRefuseReason_2,
[ImageRefuseTime] = @ImageRefuseTime_2,
[BuyerID] = @BuyerID_2,
[BuyerName] = @BuyerName_2,
[BuyerSetTime] = @BuyerSetTime_2,
[CheckStatus] = @CheckStatus_2,
[CheckReason] = @CheckReason_2,
[CheckAdminID] = @CheckAdminID_2,
[CheckAdminName] = @CheckAdminName_2,
[CheckTime] = @CheckTime_2,
[OnlineTime] = @OnlineTime_2,
[OfflineTime] = @OfflineTime_2,
[OfflineAdminID] = @OfflineAdminID_2,
[OfflineAdminName] = @OfflineAdminName_2,
[CopyProductID] = @CopyProductID_2,
[PackageCommission] = @PackageCommission_2,
[PickingCommission] = @PickingCommission_2,
[ToProcurementCheckMemo] = @ToProcurementCheckMemo_2,
[PriceAdminID] = @PriceAdminID_2,
[PriceAdminName] = @PriceAdminName_2,
[PriceSpecialistOperatTime] = @PriceSpecialistOperatTime_2,
[ToDeliveryPackNoteMemo] = @ToDeliveryPackNoteMemo_2,
[IsEditRemarkApplyAllVariants] = @IsEditRemarkApplyAllVariants_2,
[ProductDevelopmentAuditTime] = @ProductDevelopmentAuditTime_2,
[ProductEditAuditTime] = @ProductEditAuditTime_2,
[ProductPictureAuditTime] = @ProductPictureAuditTime_2,
[ProductStatusModifyTime] = @ProductStatusModifyTime_2,
[ProductPropertyModifyTime] = @ProductPropertyModifyTime_2,
[UpdateTime] = @UpdateTime_2,
[DevelopmentAuditOperatorId] = @DevelopmentAuditOperatorId_2,
[DevelopmentAuditOperatorName] = @DevelopmentAuditOperatorName_2,
[EditAuditOperatorId] = @EditAuditOperatorId_2,
[EditAuditOperatorName] = @EditAuditOperatorName_2,
[PictureAuditOperatorId] = @PictureAuditOperatorId_2,
[PictureAuditOperatorName] = @PictureAuditOperatorName_2,
[SetStopProductionTime] = @SetStopProductionTime_2,
[SetStopProductionOperatorId] = @SetStopProductionOperatorId_2,
[SetStopProductionOperatorName] = @SetStopProductionOperatorName_2,
[SetShortageTime] = @SetShortageTime_2,
[SetShortageOperatorId] = @SetShortageOperatorId_2,
[SetShortageOperatorName] = @SetShortageOperatorName_2,
[SetClearLibraryTime] = @SetClearLibraryTime_2,
[SetClearLibraryOperatorId] = @SetClearLibraryOperatorId_2,
[SetClearLibraryOperatorName] = @SetClearLibraryOperatorName_2,
[SettingUpImitationCardsOperatorId] = @SettingUpImitationCardsOperatorId_2,
[SettingUpImitationCardsOperatorName] = @SettingUpImitationCardsOperatorName_2,
[SettingMoldingsTime] = @SettingMoldingsTime_2,
[IsAmazonSale] = @IsAmazonSale_2,
[NoSalePlatform] = @NoSalePlatform_2,
[PhotographerId] = @PhotographerId_2,
[PhotographerName] = @PhotographerName_2,
[PhotographerTime] = @PhotographerTime_2,
[IntellectualPropertyRightsAdminID] = @IntellectualPropertyRightsAdminID_2,
[IntellectualPropertyRightsAdminName] = @IntellectualPropertyRightsAdminName_2,
[IntellectualPropertyRightsTime] = @IntellectualPropertyRightsTime_2,
[NoSaleCountry] = @NoSaleCountry_2,
[WrapDesignerId] = @WrapDesignerId_2,
[WrapDesignerName] = @WrapDesignerName_2,
[WrapDesignerTime] = @WrapDesignerTime_2,
[ImageDesignerId] = @ImageDesignerId_2,
[ImageDesignerName] = @ImageDesignerName_2,
[ImageDesignerTime] = @ImageDesignerTime_2,
[AssignDevelopTime] = @AssignDevelopTime_2,
[CompeteAsin] = @CompeteAsin_2
WHERE ID = @ID_2;
--PARAMS:@ID_1 = 232245 ,@CustomerID_1 = 1 ,@SKU_1 = 2211833 ,@RecommendValue_1 = 0 ,@AddAdminID_1 = 139 ,@AddAdminName_1 = 王兰兰 ,@AddTime_1 = 2017/6/6 17:53:26 ,@ToDevelopMemo_1 = 鸡眼脚趾套 白色(闭口大号1双+闭口小号4双) ,@DevelopStatus_1 = 3 ,@DevelopAdminID_1 = 139 ,@DevelopAdminName_1 = 王兰兰 ,@DevelopTime_1 = 2017/6/6 17:52:26 ,@DevelopRefuseReason_1 = ,@DevelopRefuseTime_1 = NULL ,@AssignDevelopAdminID_1 = 139 ,@AssignDevelopAdminName_1 = 王兰兰 ,@ToEditorAttachment_1 = ,@ToEditorMemo_1 = ,@EditStatus_1 = 3 ,@EditAdminID_1 = 684 ,@EditAdminName_1 = 易清玉 ,@EditTime_1 = 2017/6/10 15:30:41 ,@EditRefuseReason_1 = ,@EditRefuseTime_1 = NULL ,@ToImageMemo_1 = ,@ImageStatus_1 = 1 ,@ImageAdminID_1 = 954 ,@ImageAdminName_1 = 肖辉发 ,@ImageTime_1 = 2017/12/25 13:38:39 ,@ImageRefuseReason_1 = ,@ImageRefuseTime_1 = NULL ,@BuyerID_1 = 6279 ,@BuyerName_1 = 何静 ,@BuyerSetTime_1 = 2022/4/19 16:27:19 ,@CheckStatus_1 = 1 ,@CheckReason_1 = ,@CheckAdminID_1 = 94 ,@CheckAdminName_1 = 张英勇 ,@CheckTime_1 = 2017/6/12 22:35:12 ,@OnlineTime_1 = 2017/6/12 22:35:12 ,@OfflineTime_1 = NULL ,@OfflineAdminID_1 = 0 ,@OfflineAdminName_1 = ,@CopyProductID_1 = 0 ,@PackageCommission_1 = NULL ,@PickingCommission_1 = NULL ,@ToProcurementCheckMemo_1 = ,@PriceAdminID_1 = NULL ,@PriceAdminName_1 = NULL ,@PriceSpecialistOperatTime_1 = NULL ,@ToDeliveryPackNoteMemo_1 = ,@IsEditRemarkApplyAllVariants_1 = False ,@ProductDevelopmentAuditTime_1 = NULL ,@ProductEditAuditTime_1 = NULL ,@ProductPictureAuditTime_1 = NULL ,@ProductStatusModifyTime_1 = 2021/4/1 16:27:30 ,@ProductPropertyModifyTime_1 = NULL ,@UpdateTime_1 = 2022/4/19 16:27:19 ,@DevelopmentAuditOperatorId_1 = NULL
已参考解决方案优化
已添加索引:IX_RB_Product_Admin_ID,后续验证效果