惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Webroot Blog
Webroot Blog
U
Unit 42
A
About on SuperTechFans
宝玉的分享
宝玉的分享
月光博客
月光博客
C
CERT Recently Published Vulnerability Notes
P
Privacy International News Feed
Microsoft Security Blog
Microsoft Security Blog
G
Google Developers Blog
P
Privacy & Cybersecurity Law Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
S
Securelist
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Spread Privacy
Spread Privacy
L
Lohrmann on Cybersecurity
Apple Machine Learning Research
Apple Machine Learning Research
K
Kaspersky official blog
Hugging Face - Blog
Hugging Face - Blog
B
Blog
I
Intezer
Last Week in AI
Last Week in AI
T
Threat Research - Cisco Blogs
V
V2EX
L
LangChain Blog
AI
AI
G
GRAHAM CLULEY
T
Tor Project blog
人人都是产品经理
人人都是产品经理
D
Docker
WordPress大学
WordPress大学
Google DeepMind News
Google DeepMind News
I
InfoQ
Y
Y Combinator Blog
C
Comments on: Blog
GbyAI
GbyAI
www.infosecurity-magazine.com
www.infosecurity-magazine.com
酷 壳 – CoolShell
酷 壳 – CoolShell
T
Tailwind CSS Blog
aimingoo的专栏
aimingoo的专栏
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
腾讯CDC
N
News and Events Feed by Topic
MyScale Blog
MyScale Blog
H
Help Net Security
Vercel News
Vercel News
T
Tenable Blog
博客园 - 三生石上(FineUI控件)
爱范儿
爱范儿

博客园 - AmyQiu

sql server远程备份和恢复 批编译、重新编译和计划缓存 转:改善SQL Server数据库的内存管理方法 转:SQL Server查询处理器机制与结构 转:影响SQL Server数据库性能设计 转:查询优化 转:SQL 语句优化 转:SQL SERVER什么时候写日志 转:SQL 物理查询原理 MDX实现同比和环比 SSIS2008组件使用测试 MDX查询几个经典示例 Cube的结构 MDX涉及的一些概念 - AmyQiu - 博客园 MDX中函数的应用 MDX中的Where vs. Subselect/Subcube MDX中一些边界问题的处理 MDX常见计算方法(百分比/分配/平均值/基于时间的计算) SQL查询SP代码
MDX优化Set操作
AmyQiu · 2010-04-07 · via 博客园 - AmyQiu

MDX优化Set操作—SUM中的CrossJoin

1. 优化Set操作的关键在于:把大的SET操作变成小的SET操作。
2. 由于CrossJoin代价(CPU、内存)巨大,所以最好用其他操作代替CrossJoin操作。
SUM中的CrossJoin
作者认为:要避免SUM一个包含多个CrossJoin的Set,你可以用其他的操作(比如嵌套SUM)进行替换。据此,我测试了一下
两组语句:

WITH MEMBER MEASURES.ABC AS 
Sum (
    CrossJoin (
        Descendants (
            
[Customer].[Customer Geography].CurrentMember,
            
[Customer].[Customer Geography].[State-Province]
        ),
        Crossjoin (
            Descendants (
                
[Date].[Calendar].CurrentMember,
                
[Date].[Calendar].[Date]
            ),
            Descendants (
                
[Product].[Product Categories].CurrentMember,
                
[Product].[Product Categories].[Product Name]
            )
        )
    )
    ,[Measures].[Internet Sales Amount]-[Measures].[Internet Tax Amount]

  )
SELECT MEASURES.ABC ON 0 ,
[Customer].[Customer Geography].[Country].Members *  
[Date].[Calendar].[Calendar Year].MEMBERS * 
[Product].[Product Categories].[Category].MEMBERS
ON 1
FROM [Adventure Works]

