S
S
sportik1742021-02-21 19:02:20
Django
sportik174, 2021-02-21 19:02:20

Why is a query being slow in Django?

There is a request like this:

pcollections = PCollection.objects.filter(
    product__category__title__in = filter_items_active['categories_get'] ,
    product__color__title__in = filter_items_active['colors_get'] ,
    product__surface__title__in = filter_items_active['surfaces_get'] ,
    product__width_height__title__in = filter_items_active['width_heights_get'] ,
    product__price_type__title__in = filter_items_active['pricetypes_get'] ,
    fabrika__title__in = filter_items_active['fabriks_get'] ,
    product__appointment__title__in = filter_items_active['appointments_get'] ,
    product__premises__title__in = filter_items_active['premises_get'] ,
    fabrika__country__title__in = filter_items_active['countrys_get'] ,
    product__picture__title__in = filter_items_active['pictures_get'] ,
    product__form__title__in = filter_items_active['forms_get'] ,
    product__rectificate__in = filter_items_active['rectificate_get'] ,
    product__stock__in = filter_items_active['stock_get']
  ).distinct()


Runs very slowly. Can you tell me how to optimize it?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry, 2021-02-22
@sportik174

sportik174 Well, here it's worth starting with the fact that you are searching for a string, I guess that there are no indexes on the title.
In short, you shouldn't do this.

product__category__title__in = filter_items_active['categories_get']

and you should do it like this
product__category_id__in = filter_items_active['categories_get']

where in categories_get you pass not strings, but category ids, and so on with all other parameters

D
Dr. Bacon, 2021-02-21
@bacon

Start by analyzing the query execution plan in SQL
Shl, although there is complete trash here, you need to filter not by title, by id of the corresponding tables. For rectificate and stock you could do this.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question