0
검색 백엔드로 whoosh를 사용합니다. 난 그냥 3 검색 결과를 얻을 수django haystack slice queryset을 너무 느리게 사용하는 이유는 무엇입니까?
코드 :
#!/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
왜 나는 모든 결과가 빠른거야?
haystack에 친숙하지 않지만'timeit import default_timer as timer '를 사용 해보자. start = timer()''end = timer()''print (end-start)'그리고 결과가 아닌 print 질의'print (sqs.query)'를 입력하고 여기에 쿼리를 – madzohan
게시하고 실제로 두 쿼리를 모두 출력하면 두 번째 경우 Django가'LIMIT'과'OFFSET' 절을 추가하는 것을 볼 수 있습니다 https://docs.djangoproject.com/en/dev/topics/db/queries/# limiting-querysets, http://stackoverflow.com/questions/4481388/why-does-mysql-higher-limit-offset-slow-the-query-down – madzohan