WITH MEMBER MEASURES.ABC AS 
Sum (
    Descendants (
        
[Customer].[Customer Geography].CurrentMember,
        
[Customer].[Customer Geography].[State-Province]
    ),        
    
SUM(
        Descendants (
            
[Product].[Product Categories].CurrentMember,
            
[Product].[Product Categories].[Product Name]
        ),
        
SUM(
            Descendants (
                
[Date].[Calendar].CurrentMember,
                
[Date].[Calendar].[Date]
            )        
            ,[Measures].[Internet Sales Amount]-[Measures].[Internet Tax Amount]

        )                
    )    
)
SELECT MEASURES.ABC ON 0 ,
{
[Customer].[Customer Geography].[Country].Members} *  
[Date].[Calendar].[Calendar Year].Members * 
[Product].[Product Categories].[Category].Members
ON 1
FROM [Adventure Works]

以上语句中,作者认为第一个语句慢于第二个语句(理由是嵌套的SUM每次操作的SET更小),可实际的结果(测了10次)恰恰相反,第一个语句平均花费的时间51.654秒,而第二个语句平均花费的时间在55.912秒,这是何故呢?此外,书中认为在第二个语句的嵌套SUM中,如果把大的Set放在里面,这样会快一些。也就是说下面的语句比上面第二个语句要慢5%-20%。

WITH MEMBER MEASURES.ABC AS 
Sum (
    Descendants (
        
[Date].[Calendar].CurrentMember,
        
[Date].[Calendar].[Date]
    ),
    
SUM(
        Descendants (
            
[Product].[Product Categories].CurrentMember,
            
[Product].[Product Categories].[Product Name]
        ),      
        
SUM(
        Descendants (
            
[Customer].[Customer Geography].CurrentMember,
            
[Customer].[Customer Geography].[State-Province]
        ),    
            
[Measures].[Internet Sales Amount]-[Measures].[Internet Tax Amount]        )                
    )    
)
SELECT MEASURES.ABC ON 0 ,
{
[Customer].[Customer Geography].[Country].Members} *  
[Date].[Calendar].[Calendar Year].Members * 
[Product].[Product Categories].[Category].Members
ON 1
FROM [Adventure Works]

以上测试语句中,关于[Date].[Calendar].[Date]的Set其Turple个数在365左右,关于[Product].[Product Categories].[Product Name]的Set其Turple个数在几十个左右,而关于[Customer].[Customer Geography].[State-Province]的Set其成员个数大多在十几个左右

经过测试发现上面这条语句平均时间在57.858秒。也就是说,测试结果和书中的观点是一致的,只是幅度没有那么大。此外,我还尝试了一下这样的写法。

WITH 
MEMBER MEASURES.ABC 
AS 
Sum (
    
    CrossJoin (
        Descendants (
            
[Customer].[Customer Geography].CurrentMember,
            
[Customer].[Customer Geography].[State-Province]
        ),
        Crossjoin (
            Descendants (
                
[Date].[Calendar].CurrentMember,
                
[Date].[Calendar].[Date]
            ),
            Descendants (
                
[Product].[Product Categories].CurrentMember,
                
[Product].[Product Categories].[Product Name]
            )
        )
    ) 
AS MYABC

    ,
[Measures].[Internet Sales Amount]
)
-
Sum (    
    MYABC
    ,
[Measures].[Internet Tax Amount]
)
SELECT MEASURES.ABC ON 0 ,
[Customer].[Customer Geography].[Country].Members *  
[Date].[Calendar].[Calendar Year].MEMBERS * 
[Product].[Product Categories].[Category].MEMBERS
ON 1
FROM [Adventure Works]

上面语句的不同之处在于,把要计算的内容分散开来了,令人惊异的是,这个语句只要2-3秒种就能运行完成。

总结
由上面两次测试我们可以得出以下结论:
1)SUM中的CrossJoin并不一定会降低速度,书中的观点可能是错误的。看来MDX解析器对CrossJoin有很多有优化,在上面的测试中CrossJoin比嵌套的SUM要快8%左右。
2)嵌套SUM中,把大的SET放在里层的SUM中,这样速度能够快一些。上面的测试中,把小的Set放在里层比把大的Set放在里层慢3.5%。
3)在做SUM等统计计算时,如果能够把计算项分解到每个单独的Measure,这个时候性能提升非常明显,速度将会大大提高。上面的测试中,速度提高了20多倍。