串联性能低

时间:2017-01-05 03:42:09

标签: sql performance concatenation where

我使用以下代码来选择我想要的行,但问题是它在其他表的select DocumentNumber列上的性能很低,问题出在这一部分---> (' IN - ' + @ ItemStatus +'#&#39 + chequeserial)

SELECT     
    Banking.ReceivedCheque.Date, Banking.ReceivedCheque.StatusDate,
    Banking.ReceivedCheque.DueDate, Banking.ReceivedCheque.ChequeSerial, 
    Banking.ReceivedCheque.BankName Banking.ReceivedCheque.CompanyInfoFK, 
    Banking.ReceivedCheque.FinancialPeriodFK, 
    Banking.ReceivedCheque.CreditGFK, Banking.ReceivedCheque.CreditID, 
    ReceivedFromAccount = dbo.getname(CreditID, CreditGFK, FinancialPeriodFK),
    DefBankAccount = dbo.getname(DefaultBankID, '4', FinancialPeriodFK), 
    Banking.ReceivedCheque.StatusFK,
    Banking.ChequeStatus.Title,
    (SELECT MAX(DocumentFK) 
     FROM Accounting.DocumentDetail 
     WHERE ItemFK = ('IN-' + @ItemStatus + '#' + chequeserial)  
       AND financialPeriodFK = @FinancialPeriodFK) AS DocumentNumber
FROM         
    Banking.ReceivedCheque 
INNER JOIN
    Banking.ChequeStatus ON Banking.ReceivedCheque.StatusFK = Banking.ChequeStatus.ChequeStatusID
WHERE
    ReceivedCheque.FinancialPeriodFK = @FinancialPeriodFK  
    AND Banking.ReceivedCheque.StatusFK = @StatusFK

如果有任何其他解决方案可以提高性能,请告诉我。

1 个答案:

答案 0 :(得分:0)

看起来@ItemStatus是一个标量参数。你可以做一次这部分操作而不是比较的每一行吗?你在场景连接背后消除了很多。

set @InItemStatusPound = 'IN-'+@ItemStatus + '#'

ItemFK=(@InItemStatusPound+chequeserial)

这通常是查找表具有基于数值(如int而不是字符串值)的外键的原因之一。是在整数而不是需要连接选项的字符串上执行连接吗?

注意:如果你能保证字符串小于32位,那么使用字符串而不是int值就可以了。他们的优势在于他们的紧凑性。

Sage Advice:通常,如果有什么事情会受到伤害,请弄清楚如何做不到。