2015-03-31 92 views
0

我有如下一個MDX查詢:這個MDX可以優化嗎?

WITH 
MEMBER [Measures].[Percentage of All Accounts] AS [Measures].[SUM Principal EUR]/SUM({[Contract].[Product Group].&[P1], [Contract].[Product Group].&[P2], [Contract].[Product Group].&[P3]}, [Measures].[SUM Principal EUR]) 
SELECT 
    NON EMPTY 
    { 
     [Measures].[SUM Principal EUR], 
     [Measures].[SUM Principal USD], 
     [Measures].[SUM Principal Local Currency], 
     [Measures].[Percentage of All Accounts] 
    } ON COLUMNS, 
    NON EMPTY 
    { (
     [Contract].[Product Group].ALLMEMBERS, 
     [Dates].[YearMonth].ALLMEMBERS 
    ) } ON ROWS 
FROM 
(
    SELECT 
    { (
     STRTOSET(@ContractProductGroup, CONSTRAINED), 
     STRTOSET(@DatesYearMonth, CONSTRAINED) 
    ) } ON 0 
    FROM [Assets] 
) 
WHERE 
{ 
    { [Contract].[Unit].&[U1], [Contract].[Unit].&[U2], [Contract].[Unit].&[U3] }, 
    [Assets Cont].[Contract Status].&[C1] 
} 

可以這樣進行優化?

我知道你不能在你的機器上運行它,但我想知道在這個查詢中是否有某些東西不是很好的做法,或者它可以做得更好。

回答

1

您可以通過引入一些空值來使您的自定義度量更稀疏。
同樣在您的ON ROWS爲什麼不只是使用正常的交叉連接。
嘗試.MEMBERS而不是.ALLMEMBERS函數。
您可以嘗試在WITH子句中創建自定義設置:[nonEmpSet] 另外,我已將WHERE子句中的花括號更改爲元組花括號,因爲這是更標準的。

WITH 
    SET [nonEmpSet] AS 
    NonEmpty 
    (
     { 
     [Contract].[Product Group].MEMBERS * [Dates].[YearMonth].MEMBERS 
     }, 
    ,[Measures].[SUM Principal EUR] 
    ) 
    MEMBER [Measures].[Accounts NEW] AS 
    Aggregate 
    (
     { 
     [Contract].[Product Group].&[P1] 
     ,[Contract].[Product Group].&[P2] 
     ,[Contract].[Product Group].&[P3] 
     } 
    ,[Measures].[SUM Principal EUR] 
    ) 
    MEMBER [Measures].[Percentage of All Accounts] AS 
    IIF 
    (
     [Measures].[Accounts NEW] = 0 
    ,null 
    , 
     [Measures].[SUM Principal EUR]/[Measures].[Accounts NEW] 
    ) 
SELECT 
    NON EMPTY 
    { 
     [Measures].[SUM Principal EUR] 
    ,[Measures].[SUM Principal USD] 
    ,[Measures].[SUM Principal Local Currency] 
    ,[Measures].[Percentage of All Accounts] 
    } ON COLUMNS 
,NON EMPTY 
    [nonEmpSet] ON ROWS 
FROM 
(
    SELECT 
    { 
     (
     StrToSet 
     (@ContractProductGroup 
     ,CONSTRAINED 
     ) 
     ,StrToSet 
     (@DatesYearMonth 
     ,CONSTRAINED 
     ) 
    ) 
    } ON 0 
    FROM [Assets] 
) 
WHERE 
    (
    { 
     [Contract].[Unit].&[U1] 
    ,[Contract].[Unit].&[U2] 
    ,[Contract].[Unit].&[U3] 
    } 
    ,[Assets Cont].[Contract Status].&[C1] 
); 
+0

謝謝您的建議。我剛剛做到了這一點,但不幸的是沒有注意到任何時間的改進。 – irusul 2015-03-31 14:20:11

+0

@irusul - 讓我編輯我的腳本,因爲我認爲我可以看到一個問題 - 您的「ON ROWS」代碼很奇怪 - 爲什麼不直接進行正常的交叉連接? – whytheq 2015-03-31 14:52:50

+0

這是交叉連接的另一種方式,無論如何,性能是相同的。 – irusul 2015-03-31 14:59:26

