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

แชร์
ฝัง
  • เผยแพร่เมื่อ 25 ส.ค. 2024

ความคิดเห็น • 80

  • @bugbytes3923
    @bugbytes3923  4 หลายเดือนก่อน

    Playlist link: th-cam.com/video/EsBqIZmR2Uc/w-d-xo.html

  • @anthonymwangi6889
    @anthonymwangi6889 7 หลายเดือนก่อน +10

    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.

    • @bugbytes3923
      @bugbytes3923  7 หลายเดือนก่อน

      Haha bold claim 😃 But thank you, that's amazing to hear!

  • @Peterstavrou
    @Peterstavrou ปีที่แล้ว +3

    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!

  • @alexdin1565
    @alexdin1565 ปีที่แล้ว +4

    Thanks, You have taken Django to another level❤

  • @streamocu2929
    @streamocu2929 ปีที่แล้ว +5

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

    • @bugbytes3923
      @bugbytes3923  ปีที่แล้ว

      Thanks a lot, really appreciate that!

  • @devlin1234
    @devlin1234 หลายเดือนก่อน

    excellent, thanks again! been watching your videos for a while now.. very well explained!

    • @bugbytes3923
      @bugbytes3923  หลายเดือนก่อน +1

      Thanks a lot - great to hear that! Thank you for watching

  • @guledomer
    @guledomer ปีที่แล้ว +2

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

    • @bugbytes3923
      @bugbytes3923  ปีที่แล้ว

      Thanks a lot, really appreciate that comment ! Glad you liked the video.

  • @ali-cu1ne
    @ali-cu1ne 5 หลายเดือนก่อน +2

    Thanks, You have taken me to another level in django

    • @bugbytes3923
      @bugbytes3923  5 หลายเดือนก่อน

      Thanks a lot, glad to hear that!

  • @adhd_arti
    @adhd_arti ปีที่แล้ว +1

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

    • @bugbytes3923
      @bugbytes3923  ปีที่แล้ว

      Thanks so much for that comment, really appreciate it!

  • @satishshah3861
    @satishshah3861 5 หลายเดือนก่อน

    You are true genius❤

    • @bugbytes3923
      @bugbytes3923  5 หลายเดือนก่อน

      Thanks a lot!

  • @georgewestbrook4512
    @georgewestbrook4512 4 หลายเดือนก่อน

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

    • @bugbytes3923
      @bugbytes3923  4 หลายเดือนก่อน

      Glad to hear it! Thanks a lot!

  • @lfc5times130
    @lfc5times130 9 หลายเดือนก่อน

    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.

    • @bugbytes3923
      @bugbytes3923  9 หลายเดือนก่อน

      Thanks a lot! Really glad to hear it was helpful!

  • @saulo2264
    @saulo2264 หลายเดือนก่อน

    Nice video! Very helpful.

  • @wanderingtravellerAB99
    @wanderingtravellerAB99 9 หลายเดือนก่อน +1

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

    • @bugbytes3923
      @bugbytes3923  9 หลายเดือนก่อน

      Haha thanks, some words do come across strangely in a Scottish accent! Thanks for watching!

  • @rangabharath4253
    @rangabharath4253 ปีที่แล้ว +1

    Awesome as always 🥳🎉

  • @open-source243
    @open-source243 3 หลายเดือนก่อน

    Thank you so much for this great channel 🥰🥰

  • @ewartmouton
    @ewartmouton 2 หลายเดือนก่อน +1

    Thanks

    • @bugbytes3923
      @bugbytes3923  2 หลายเดือนก่อน

      Thanks so much for this, much appreciated!

  • @codethinkersacademy2794
    @codethinkersacademy2794 29 วันที่ผ่านมา

    awesome

  • @rprana12777
    @rprana12777 10 หลายเดือนก่อน +1

    Love the content

    • @bugbytes3923
      @bugbytes3923  10 หลายเดือนก่อน

      Thanks a lot!

  • @repotranstech9614
    @repotranstech9614 ปีที่แล้ว

    Very important video.

  • @Dorso2009
    @Dorso2009 11 หลายเดือนก่อน

    Excellent video!! Thank you!

    • @bugbytes3923
      @bugbytes3923  11 หลายเดือนก่อน

      Thanks a lot!

  • @TomislavMiletic
    @TomislavMiletic ปีที่แล้ว +1

    Thank you!

    • @bugbytes3923
      @bugbytes3923  ปีที่แล้ว

      You're welcome, thanks for watching Tomislav!

  • @Coden69
    @Coden69 ปีที่แล้ว +1

    Amazing

  • @onnot701
    @onnot701 ปีที่แล้ว +1

    thanks

  • @the-antroy
    @the-antroy ปีที่แล้ว +1

    Nice, Thanks!

    • @bugbytes3923
      @bugbytes3923  ปีที่แล้ว

      You’re welcome and thanks for watching!

  •  ปีที่แล้ว +1

    Great!

  • @aashayamballi
    @aashayamballi ปีที่แล้ว

    Thank you

    • @bugbytes3923
      @bugbytes3923  ปีที่แล้ว

      You're welcome, thanks for watching!

  • @buxx5334
    @buxx5334 ปีที่แล้ว +1

    I hope your channel be 1m subs ❤

  • @wiki-infodevelopment3369
    @wiki-infodevelopment3369 ปีที่แล้ว +1

    Sure it is very good

  • @HalloWorld123
    @HalloWorld123 7 หลายเดือนก่อน

    Thank you, sir

  • @dodokwak
    @dodokwak ปีที่แล้ว

    Awesome!

  • @seeker511
    @seeker511 7 หลายเดือนก่อน

    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?

  • @frameff9073
    @frameff9073 ปีที่แล้ว

    thank

  • @shivar8023
    @shivar8023 9 หลายเดือนก่อน

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

    • @bugbytes3923
      @bugbytes3923  9 หลายเดือนก่อน +1

      Thanks - yes I’ll be doing some Caching + Redis stuff soon!

  • @IMTyranus
    @IMTyranus 6 หลายเดือนก่อน

    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?

  • @user-et7hv3oc8h
    @user-et7hv3oc8h 3 หลายเดือนก่อน

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

  • @sibik5695
    @sibik5695 11 หลายเดือนก่อน

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

  • @joshajiniran
    @joshajiniran ปีที่แล้ว +1

    Is debugtoolbar still usable for rest APIs?

  • @mohamedazizchibani6004
    @mohamedazizchibani6004 9 หลายเดือนก่อน

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

  • @TheApgreyd
    @TheApgreyd ปีที่แล้ว

    What about ur Django-leaflet series?

    • @bugbytes3923
      @bugbytes3923  ปีที่แล้ว

      I’ll hopefully create some more soon!

  • @Peterstavrou
    @Peterstavrou ปีที่แล้ว

    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:
    Sale.objects.filter(datetime__gte=two_days_ago)
    I get a queryset with 2 objects but when I print the below I get a queryset of 6
    Restaurant.objects.prefetch_related('ratings', two_days_ago).filter(ratings__rating=5)
    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.

    • @bugbytes3923
      @bugbytes3923  ปีที่แล้ว

      Hey Peter - are you building a Prefetch() object in this query? It looks like you're passing in a datetime object - "two_days_ago" - to the "prefetch_related" query above?

    • @user-zv7gf9zv2j
      @user-zv7gf9zv2j หลายเดือนก่อน

      same problem get , whenever i put , filter(ratings__rating=5, sales__date_time__range=[month_ago_30, timezone.now()- timezone.timedelta(days=3)]) then its work

  • @bengabp
    @bengabp 23 วันที่ผ่านมา

    what if im using django rest framework and im working on api ?

    • @bugbytes3923
      @bugbytes3923  23 วันที่ผ่านมา

      @@bengabp on your api you’ll usually be fetching data from the database, so all the same concepts will apply

    • @bengabp
      @bengabp 23 วันที่ผ่านมา

      @@bugbytes3923 yeah but where does the sidebar show ? my api doesnt have html page, or do i have to add atleast a form or html page ?, i use postman to test my api

  • @Septumsempra8818
    @Septumsempra8818 ปีที่แล้ว

    N+1 has been kicking my @ss

    • @bugbytes3923
      @bugbytes3923  ปีที่แล้ว

      Hope this can help 😁

    • @Septumsempra8818
      @Septumsempra8818 ปีที่แล้ว

      ​@bugbytes3923 honestly it's made things kinda worse. It's sped up my landing page etc, but it's forcing me to redesign my models and come to terms with how little I understand databases.

  • @Ahmed-gz7zq
    @Ahmed-gz7zq 9 หลายเดือนก่อน

    hello sir, thanks for the explanation, i just wanted to know when will the select_related not working,
    I'm using a different ways to implement it here:
    ` loans = self.lending_loans.select_related('lender').filter(lender_loan_status__in=[Loan.STATUS.funded, Loan.STATUS.completed])
    `
    Where
    lending_loans is the related name of the the FK inside the loan model.

  • @jonathanbriones3235
    @jonathanbriones3235 2 หลายเดือนก่อน

    Great video!
    restaurants = Restaurant.objects.prefetch_related('rating_set', monthly_sales).filter(rating__rating=5)
    restaurants.annotate(total=Sum('sale__income'))
    I'm cross referencing the values printed out on the terminal vs the dB Browser table. The Sum value doubles up for Restaurant rows when the query set returns the same restaurant name twice (i.e. Bombay Bustle has 2-off 5-star entries, and it's sales are summed x2 - instead of the x1 which is expected).
    I tried troubleshooting with ChatGPT using the distinct() function on the filtered query set. It worked on the query set, but did not translate with the annotate function.
    Just wondering if anyone else encountered the same anomaly?