我正在努力提高目前需要5个多小时的存储过程的性能。 我已经做了以下事情来改善这个程序的表现 - - 我为每个表创建了索引。 - 而不是count(*),我使用count(1)。
但我只是想知道用什么代替while循环,如果可能的话,我可以使用光标吗?会不会有任何影响,因为我读过游标确实需要花费大量时间进行处理..
程序摘要: - 计算表的行数。 - 为每行计数选择参数。 - 这些参数正被另一个程序用于执行。
DECLARE @lnTotalCount INT
SELECT @lnTotalCount = COUNT(*) FROM
firstTable chksts
SELECT @lnRecordCount = 1, @lnRowsProcessed =0
WHILE @lnRecordCount <= @lnTotalCount
BEGIN
-- SET ROWCOUNT 1
SELECT @lcCKPY_REF_ID = ckck.CKPY_REF_ID,
@lnCKCK_SEQ_NO = ckck.CKCK_SEQ_NO,
@lcPYPY_ID = ckck.PYPY_ID,
@lnCKCK_CK_NO = ckck.CKCK_CK_NO,
@ldtCKCK_CASHED_DT = ckck.CKCK_CASHED_DT,
@ldtCKCK_PRINTED_DT = ckck.CKCK_PRINTED_DT,
@ldtCKCK_REISS_DT = ckck.CKCK_REISS_DT,
@lcCKCK_TYPE = ckck.CKCK_TYPE,
@lcCKCK_PAYEE_NAME = ckck.CKCK_PAYEE_NAME,
@lcCKCK_CURR_STS = LTRIM(RTRIM(chksts.[TRANSACTION])),
@lnCKST_SEQ_NO = ckck.CKST_SEQ_NO,
@lcCKCK_REISS_USUS_ID = ckck.CKCK_REISS_USUS_ID,
@lnCKCK_LOCK_TOKEN = ckck.CKCK_LOCK_TOKEN,
@ldtATXR_SOURCE_ID = ckck.ATXR_SOURCE_ID,
@lnBPID_CK_NO = chksts.SERIAL_NUMBER
FROM
firstTable chksts,
secondTable ckck,
thirdTable bpid
WHERE ckck.CKPY_REF_ID = bpid.CKPY_REF_ID
AND bpid.BPID_CK_NO = chksts.SERIAL_NUMBER
AND ckck.CKCK_SEQ_NO = bpid.CKCK_SEQ_NO
AND ckck.CKCK_CURR_STS IN ('03','X3')
AND chksts.ID = @lnRecordCount
IF(@@ROWCOUNT<>0) /* 1.5 If-Else condition*/
BEGIN
EXEC @lnRetCd = ABProcedure
NULL,
NULL,
@lcCKPY_REF_ID,
@lnCKCK_SEQ_NO,
@lcPYPY_ID,
@lnCKCK_CK_NO,
@ldtCKCK_CASHED_DT,
@ldtCKCK_PRINTED_DT,
@ldtCKCK_REISS_DT,
@lcCKCK_TYPE,
@lcCKCK_PAYEE_NAME,
@lcCKCK_CURR_STS,
@lnCKST_SEQ_NO,
@lcCKCK_REISS_USUS_ID,
@lnCKCK_LOCK_TOKEN,
@ldtATXR_SOURCE_ID
SELECT @lnRowsProcessed = @lnRowsProcessed + @@ROWCOUNT
SELECT @lnRecordCount = @lnRecordCount + 1