不尋常的使用CASE。你可以沒有它,像這樣: -
DECLARE sumCursor CURSOR FOR
(
SELECT feeTxn.toParticipantId, txn.cardType, SUM(IF(feeTxn.isCredit='t', feeTxn.amt, -feeTxn.amt)) amt, MAX(feeTxn.feeTxnId) maxId, MIN(feeTxn.feeTxnId) minId, IFNULL(md.acquirerId, bd.acquirerId) as acquirerId
FROM mas_feeTxn feeTxn
LEFT JOIN mas_txn txn ON feeTxn.txnId = txn.txnId AND feeTxn.toParticipantId = txn.merchantId
JOIN mas_merchantDetail md ON feeTxn.toParticipantId = md.participantId AND md.status = 'A'
JOIN mas_achInfo achInfo on achInfo.participantId = md.participantId and achInfo.status = 'A' and achInfo.fundingBankAccountId is not null
LEFT JOIN mas_bankDetail bd ON md.bankId = bd.participantId AND bd.status = 'A'
WHERE feeTxn.billedStatus = 'f'
AND IFNULL(md.acquirerId, bd.acquirerId) = dAcquirerId
AND ((includeTodaysTxnsInFeeBilling(feeTxn.toParticipantId) AND feeTxn.txnDate <= dTxnDate)
OR feeTxn.txnDate < dTxnDate)
GROUP BY feeTxn.toParticipantId, txn.cardType
);
沒有看到,我不知道下面將有助於數據,但鑑於你的函數使用了少量的數據,並有效地或運算數據如果你將查詢拆分成幾個聯合在一起的查詢,它可能會幫助MySQL更有效地使用索引來提高性能。類似這樣的: -
SELECT toParticipantId, cardType, SUM(amt) AS amt, MAX(feeTxnId) AS maxId, MIN(feeTxnId) AS minId, IFNULL(md.acquirerId, bd.acquirerId) as acquirerId
FROM (
SELECT feeTxn.toParticipantId, txn.cardType, feeTxn.amt AS amt, feeTxn.feeTxnId, IFNULL(md.acquirerId, bd.acquirerId) as acquirerId
FROM mas_feeTxn feeTxn
INNER JOIN mas_merchantDetail md ON feeTxn.toParticipantId = md.participantId AND md.status = 'A'
INNER JOIN mas_achInfo achInfo on achInfo.participantId = md.participantId and achInfo.status = 'A' and achInfo.fundingBankAccountId is not null
LEFT JOIN mas_txn txn ON feeTxn.txnId = txn.txnId AND feeTxn.toParticipantId = txn.merchantId
LEFT JOIN mas_bankDetail bd ON md.bankId = bd.participantId AND bd.status = 'A'
WHERE feeTxn.billedStatus = 'f'
AND IFNULL(md.acquirerId, bd.acquirerId) = dAcquirerId
AND includeTodaysTxnsInFeeBilling(feeTxn.toParticipantId)
AND feeTxn.txnDate = dTxnDate
AND feeTxn.isCredit = 't'
UNION
SELECT feeTxn.toParticipantId, txn.cardType, feeTxn.amt AS amt, feeTxn.feeTxnId, IFNULL(md.acquirerId, bd.acquirerId) as acquirerId
FROM mas_feeTxn feeTxn
INNER JOIN mas_merchantDetail md ON feeTxn.toParticipantId = md.participantId AND md.status = 'A'
INNER JOIN mas_achInfo achInfo on achInfo.participantId = md.participantId and achInfo.status = 'A' and achInfo.fundingBankAccountId is not null
LEFT JOIN mas_txn txn ON feeTxn.txnId = txn.txnId AND feeTxn.toParticipantId = txn.merchantId
LEFT JOIN mas_bankDetail bd ON md.bankId = bd.participantId AND bd.status = 'A'
WHERE feeTxn.billedStatus = 'f'
AND IFNULL(md.acquirerId, bd.acquirerId) = dAcquirerId
AND feeTxn.txnDate < dTxnDate
AND feeTxn.isCredit = 't'
UNION
SELECT feeTxn.toParticipantId, txn.cardType, -feeTxn.amt AS amt, feeTxn.feeTxnId, IFNULL(md.acquirerId, bd.acquirerId) as acquirerId
FROM mas_feeTxn feeTxn
INNER JOIN mas_merchantDetail md ON feeTxn.toParticipantId = md.participantId AND md.status = 'A'
INNER JOIN mas_achInfo achInfo on achInfo.participantId = md.participantId and achInfo.status = 'A' and achInfo.fundingBankAccountId is not null
LEFT JOIN mas_txn txn ON feeTxn.txnId = txn.txnId AND feeTxn.toParticipantId = txn.merchantId
LEFT JOIN mas_bankDetail bd ON md.bankId = bd.participantId AND bd.status = 'A'
WHERE feeTxn.billedStatus = 'f'
AND IFNULL(md.acquirerId, bd.acquirerId) = dAcquirerId
AND includeTodaysTxnsInFeeBilling(feeTxn.toParticipantId)
AND feeTxn.txnDate = dTxnDate
AND feeTxn.isCredit != 't'
UNION
SELECT feeTxn.toParticipantId, txn.cardType, -feeTxn.amt AS amt, feeTxn.feeTxnId, IFNULL(md.acquirerId, bd.acquirerId) as acquirerId
FROM mas_feeTxn feeTxn
INNER JOIN mas_merchantDetail md ON feeTxn.toParticipantId = md.participantId AND md.status = 'A'
INNER JOIN mas_achInfo achInfo on achInfo.participantId = md.participantId and achInfo.status = 'A' and achInfo.fundingBankAccountId is not null
LEFT JOIN mas_txn txn ON feeTxn.txnId = txn.txnId AND feeTxn.toParticipantId = txn.merchantId
LEFT JOIN mas_bankDetail bd ON md.bankId = bd.participantId AND bd.status = 'A'
WHERE feeTxn.billedStatus = 'f'
AND IFNULL(md.acquirerId, bd.acquirerId) = dAcquirerId
AND feeTxn.txnDate < dTxnDate
AND feeTxn.isCredit != 't'
} Sub1
GROUP BY toParticipantId, cardType
您的查詢將會非常非常慢... –