有没有人注意到使用Django的F()对象的性能问题?我正在运行Windows XP SP3并针对Django主干进行开发。我正在使用的模型片段和我正在构建的查询如下。当我有F()对象时,每次调用QuerySet方法(例如filter,exclude,order_by,distinct等)大约需要2秒,但是当我注释掉F()子句时,调用是第二。我有一个同事在他的Ubuntu机器上测试它,他没有遇到与F()子句相同的性能问题。
其他人看到这种行为?
class Move (models.Model):
state_meaning = models.CharField(
max_length=16,
db_index=True,
blank=True,
default=''
)
drop = models.ForeignKey(
Org,
db_index=True,
null=False,
default=1,
related_name='as_move_drop'
)
class Split(models.Model):
state_meaning = models.CharField(
max_length=16,
db_index=True,
blank=True,
default=''
)
move = models.ForeignKey(
Move,
related_name='splits'
)
pickup = models.ForeignKey(
Org,
db_index=True,
null=False,
default=1,
related_name='as_split_pickup'
)
pickup_date = models.DateField(
null=True,
default=None
)
drop = models.ForeignKey(
Org,
db_index=True,
null=False,
default=1,
related_name='as_split_drop'
)
drop_date = models.DateField(
null=True,
default=None,
db_index=True
)
def get_splits(begin_date, end_date):
qs = Split.objects \
.filter(state_meaning__in=['INPROGRESS','FULFILLED'],
drop=F('move__drop'), # <<< the line in question
pickup_date__lte=end_date)
elapsed = timer.clock() - start
print 'qs1 took %.3f' % elapsed
start = timer.clock()
qs = qs.filter(Q(drop_date__gte=begin_date) |
Q(drop_date__isnull=True))
elapsed = timer.clock() - start
print 'qs2 took %.3f' % elapsed
start = timer.clock()
qs = qs.exclude(move__state_meaning='UNFULFILLED')
elapsed = timer.clock() - start
print 'qs3 took %.3f' % elapsed
start = timer.clock()
qs = qs.order_by('pickup_date', 'drop_date')
elapsed = timer.clock() - start
print 'qs7 took %.3f' % elapsed
start = timer.clock()
qs = qs.distinct()
elapsed = timer.clock() - start
print 'qs8 took %.3f' % elapsed