gpt4 book ai didi

python - Django haystack whoosh 超慢

转载 作者:太空狗 更新时间:2023-10-29 17:35:08 24 4
gpt4 key购买 nike

我有一个简单的 django-haystack 和 whoosh 引擎设置。搜索产生 19 个对象花了我 8 秒。我使用 django-debug-toolbar 来确定我有一堆重复的查询。

然后我将搜索 View 更新为预取关系,这样就不会发生重复查询:

class MySearchView(SearchView):
template_name = 'search_results.html'
form_class = SearchForm
queryset = RelatedSearchQuerySet().load_all().load_all_queryset(
models.Customer, models.Customer.objects.all().select_related('customer_number').prefetch_related(
'keywords'
)
).load_all_queryset(
models.Contact, models.Contact.objects.all().select_related('customer')
).load_all_queryset(
models.Account, models.Account.objects.all().select_related(
'customer', 'account_number', 'main_contact', 'main_contact__customer'
)
).load_all_queryset(
models.Invoice, models.Invoice.objects.all().select_related(
'customer', 'end_customer', 'customer__original', 'end_customer__original', 'quote_number', 'invoice_number'
)
).load_all_queryset(
models.File, models.File.objects.all().select_related('file_number', 'customer').prefetch_related(
'keywords'
)
).load_all_queryset(
models.Import, models.Import.objects.all().select_related('import_number', 'customer').prefetch_related(
'keywords'
)
).load_all_queryset(
models.Event, models.Event.objects.all().prefetch_related('customers', 'contracts', 'accounts', 'keywords')
)

但即便如此,搜索仍然需要 5 秒。然后我使用了 django-debug-toolbar 中的探查器,它给了我以下信息:

Django debug toolbar profiler results

据我所知,问题出在 haystack/query:779::__getitem__ 上,它被击中了两次,每次耗时 1.5 秒。我浏览了有问题的代码,但无法理解它。那么我从这里去哪里呢?

最佳答案

你在问题​​中说:

I then updated my search view to prefetch relations […]

不过,您提供的代码不使用 QuerySet.prefetch_related对于他们中的大多数人。相反,您的示例代码使用 QuerySet.select_related对他们中的大多数人来说;这不会预取对象。

每种方法的文档都非常丰富,可以帮助您确定哪种方法适合您的情况。

特别是,the QuerySet.prefetch_related documentation说:

select_related works by creating an SQL join and including the fields of the related object in the SELECT statement. For this reason, select_related gets the related objects in the same database query. However, to avoid the much larger result set that would result from joining across a ‘many’ relationship, select_related is limited to single-valued relationships - foreign key and one-to-one.

prefetch_related, on the other hand, does a separate lookup for each relationship, and does the ‘joining’ in Python. This allows it to prefetch many-to-many and many-to-one objects, which cannot be done using select_related, in addition to the foreign key and one-to-one relationships that are supported by select_related. It also supports prefetching of GenericRelation and GenericForeignKey, however, it must be restricted to a homogeneous set of results. For example, prefetching objects referenced by a GenericForeignKey is only supported if the query is restricted to one ContentType.

关于python - Django haystack whoosh 超慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32183326/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com