Sunday, June 11, 2017

Re: Django + Django REST Framework SQL query response handling and serialization is very, very slow - yet not a “N+1” problem

On Sunday 11 June 2017 09:15:09 Miika Huusko wrote:

> Thanks for response!

>

> Yep, timing with "time" is not the best way to go to compare SQL query

> times. The reason I added that "time" test is that in Stack Overflow

> I was asked to confirm that data transfer is not factor here. I timed

> SQL queries with "EXPLAIN ANALYZE" and "\timing" first. That don't

> take data transfer into account, so, "time" as quick test that data

> transfer is not a problem.

>

> About timing: as an example I can reduce the problem to serializing

> only Items and related Photos. It results only two queries. For

> example for a dataset of about 40k items results:

>

> django.db.backends: (1.252) SELECT "todo_item"."version", *... all

> item properties ...* FROM "todo_item" WHERE ("todo_item"."project_id"

> = '...' AND "todo_item"."deleted_at" IS NULL);

>

> django.db.backends: (0.883) SELECT "photos_photo"."version", *... all

> item properties ...* FROM "photos_photo" WHERE

> "photos_photo"."note_id" IN (349527, 349528, 349529, *... and rest of

> the 40k IDs ...* );

 

Why did you opt to use prefetch_related and not select_related?

 

--

Melvyn Sopacua

No comments:

Post a Comment