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: https://ko-fi.com/bugbytes โถ๏ธ Full Playlist: https://www.youtube.com/playlist?list=PL-2EBeDYMIbQXKsyNweppuFptuogJe2L- ๐ฆ๐ผ๐ฐ๐ถ๐ฎ๐น ๐ ๐ฒ๐ฑ๐ถ๐ฎ: ๐ Blog: https://bugbytes.io/posts/ ๐พ Github: https://github.com/bugbytes-io/django-orm-series ๐ฆ Twitter: https://twitter.com/bugbytesio ๐ ๐๐๐ฟ๐๐ต๐ฒ๐ฟ ๐ฟ๐ฒ๐ฎ๐ฑ๐ถ๐ป๐ด ๐ฎ๐ป๐ฑ ๐ถ๐ป๐ณ๐ผ๐ฟ๐บ๐ฎ๐๐ถ๐ผ๐ป: Github: https://github.com/bugbytes-io/django-orm-series django-debug-toolbar: https://django-debug-toolbar.readthedocs.io/en/latest/ prefetch_related: https://docs.djangoproject.com/en/4.2/ref/models/querysets/#prefetch-related select_related: https://docs.djangoproject.com/en/4.2/ref/models/querysets/#select-related QuerySet only() function: https://docs.djangoproject.com/en/4.2/ref/models/querysets/#only Serving Static Files During Development: https://docs.djangoproject.com/en/4.2/howto/static-files/#serving-static-files-during-development #python #django #webdevelopment #database #sql