0

如果不是一個問題,創建如下立方體級成員:

CREATE 
MEMBER [Measures].[Percentage of All Accounts] AS NULL; 

SCOPE(
{ 
    { [Contract].[Unit].&[U1], [Contract].[Unit].&[U2], [Contract].[Unit].&[U3] }, 
    [Assets Cont].[Contract Status].&[C1] 
}); 

[Measures].[Percentage of All Accounts] = 
Measures].[SUM Principal EUR] 
/
SUM({[Contract].[Product Group].&[P1], [Contract].[Product Group].&[P2], [Contract].[Product Group].&[P3]}, [Measures].[SUM Principal EUR]); 

END SCOPE; 

另外,在查詢中使用的NonEmpty()功能擺脫不必要crossjoins的。

SELECT 
    NON EMPTY 
    { 
     [Measures].[SUM Principal EUR], 
     [Measures].[SUM Principal USD], 
     [Measures].[SUM Principal Local Currency], 
     [Measures].[Percentage of All Accounts] 
    } ON COLUMNS, 
    NON EMPTY 
    NonEmpty(
     [Contract].[Product Group].ALLMEMBERS, 
     [Dates].[YearMonth].ALLMEMBERS 
    ) ON ROWS 
FROM 
(
    SELECT 
    { (
     STRTOSET(@ContractProductGroup, CONSTRAINED), 
     STRTOSET(@DatesYearMonth, CONSTRAINED) 
    ) } ON 0 
    FROM [Assets] 
) 

編輯 - WHERE子句,而不是子選擇

WITH MEMBER [Measures].[Percentage of All Accounts] AS 
[Measures].[SUM Principal EUR] 
/
SUM({[Contract].[Product Group].&[P1], [Contract].[Product Group].&[P2], [Contract].[Product Group].&[P3]}, [Measures].[SUM Principal EUR]) 

SELECT 
    NON EMPTY 
    { 
     [Measures].[SUM Principal EUR], 
     [Measures].[SUM Principal USD], 
     [Measures].[SUM Principal Local Currency], 
     [Measures].[Percentage of All Accounts] 
    } ON COLUMNS, 
    NON EMPTY 
    { (
     [Contract].[Product Group].ALLMEMBERS, 
     [Dates].[YearMonth].ALLMEMBERS 
    ) } ON ROWS 

    FROM [Assets] 
WHERE 
{ 
    { [Contract].[Unit].&[U1], [Contract].[Unit].&[U2], [Contract].[Unit].&[U3] }, 
    [Assets Cont].[Contract Status].&[C1], 
    STRTOSET(@ContractProductGroup, CONSTRAINED), 
    STRTOSET(@DatesYearMonth, CONSTRAINED) 
} 

EDIT 2

WITH MEMBER [Measures].[Percentage of All Accounts] AS 
[Measures].[SUM Principal EUR] 
/
SUM({[Contract].[Product Group].&[P1], [Contract].[Product Group].&[P2], [Contract].[Product Group].&[P3]}, [Measures].[SUM Principal EUR]) 

SELECT 
    NON EMPTY 
    { 
     [Measures].[SUM Principal EUR], 
     [Measures].[SUM Principal USD], 
     [Measures].[SUM Principal Local Currency], 
     [Measures].[Percentage of All Accounts] 
    } ON COLUMNS, 
    NON EMPTY 
    { (
     STRTOSET(@ContractProductGroup, CONSTRAINED), 
     STRTOSET(@DatesYearMonth, CONSTRAINED) 
    ) } ON ROWS 

    FROM [Assets] 
WHERE 
{ 
    { [Contract].[Unit].&[U1], [Contract].[Unit].&[U2], [Contract].[Unit].&[U3] }, 
    [Assets Cont].[Contract Status].&[C1] 
} 
+0

行上的NONEMPTY沒有幫助。我無法創建立方體級別的成員,我有更多的成員(此處未顯示)。 – irusul 2015-03-31 15:00:23

+0

@irusul如何從兩個軸上徹底擺脫'NON EMPTY'? – SouravA 2015-03-31 15:12:52

+0

也嘗試上面的編輯 – SouravA 2015-03-31 15:14:48