开发者

Performance Problems with Django's F() Object

Has anyone else noticed performance issues using Django's F() object? I am running Windows XP SP3 and developing against the Django trunk. A snippet of the models I'm using and the query I'm building are below. When I have the F() object in place, each call to a QuerySet method (e.g. filter, exclude, order_by, distinct, etc.) takes approximately 2 seconds, but when I comment out the F() clause the calls are sub-second. I had a co-worker test it on his Ubuntu machine, and he is not experiencing the same performance issues I am with the F() clause.

Anyone else seeing this behavior?

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(
        开发者_StackOverflowmax_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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