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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…