2016-11-03 156 views
0

我有一個查詢,它使用傳遞給查詢的參數爲當前月份&年建立一個月迄今和今年的SSRS報告。問題在於,由於有太多的客戶,零件號碼等,查詢花費的時間太長而無法執行。有什麼方法可以更好地編寫查詢或加快速度。對不起,我對此很新。下面是查詢...慢MDX查詢

WITH 
    MEMBER [Measures].[Key for Today] AS 
    Format 
    (
     Now(),'yyyyMMdd' 
    ) 
    MEMBER [Measures].[Today string] AS 
    '[Date].[Dates].[Day].&[' + [Measures].[Key for Today] + ']' 

    MEMBER [Measures].[Quantity Shipped MTD] AS 
    Sum 
    (
     MTD([Date].[Dates].CurrentMember) 
    ,[Measures].[Quantity Shipped] 
    ) 
    MEMBER [Measures].[Quantity Shipped YTD] AS 
    Sum 
    (
     YTD([Date].[Dates].CurrentMember) 
    ,[Measures].[Quantity Shipped] 
    ) 
    MEMBER [Measures].[Sales Amount MTD] AS 
    Sum 
    (
     MTD([Date].[Dates].CurrentMember) 
    ,[Measures].[Sales Amount] 
    ) 
    MEMBER [Measures].[Sales Amount YTD] AS 
    Sum 
    (
     YTD([Date].[Dates].CurrentMember) 
    ,[Measures].[Sales Amount] 
    ) 
    MEMBER [Measures].[Cost Amount MTD] AS 
    Sum 
    (
     MTD([Date].[Dates].CurrentMember) 
    ,[Measures].[Cost Amount - Sales] 
    ) 
    MEMBER [Measures].[Cost Amount YTD] AS 
    Sum 
    (
     YTD([Date].[Dates].CurrentMember) 
    ,[Measures].[Cost Amount - Sales] 
    ) 
    MEMBER [Measures].[Sales Margin MTD] AS 
    Sum 
    (
     MTD([Date].[Dates].CurrentMember) 
    ,[Measures].[Sales Margin] 
    ) 
    MEMBER [Measures].[Sales Margin YTD] AS 
    Sum 
    (
     YTD([Date].[Dates].CurrentMember) 
    ,[Measures].[Sales Margin] 
    ) 
MEMBER [Measures].[Forecast Quantity MTD] AS 
    Sum 
    (
     MTD([Date].[Dates].CurrentMember) 
    ,[Measures].[Forecast Quantity] 
    ) 
    MEMBER [Measures].[Forecast Quantity YTD] AS 
    Sum 
    (
     YTD([Date].[Dates].CurrentMember) 
    ,[Measures].[Forecast Quantity] 
    ) 
    MEMBER [Measures].[Forecast Turnover MTD] AS 
    Sum 
    (
     MTD([Date].[Dates].CurrentMember) 
    ,[Measures].[Forecast Total Turnover] 
    ) 
    MEMBER [Measures].[Forecast Turnover YTD] AS 
    Sum 
    (
     YTD([Date].[Dates].CurrentMember) 
    ,[Measures].[Forecast Total Turnover] 
    ) 
    MEMBER [Measures].[Forcast Cost MTD] AS 
    Sum 
    (
     MTD([Date].[Dates].CurrentMember) 
    ,[Measures].[Forecast Total Cost] 
    ) 
    MEMBER [Measures].[Forecast Cost YTD] AS 
    Sum 
    (
     YTD([Date].[Dates].CurrentMember) 
    ,[Measures].[Forecast Total Cost] 
    ) 
    MEMBER [Measures].[Forecast Margin MTD] AS 
    Sum 
    (
     MTD([Date].[Dates].CurrentMember) 
    ,[Measures].[Forecast Margin] 
    ) 
    MEMBER [Measures].[Forecast Margin YTD] AS 
    Sum 
    (
     YTD([Date].[Dates].CurrentMember) 
    ,[Measures].[Forecast Margin] 
    ) 
    MEMBER [Measures].[Budget Quantity MTD] AS 
    Sum 
    (
     MTD([Date].[Dates].CurrentMember) 
    ,[Measures].[Budget Quantity] 
    ) 
    MEMBER [Measures].[Budget Quantity YTD] AS 
    Sum 
    (
     YTD([Date].[Dates].CurrentMember) 
    ,[Measures].[Budget Quantity] 
    ) 
    MEMBER [Measures].[Budget Turnover MTD] AS 
    Sum 
    (
     MTD([Date].[Dates].CurrentMember) 
     ,[Measures].[Budget Total Turnover] 
    ) 
    MEMBER [Measures].[Budget Turnover YTD] AS 
    Sum 
    (
     YTD([Date].[Dates].CurrentMember) 
     ,[Measures].[Budget Total Turnover] 
    ) 
    MEMBER [Measures].[Budget Cost MTD] AS 
    Sum 
    (
     MTD([Date].[Dates].CurrentMember) 
     ,[Measures].[Budget Total Cost] 
    ) 
    MEMBER [Measures].[Budget Cost YTD] AS 
    Sum 
    (
     YTD([Date].[Dates].CurrentMember) 
     ,[Measures].[Budget Total Cost] 
    ) 
    MEMBER [Measures].[Budget Margin MTD] AS 
    Sum 
    (
     MTD([Date].[Dates].CurrentMember) 
     ,[Measures].[Budget Margin] 
    ) 
    MEMBER [Measures].[Budget Margin YTD] AS 
    Sum 
    (
     YTD([Date].[Dates].CurrentMember) 
     ,[Measures].[Budget Margin] 
    ) 
