2009-01-22 43 views
7

我是新來的MDX/OLAP,我想知道是否有任何類似Django ORM的Python支持OLAP。任何Python OLAP/MDX ORM引擎?

我是一名Python/Django開發人員,如果會有某種程度會與Django進行一定程度的集成,我會對更多地瞭解它感興趣。

回答

6

Django有一些即將發佈的OLAP功能。

閱讀http://www.eflorenzano.com/blog/post/secrets-django-orm/

http://doughellmann.com/2007/12/30/using-raw-sql-in-django.html,也

如果你有在首位正確的星型架構設計,則一維的結果可能有以下形式。

from myapp.models import SomeFact 
from collections import defaultdict 

facts = SomeFact.objects.filter(dimension1__attribute=this, dimension2__attribute=that) 
myAggregates = defaultdict(int) 
for row in facts: 
    myAggregates[row.dimension3__attribute] += row.someMeasure 

如果你想創建一個二維的總結,你必須做如下的事情。

facts = SomeFact.objects.filter(dimension1__attribute=this, dimension2__attribute=that) 
myAggregates = defaultdict(int) 
for row in facts: 
    key = (row.dimension3__attribute, row.dimension4__attribute) 
    myAggregates[key] += row.someMeasure 

要計算多SUM的和COUNT的和什麼,而不是,你必須做這樣的事情。

class MyAgg(object): 
    def __init__(self): 
     self.count = 0 
     self.thisSum= 0 
     self.thatSum= 0 

myAggregates= defaultdict(MyAgg) 
for row in facts: 
    myAggregates[row.dimension3__attr].count += 1 
    myAggregates[row.dimension3__attr].thisSum += row.this 
    myAggregates[row.dimension3__attr].thatSum += row.that 

這 - 初看起來 - 似乎效率低下。你正在通過事實表來回顧你在應用程序中聚合的很多行。

在某些情況下,這可能是比RDBMS的原生sum/group_by更快。爲什麼?您正在使用一個簡單的映射,而不是RDBMS經常必須使用的更復雜的基於排序的分組操作。是的,你得到很多行;但你做得不那麼容易。

這有缺點,它不是我們想要的。它的優點是它是純粹的Django ORM。

+0

我想知道這將如何更新以利用Djangos最近包含的對多個數據庫的支持: http://docs.djangoproject.com/en/dev/topics/db/multi-db/ – fccoelho 2010-01-23 08:02:27

0

我有一個類似的需求 - 不是一個完整的ORM,而是一個簡單的OLAP類似的Python數據存儲。上來幹搜索現有的工具後,我寫了這個小黑客:

https://github.com/kpwebb/python-cube/blob/master/src/cube.py

即使它不解決您的具體需求,這可能是寫一些更復雜的一個很好的起點。