问题
I was looking at this post in MSDN: http://msdn.microsoft.com/en-us/library/ms145493.aspx
It states that + and Union are equivalent ... not exactly in the following as Aggregate needs to be applied if I choose to use Union.
It's more readable but is there any performance gain in choosing + over Aggregate(Union ?
WITH
MEMBER [Employee].[Employee].[blah] AS
[Employee].[Employee].[Amy E. Alberts]
+
[Employee].[Employee].[Garrett R. Vargas]
MEMBER [Employee].[Employee].[blahblah] AS
Aggregate
(
Union
(
{[Employee].[Employee].[Amy E. Alberts]}
,{[Employee].[Employee].[Garrett R. Vargas]}
)
)
SELECT
[Measures].[Reseller Sales Amount] ON 0
,{
[Employee].[Employee].[Amy E. Alberts]
,[Employee].[Employee].[Garrett R. Vargas]
,[Employee].[Employee].[blah]
,[Employee].[Employee].[blahblah]
} ON 1
FROM [Adventure Works]
WHERE
[Date].[Calendar].[Month].&[2006]&[8];
回答1:
The issue is that + is used for several purposes in MDX: as numerical addition, for string concatenation, and as union operator. And as the a member definition expects an expression that delivers a value and you use the operator between two members, and the value of the current measure is numerical, AS uses numerical addition.
If you enclose the members in braces:
MEMBER [Employee].[Employee].[blah] AS
{[Employee].[Employee].[Amy E. Alberts]}
+
{[Employee].[Employee].[Garrett R. Vargas] }
you get an error that AS expects string or numeric expressions for the + operator.
And there is probably a small performance gain, but the result may be different if the current measure is not aggregated by sum, which would be equivalent to adding both member values in this case.
I personally do not like this operator overloading of +, * etc. for different purposes. + and * should be restricted to numbers, be it integers, floats, or decimals. I also would also prefer another operator for string concatenation than + in Java, JavaScript, and .net, as well as Microsoft SQL and MDX. Standard SQL uses ||, Perl uses ., so there are alternatives that clearly avoid ambiguities and you clearly know what to expect when you write '2' || 1 vs. '2' + 1 vs. vs. 2 + '1' vs. 2 + 1.
来源:https://stackoverflow.com/questions/26886382/is-there-a-performance-gain-using-rather-than-aggregateunion