0

This is my stored procedure, it's taking some time to execute even though running with local database.

Please suggest changes in order to improve the performance

BEGIN TRY
        DECLARE @COUNTRY_CD INT
        SET @COUNTRY_CD =(SELECT COUNTRY_CD FROM COUNTRY WHERE COUNTRY_DESC = LTRIM(RTRIM(@COUNTRY_DESC)))
        DECLARE @COMPANNY_CD INT
        SET @COMPANNY_CD =(SELECT COMPANY_CD FROM COMPANY WHERE COMPANY_DESC = LTRIM(RTRIM(@COMPANY_DESC)))
    BEGIN TRANSACTION
        DELETE FROM PACK 
        WHERE   COUNTRY_CD = @COUNTRY_CD 
                AND COMPANY_CD = @COMPANNY_CD
                AND PACK_DESC = LTRIM(RTRIM(@PACK_DESC))
    COMMIT TRANSACTION
END TRY
BEGIN CATCH
    IF(@@TRANCOUNT > 0)
        ROLLBACK TRANSACTION
    DECLARE @ErrMsg nvarchar(4000), 
            @ErrSeverity int
    SELECT @ErrMsg = ERROR_MESSAGE(),@ErrSeverity = ERROR_SEVERITY()
    RAISERROR(@ErrMsg, @ErrSeverity, 1)
END CATCH
4

4 回答 4

1

尝试在单独的过程中评估变量 @COUNTRY_CD 和 @COMPANNY_CD 的值,并将它们作为 i/p 参数传递给该过程,看看是否有帮助。我过去看到过这个问题,我刚才提到的解决方案解决了这个问题。

于 2013-07-31T12:20:34.343 回答
0

如果不了解更多有关您的数据库架构的信息,很难准确地说出。一些初步想法可能是立即清理 *_DESC 变量,而不是在 WHERE 子句中执行 LTRIM 和 RTRIM。也许考虑或添加到包含 COUNTRY_CD/COMPANY_CD 的 PACK 表上的索引(虽然不是描述,假设它是长字符串文本。我认为 COMPANY 和 COUNTRY 是非常小的表,但希望你在这些字段上有正确的索引。可能也值得尝试在 DELETE 中加入这些表,而不是提前进行查找。

-- clenaup variables
-- these should be new vars, not input parms
SELECT @COUNTRY_DESC = LTRIM(RTRIM(@COUNTRY_DESC))
    ,@COMPANY_DESC = LTRIM(RTRIM(@COMPANY_DESC))
    ,PACK_DESC = LTRIM(RTRIM(@PACK_DESC ))

-- delete   
DELETE PACK
FROM PACK
JOIN COUNTRY ON PACK.COUNTRY_CD = COUNTRY.COUNTRY_CD
JOIN COMPANY ON PACK.COMPANY_CD = COMPANY.COMPANY_CD
WHERE   COUNTRY.COUNTRY_DESC = @COUNTRY_DESC 
AND COMPANY.COMPANY_DESC = @COMPANY_DESC
AND PACK.PACK_DESC = @PACK_DESC
于 2013-07-31T12:25:52.513 回答
0

确保 COMPANY 在 company_cd 上被索引,COUNTRY 在 country_cd 上,PACK 在 company_cd、country_cd、pack_desc 上。

如果没有正确的索引,从大表中删除需要一些时间。

于 2013-07-31T16:44:00.263 回答
0

尝试在您的存储过程中单击鼠标右键并检查估计的执行计划。您可以看到您的 SP 将有多“昂贵”。

如果需要你可以试试

https://stackoverflow.com/a/797968/1504882

于 2013-07-31T12:07:47.663 回答