我正在尝试提高当前需要超过 5 个小时的存储过程的性能。我已经做了以下事情来提高这个过程的性能——我已经为每个表创建了索引。--我使用的是count(1),而不是count(*)。
但我只是想知道用什么代替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