Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

django - Any Python OLAP/MDX ORM engines?

I'm new to the MDX/OLAP and I'm wondering if there is any ORM similar like Django ORM for Python that would support OLAP.

I'm a Python/Django developer and if there would be something that would have some level of integration with Django I would be much interested in learning more about it.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Django has some OLAP features that are nearing release.

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

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

If you have a proper star schema design in the first place, then one-dimensional results can have the following form.

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

If you want to create a two-dimensional summary, you have to do something like the following.

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

To compute multiple SUM's and COUNT's and what-not, you have to do something like this.

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

This -- at first blush -- seems inefficient. You're trolling through the fact table returning lots of rows which you are then aggregating in your application.

In some cases, this may be faster than the RDBMS's native sum/group_by. Why? You're using a simple mapping, not the more complex sort-based grouping operation that the RDBMS often has to use for this. Yes, you're getting a lot of rows; but you're doing less to get them.

This has the disadvantage that it's not so declarative as we'd like. It has the advantage that it's pure Django ORM.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...