2017-04-27 53 views
-1
SELECT 
    B.AccountBranchID 
    ,B.VoucherNo 
    ,B.BranchName AS BranchName 
    ,B.InvoiceNo 
    ,CONVERT(VARCHAR, B.InvoiceDate, 103) AS InvoiceDate 
    ,CONVERT(VARCHAR, B.VoucherDate, 103) AS VoucherDate 
    ,B.CustomerName 
    ,B.RefID 
    ,LN.AccountName AS LedgerName 
    ,b.SalesPersonName AS SalesPersonName 
    ,LN.LedgerCode 
    ,B.AgentName 
    ,B.ShipperName 
    ,B.Segment 
    ,B.TransactionType 
    ,B.JobNo 
    ,CONVERT(VARCHAR, B.JOBDate, 103) AS JOBDate 
    ,B.MAWBNo 
    ,B.HAWBNo 
    ,B.AccountName 
    ,B.LedgerCode AS AccountLedgerCode 
    ,B.CurrencyCode 
    ,ISNULL(B.Amount, 0) AS Amount 
    ,B.ChargeExRate 
    ,(CASE B.CRDR 
     WHEN 'CR' THEN (B.ChargeBaseAmount * -1) 
     ELSE B.ChargeBaseAmount 
    END) AS ChargeBaseAmount 
    ,(CASE B.CRDR 
     WHEN 'CR' THEN 'Credit' 
     ELSE 'Debit' 
    END) AS CRDR 

FROM VW_VoucherTR AS B 
INNER JOIN VW_VoucherTR AS LN 
    ON B.VoucherID = LN.VoucherID 
WHERE B.CompanyID = @CompanyID 
AND (CASE @Type 
    WHEN 'I' THEN B.InvoiceDate 
    ELSE B.VoucherDate 
END) BETWEEN ISNULL(@FromDate, (SELECT 
     FYearStart 
    FROM Secmst_FinancialYear 
    WHERE FyearId = @yearID) 
) AND ISNULL(@ToDate, GETDATE()) 
AND (@Segment IS NULL 
OR B.Segment = @Segment) 
AND (@BranchMappingID IS NULL 
OR B.BranchMappingID = @BranchMappingID) 
AND B.VoucherTypeCode IN ('sv') 
AND B.IsDeleted = 0 
AND (B.GroupName <> 'Sundry Creditors' 
AND B.GroupName <> 'Sundry Debtors') 
AND LN.GroupName IN ('Sundry Debtors', 'Sundry Creditors') 
+0

請格式化上述查詢。 – Wintergreen

+0

格式化查詢,然後將索引添加到聯接列(如果尚未存在)。 –

+0

格式格式格式!!! – abhiarora

回答

0

BETWEEN中的子查詢可能是什麼殺死你。你看過執行計劃嗎?

BETWEEN ISNULL(@FromDate, ( SELECT FYearStart FROM Secmst_FinancialYear WHERE FyearId = @yearID )) AND ISNULL(@ToDate, GETDATE())

發生了什麼事是你正在運行跨每一行該查詢,並通過我的長相,這是不必要的,因爲你只需要靜態日期還有(基於加入行沒有什麼。)

試試這個:

DECLARE @FromDateActual datetime = ISNULL(@FromDate, (
       SELECT FYearStart 
       FROM Secmst_FinancialYear 
       WHERE FyearId = @yearID 
       )); 
DECLARE @ToDateActual datetime = ISNULL(@ToDate, GETDATE()); 

