Django Query Optimization / select_related & prefetch_related / django-debug-toolbar / N+1 Problem

preview_player
Показать описание
In this video, we learn about query optimization in Django, and solving the N+1 problem using the select_related() and prefetch_related() functions.

We will look at how to use prefetch_related() to fetch all related objects in subsequent queries, as well as how to use select_related() to create a JOIN statement in SQL.

Both methods can greatly cut down on the number of queries issued to the database, and therefore increase performance.

We'll also see how to install and use django-debug-toolbar, and inspect and optimize the SQL queries via its feedback.

Finally, we'll also look at Prefetch objects in Django, and how they can be used to customize the behaviour of the prefetch_related() function, as well as a quick look at the .only() function and .annotate() functions.

📌 𝗖𝗵𝗮𝗽𝘁𝗲𝗿𝘀:
00:00 Intro
00:32 Using django-debug-toolbar for SQL analysis
08:47 Using prefetch_related to optimise querying and avoid N+1 problem
14:04 Using select_related with ForeignKey objects to avoid N+1 problem
17:23 Using only() function to fetch specific fields
19:22 Prefetch objects in Django

☕️ 𝗕𝘂𝘆 𝗺𝗲 𝗮 𝗰𝗼𝗳𝗳𝗲𝗲:
To support the channel and encourage new videos, please consider buying me a coffee here:

▶️ Full Playlist:

𝗦𝗼𝗰𝗶𝗮𝗹 𝗠𝗲𝗱𝗶𝗮:

📚 𝗙𝘂𝗿𝘁𝗵𝗲𝗿 𝗿𝗲𝗮𝗱𝗶𝗻𝗴 𝗮𝗻𝗱 𝗶𝗻𝗳𝗼𝗿𝗺𝗮𝘁𝗶𝗼𝗻:

#python #django #webdevelopment #database #sql
Рекомендации по теме
Комментарии
Автор

You are the best thing that has ever happened to Django. Thank you. The best thing we can do, is buy you coffee. You deserve it.

anthonymwangi
Автор

I've watched so many Django videos and you're the first person I've come across that talks about this. Great work once again!

Peterstavrou
Автор

Thanks, You have taken Django to another level❤

alexdin
Автор

love your cool advanced content... you are my django master. thank you for sharing ❤

streamocu
Автор

Great content, easy concise and well paced. You are a real masterclass educator.

guledomer
Автор

Thanks, You have taken me to another level in django

ali-cune
Автор

Your channel is peace of gold on youtube. Exact hit that I'd been searching for

adhd_arti
Автор

I have used prefetch and select related blindly in real time project (Not knowing in-depth) and query time was reduced. But this explanation is too good and I got to know the real use. Thank you so much man.

lfctimes
Автор

Thanks alot, was wondering why my application was so slow and this completely fixed it. Love this channel!

georgewestbrook
Автор

Dude i love ur stuff. For me the special highlight tho is every time you say boolean I hear boullion

wanderingtravellerAB
Автор

Great tutorial! but can you let me know,
why we are configuring django to server static files explicitly,
when its already being done by runserver implicitly?

seeker
Автор

Thank you so much for this great channel 🥰🥰

open-source
Автор

awesome video❤... could you also post a video on caching in django

shivar
Автор

Instead of using the 'ratings' related_name and then filtering the queryset, could you have created another Prefetch object for that like you did with monthly sales and call it five_star_ratings or something?

IMTyranus
Автор

nice video ! Do you have any suggestions for debugging the queries with django rest framework ?

mohamedazizchibani
Автор

Is it mandatory for me to work on templates for each endpoints if i wish to use the django debug toolbar?

ethvoch
Автор

I have updated date of sale of a restaurant to two months before. But still I'm getting the sale object

sibik
Автор

Is the management/command pretty much the same as the scripts in the extensions just best practice to put code to import data as a command rather than a script?

Also, I think there may be an error with the Prefetch() code because if I change "month_ago" to "two_days_ago":
two_days_ago = timezone.now() - timezone.timedelta(days=2)

and print:


I get a queryset with 2 objects but when I print the below I get a queryset of 6
Restaurant.objects.prefetch_related('ratings',

and when I print the totals it's exactly the same as when we get all of the Sales rather than the Sales for a specific date.
It's a list with 4 results.

Hope this makes sense and not an issue of how I coded along.

Peterstavrou