2013-05-22 43 views
1

遊標中的case語句不起作用,但select sql自身正常工作。請指教最新的問題mysql Case語句在光標中不起作用

/* cursor for summed txns fee amounts of merchants */ 
    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 CASE 
       WHEN includeTodaysTxnsInFeeBilling(feeTxn.toParticipantId) THEN feeTxn.txnDate <= dTxnDate 
       ELSE feeTxn.txnDate < dTxnDate 
      END 
     GROUP BY feeTxn.toParticipantId, txn.cardType 
); 
+0

您的查詢將會非常非常慢... –

回答

0

不尋常的使用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 
+0

感謝您的快速響應。我正在處理大量數據。我想要使​​用該案例的唯一原因是確定includeTodaysTxnsInFeeBilling(feeTxn.toParticipantId)是否返回true,然後在where子句中添加feeTxn.txnDate <= dTxnDate,否則添加feeTxn.txnDate user2410257

+0

函數調用可能會很慢,使用類似的情況(比如OR)可能會阻止它使用索引。上面的聯合示例有望允許它使用索引。然而,它在包含TodaysTxnsInFeeBilling中做了什麼?如果只是檢查一個表,那麼直接加入該表可能會更快。 – Kickstart