什么_really_缓存Django QuerySet?

时间:2012-12-31 00:48:02

标签: django django-models django-queryset

根据(我的阅读)官方dox在这里:

https://docs.djangoproject.com/en/dev/ref/models/querysets/#when-querysets-are-evaluated

在评估Django QuerySet时应该缓存它。但事实似乎并非如此。在下面的示例中,TrackingImport是一个模型,后面有一个非常大的表。 (为简洁起见,略微编辑了输出。)

recs = TrackingImport.objects.filter(...stuff...)

In [102]: time(recs[0])
Wall time: 1.84 s

In [103]: time(recs[0])
Wall time: 1.84 s

调用len()似乎与宣传的一样:

In [104]: len(recs)
Out[104]: 1823

In [105]: time(recs[0])
Wall time: 0.00 s

我不明白为什么解除引用数组没有缓存QuerySet结果。它必须评估它,对吧?那我错过了什么?

1 个答案:

答案 0 :(得分:7)

你可以浏览源代码(django.db.model.query),然后你就会清楚,这里是django 1.3.4的query.py,

def __getitem__(self, k):
    """
    Retrieves an item or slice from the set of results.
    """
    if not isinstance(k, (slice, int, long)):
        raise TypeError
    assert ((not isinstance(k, slice) and (k >= 0))
            or (isinstance(k, slice) and (k.start is None or k.start >= 0)
                and (k.stop is None or k.stop >= 0))), \
            "Negative indexing is not supported."

    if self._result_cache is not None:
        if self._iter is not None:
            # The result cache has only been partially populated, so we may
            # need to fill it out a bit more.
            if isinstance(k, slice):
                if k.stop is not None:
                    # Some people insist on passing in strings here.
                    bound = int(k.stop)
                else:
                    bound = None
            else:
                bound = k + 1
            if len(self._result_cache) < bound:
                self._fill_cache(bound - len(self._result_cache))
        return self._result_cache[k]

    if isinstance(k, slice):
        qs = self._clone()
        if k.start is not None:
            start = int(k.start)
        else:
            start = None
        if k.stop is not None:
            stop = int(k.stop)
        else:
            stop = None
        qs.query.set_limits(start, stop)
        return k.step and list(qs)[::k.step] or qs
    try:
        qs = self._clone()
        qs.query.set_limits(k, k + 1)
        return list(qs)[0]
    except self.model.DoesNotExist, e:
        raise IndexError(e.args)

当您不遍历查询集时,_result_cache为None,那么当您调用resc [0]时,它将跳到以下行,

try:
   qs = self._clone()
   qs.query.set_limits(k, k + 1)
   return list(qs)[0]
except self.model.DoesNotExist, e:
   raise IndexError(e.args)

你会发现,在这种情况下,没有设置_result_cache。这就是为什么多个resc [0]的持续时间相同的原因。

调用len(resc)后,您可以找到源代码

def __len__(self):
    # Since __len__ is called quite frequently (for example, as part of
    # list(qs), we make some effort here to be as efficient as possible
    # whilst not messing up any existing iterators against the QuerySet.
    if self._result_cache is None:
        if self._iter:
            self._result_cache = list(self._iter)
        else:
            self._result_cache = list(self.iterator())
    elif self._iter:
        self._result_cache.extend(self._iter)
    return len(self._result_cache)

你可以看到_result_cache有值,然后你调用recs [0],它只会使用缓存,

 if self._result_cache is not None:
         ....
     return self._result_cache[k]

源代码永远不会存在,所以当你在文档中找不到答案时,最好阅读源代码。