为什么使用django haystack slice queryset太慢了?

时间:2014-11-24 10:00:20

标签: python django django-haystack whoosh

我使用whoosh作为搜索后端。

当我得到3个搜索结果时,代码:

#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
sys.path.append('/home/guomeng/projects/tapplex_ringtones')
import os
os.environ['DJANGO_SETTINGS_MODULE'] = "tapplex_ringtones.settings"

from haystack.query import SearchQuerySet
from ringtones.models import Ringtone
import time

query_word = u'sky'
t0 = time.time()
sqs = SearchQuerySet().models(Ringtone).filter(content=query_word)[:3]
t1 = time.time()
print sqs
print t1 - t0

结果是:

[<SearchResult: ringtones.ringtone (pk=u'1730')>, <SearchResult: ringtones.ringtone (pk=u'28959')>, <SearchResult: ringtones.ringtone (pk=u'25889')>]
0.422543048859> 0.422543048859

当我得到所有搜索结果时,代码:

#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
sys.path.append('/home/guomeng/projects/tapplex_ringtones')
import os
os.environ['DJANGO_SETTINGS_MODULE'] = "tapplex_ringtones.settings"

from haystack.query import SearchQuerySet
from ringtones.models import Ringtone
import time

query_word = u'sky'
t0 = time.time()
sqs = SearchQuerySet().models(Ringtone).filter(content=query_word)
t1 = time.time()
print sqs
print t1 - t0

结果是:

[<SearchResult: ringtones.ringtone (pk=u'1730')>, <SearchResult: ringtones.ringtone (pk=u'28959')>, <SearchResult: ringtones.ringtone (pk=u'25889')>, <SearchResult: ringtones.ringtone (pk=u'5303')>, <SearchResult: ringtones.ringtone (pk=u'5335')>, <SearchResult: ringtones.ringtone (pk=u'5411')>, <SearchResult: ringtones.ringtone (pk=u'1212')>, <SearchResult: ringtones.ringtone (pk=u'28473')>, <SearchResult: ringtones.ringtone (pk=u'23867')>, <SearchResult: ringtones.ringtone (pk=u'27087')>, <SearchResult: ringtones.ringtone (pk=u'26849')>, <SearchResult: ringtones.ringtone (pk=u'2973')>, <SearchResult: ringtones.ringtone (pk=u'2645')>, <SearchResult: ringtones.ringtone (pk=u'31007')>, <SearchResult: ringtones.ringtone (pk=u'11637')>, <SearchResult: ringtones.ringtone (pk=u'16957')>, <SearchResult: ringtones.ringtone (pk=u'106')>, <SearchResult: ringtones.ringtone (pk=u'2481')>, <SearchResult: ringtones.ringtone (pk=u'15697')>]
0.19460105896

为什么我得到的所有结果都很快?

1 个答案:

答案 0 :(得分:0)

可能是因为:

SearchQuerySet().models(Ringtone).filter(content=query_word)[:3]

做一切

SearchQuerySet().models(Ringtone).filter(content=query_word)

但是,还使用limit子句更新查询集并返回新的查询集。这可以解释全部或部分时差。要获得有意义的结果,您需要平均多次调用所花费的时间,并分析一些内部代码 - 即数据库处理查询需要多长时间,以及查询实际上是什么等。