SELECT 
    { 
    [Measures].[Quantity Shipped MTD], 
    [Measures].[Sales Amount MTD], 
    [Measures].[Cost Amount MTD], 
    [Measures].[Sales Margin MTD], 
    [Measures].[Margin %], 
    [Measures].[Forecast Quantity MTD], 
    [Measures].[Forecast Turnover MTD], 
    [Measures].[Forcast Cost MTD], 
    [Measures].[Forecast Margin MTD], 
    [Measures].[Margin %], 
    [Measures].[Budget Quantity MTD], 
    [Measures].[Budget Turnover MTD], 
    [Measures].[Budget Cost MTD], 
    [Measures].[Budget Margin MTD], 
    [Measures].[Margin %], 
    [Measures].[Quantity Shipped YTD], 
    [Measures].[Sales Amount YTD], 
    [Measures].[Cost Amount YTD], 
    [Measures].[Sales Margin YTD], 
    [Measures].[Margin %], 
    [Measures].[Forecast Quantity YTD], 
    [Measures].[Forecast Turnover YTD], 
    [Measures].[Forecast Cost YTD], 
    [Measures].[Forecast Margin YTD], 
    [Measures].[Margin %], 
    [Measures].[Budget Quantity YTD], 
    [Measures].[Budget Turnover YTD], 
    [Measures].[Budget Cost YTD], 
    [Measures].[Budget Margin YTD], 
    [Measures].[Margin %] 
    } ON COLUMNS, 
    NON EMPTY { (
[Customer].[Customer].[Customer].ALLMEMBERS * 
[Customer Sales].[Summary Prod Group 1].[Summary Prod Group 1].ALLMEMBERS * 
[Customer Sales].[Vehicle Mode].[Vehicle Mode].ALLMEMBERS * 
[Customer Sales].[Part Number].[Part Number].ALLMEMBERS) } 
ON ROWS 
FROM (SELECT (STRTOSET(@SummaryProdGroup, CONSTRAINED)) ON COLUMNS 
FROM (SELECT (STRTOSET(@SalesSummaryCode, CONSTRAINED)) ON COLUMNS 
FROM (SELECT (STRTOSET(@BusinessType, CONSTRAINED)) ON COLUMNS FROM [Sales]))) 
Where((
StrToMember 
    (
     [Measures].[Today string],constrained 
    )), 
IIF(STRTOSET(@BusinessType, CONSTRAINED).Count = 1, STRTOSET(@BusinessType, CONSTRAINED), [Customer].[Business Type].currentmember), 
IIF(STRTOSET(@SalesSummaryCode, CONSTRAINED).Count = 1, STRTOSET(@SalesSummaryCode, CONSTRAINED), [Customer].[Sales Summary Code].currentmember)) 

謝謝

+0

您是否嘗試過真正簡化它,然後添加一個部分,然後重新運行,再添加一點,然後重新運行等,試圖找出瓶頸在哪裏? – whytheq

+0

我發現不尋常的一件事是在'WHERE'子句中使用'.currentmember'。 – whytheq

回答

0

我會忍不住去嘗試了以下各點出你的WHERE條款:

StrToMember 
    (
     [Measures].[Today string],constrained 
    ) 

所以WITH條款便可設爲如下所示:

WITH 
    MEMBER [Measures].[Key for Today] AS 
    Format 
    (
     Now(),'yyyyMMdd' 
    ) 
    MEMBER [Measures].[Today string] AS 
    '[Date].[Dates].[Day].&[' + [Measures].[Key for Today] + ']' 
    SET [today] AS 
    StrToSet([Measures].[Today string],constrained) 
    MEMBER [Measures].[Quantity Shipped MTD] AS 
    Sum 
    (
     MTD([today].ITEM(0)) 
    ,[Measures].[Quantity Shipped] 
    ) 
    MEMBER [Measures].[Quantity Shipped YTD] AS 
    Sum 
    (
     YTD([today].ITEM(0)) 
    ,[Measures].[Quantity Shipped] 
    ) 
    ... 
    ... 
+0

嗨whytheq。我嘗試了上面的建議,除了您建議的解決方案正在通過其他不應該存在的數據帶來的解決方案之外,它與原始腳本需要的時間相同。減少查詢速度非常糟糕的是[部件號]上的交叉連接。沒有它,查詢需要5秒鐘運行。使用零件號碼,最多需要45分鐘。問題是,零件號碼是最重要的,我們在數據庫中有很多零件號碼。 – GarethS