SELECT B.AccountBranchID 
    ,B.VoucherNo 
    ,B.BranchName AS BranchName 
    ,B.InvoiceNo 
    ,convert(VARCHAR, B.InvoiceDate, 103) AS InvoiceDate 
    ,convert(VARCHAR, B.VoucherDate, 103) AS VoucherDate 
    ,B.CustomerName 
    ,B.RefID 
    ,LN.AccountName AS LedgerName 
    ,b.SalesPersonName AS SalesPersonName 
    ,LN.LedgerCode 
    ,B.AgentName 
    ,B.ShipperName 
    ,B.Segment 
    ,B.TransactionType 
    ,B.JobNo 
    ,convert(VARCHAR, B.JOBDate, 103) AS JOBDate 
    ,B.MAWBNo 
    ,B.HAWBNo 
    ,B.AccountName 
    ,B.LedgerCode AS AccountLedgerCode 
    ,B.CurrencyCode 
    ,ISNULL(B.Amount, 0) AS Amount 
    ,B.ChargeExRate 
    ,(
     CASE B.CRDR 
      WHEN 'CR' 
       THEN (B.ChargeBaseAmount * - 1) 
      ELSE B.ChargeBaseAmount 
      END 
     ) AS ChargeBaseAmount 
    ,(
     CASE B.CRDR 
      WHEN 'CR' 
       THEN 'Credit' 
      ELSE 'Debit' 
      END 
     ) AS CRDR 
FROM VW_VoucherTR AS B 
INNER JOIN VW_VoucherTR AS LN ON B.VoucherID = LN.VoucherID 
WHERE B.CompanyID = @CompanyID 
    AND (
     CASE @Type 
      WHEN 'I' 
       THEN B.InvoiceDate 
      ELSE B.VoucherDate 
      END 
     ) BETWEEN @FromDateActual 
     AND @ToDateActual 
    AND (
     @Segment IS NULL 
     OR B.Segment = @Segment 
     ) 
    AND (
     @BranchMappingID IS NULL 
     OR B.BranchMappingID = @BranchMappingID 
     ) 
    AND B.VoucherTypeCode IN ('sv') 
    AND B.IsDeleted = 0 
    AND (
     B.GroupName <> 'Sundry Creditors' 
     AND B.GroupName <> 'Sundry Debtors' 
     ) 
    AND LN.GroupName IN (
     'Sundry Debtors' 
     ,'Sundry Creditors' 
     ) 

除此之外,你可以考慮添加非聚集索引。查詢分析器甚至可能會建議一對夫婦。但是,在這裏需要小心,這取決於數據的使用和加載方式,因爲太多的索引或大的索引可能會導致其他位置出現進一步的性能問題(行插入,頁面碎片等)。

+0

它採取幾乎相同的時間執行沒有影響後,你有什麼建議給我:(是我需要做的任何其他更改? – bharat

0

可能有很多原因,但有一點很簡單。以下部分不是sargable

(CASE @Type 
    WHEN 'I' THEN B.InvoiceDate 
    ELSE B.VoucherDate 
END) BETWEEN ISNULL(@FromDate, (SELECT 
     FYearStart 
    FROM Secmst_FinancialYear 
    WHERE FyearId = @yearID) 
) AND ISNULL(@ToDate, GETDATE()) 

應該重寫爲sargable,以便可以使用索引。

SELECT @FromDate = ISNULL(@FromDate, (SELECT 
     TOP 1 FYearStart 
     FROM Secmst_FinancialYear 
     WHERE FyearId = @yearID))) 

SELECT @ToDate = ISNULL(@ToDate, GETDATE()) 

SELECT 
... 
WHERE 
... 
AND 
((@Type='I' AND B.InvoiceDate BETWEEN @FromDate AND @ToDate) 
OR 
(@Type<>'I' AND B.VoucherDate BETWEEN @FromDate AND @ToDate)) 
AND 
... 

當然適當的索引是如何加快您的查詢,如果沒有索引上InvoiceDate,VoucherDate等那麼你的查詢將使用全表掃描而不是索引查找,這將是緩慢的方式。

+0

noooo它不工作任何如何plxz幫我出 – bharat

+0

@bharat你也可以嘗試一個通過@類型變量=我'B.InvoiceDate 之間ISNULL(@FromDate,(SELECT FYearStart FROM Secmst_FinancialYear WHERE FyearId = @yearID) )和ISNULL(@ToDate,GETDATE())'和看到發生了什麼 –

+0

@VojtěchDohnal no sir ....它增加我的時間不與這一個工作 – bharat

相關問題