Friday, December 25, 2015

admin search_fields performance

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?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/24147884-9d9b-40b6-bc17-b36fcaeae23c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment