我有一个很长的复杂查询,其中包含很多计算和条件,但主要结构如下所示:
WITH
MEMBER [Id1] AS [Level].[Level1].CurrentMember.Member_Key
MEMBER [Id2] AS [Level].[Level2].CurrentMember.Member_Key
MEMBER [Level].[Level1].[FirstSet] AS NULL
MEMBER [Level].[Level1].[SecondSet] AS NULL
SET [Set 1] AS {some processed set members}
SET [Set 2] AS {some other processed set members}
SET [Common CrossJoin Set] AS [Level].[Level2].Members
MEMBER [Calculated Measure 1] AS
IIF([Level].[Level].CurrentMember.Member_Key = 'FirstSet',
SUM(existing [Set 1]),
IIF([Level].[Level].CurrentMember.Member_Key = 'SecondSet',
SUM(existing [Set 2]),
SUM([Measures].[Measure1]) * 15
)
)
MEMBER [Calculated Measure 2] AS
IIF([Level].[Level].CurrentMember.Member_Key = 'FirstSet',
SUM(existing [Set 1]),
IIF([Level].[Level].CurrentMember.Member_Key = 'SecondSet',
SUM(existing [Set 2]),
SUM([Measures].[Measure2]) * 20
)
)
SELECT
{ [Id1], [Id2], [Calculated Measure 1], [Calculated Measure 2]} ON COLUMNS,
{ ([Common CrossJoin Set], [Level].[Level1].[FirstSet]),
([Common CrossJoin Set], [Level].[Level1].[SecondSet])
} ON ROWS
FROM [Cube]
所以结果表如下所示:
║ ---------------║ ----------------------------║ Id1 ║ Id2 ║ Measure1 ║测量2║</p>
║ L2 成员 ║ L1.FirstSet 成员 ║ L2-1 ║ L1-8 ║ 1 ║ 5 ║</p>
║ L2 成员 ║ L1.FirstSet 成员 ║ L2-2 ║ L1-9 ║ 2 ║ 6 ║</p>
║ L2 成员 ║ L1.SecondSet 成员 ║ L2-3 ║ L1-98 ║ 3 ║ 7 ║</p>
║ L2 成员 ║ L1.SecondSet 成员 ║ L2-4 ║ L1-99 ║ 4 ║ 8 ║</p>
结果是正确的,但查询很慢(> 4 秒)。我的实际查询更大并且包含很多这样的集合和度量,所以看起来问题出在现有功能和整体结构中,阻止引擎执行内部优化。
这种解决方案是错误的和丑陋的,但是我怎样才能重写它并更快地得到相同的结果呢?