admin search_fields performance

31 views
Skip to first unread message

Maria Iakymova

unread,
Dec 25, 2015, 8:57:12 AM12/25/15
to Django users
Hello!
I faced with the performance issue while searching in admin by long string.
I have CompanyAdmin.search_fields = ['business_name', 'contact_persons__name',]
and tried to find company by name like ''COMPANY - REAL ESTATE COMPANY NAME" and query handling took about 7 minutes. (There are about 3000 company objects)

While investigating I found that there a lot of outer joins of contact_persons table (for each splitted word from search string).

in BaseModelAdmin.get_search_results

for bit in search_term.split():
    or_queries
= [models.Q(**{orm_lookup: bit}) for orm_lookup in orm_lookups]
    queryset
= queryset.filter(reduce(operator.or_, or_queries))


as I understand, joins that I wrote above are the result of queryset.filter

After I change it for:

Q_obj = Q()
...

for bit in search_term.split():
    or_queries
= [models.Q(**{orm_lookup: bit}) for orm_lookup in orm_lookups]
    Q_obj
&= reduce(operator.or_, or_queries)
queryset
= queryset.filter(Q_obj)

...


my query was handled immediately. So the question is, if it is right solution and can it cause any other issues?
Reply all
Reply to author
Forward
0 new messages