Eloquent Performance Challenge: My Solution Version

แชร์
ฝัง
  • เผยแพร่เมื่อ 20 ก.ย. 2024

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

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

    Really nice challenge, a small tip, when eager loading you can also specify the columns you want from the relationship like this ->with('category:id,name'), it doesn't really matter here since in this case this is a very minor performance boost, but relationship could have like 20 columns, and this way you are loading only those you need for the query :)

    • @LaravelLover
      @LaravelLover 3 ปีที่แล้ว

      Good tip bro

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

      Nice tip.
      This also reduces the data that is to be returned by DB, so less cpu intensive and also data transfer size is significantly low when transferring through Ajax.

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

    I am not a PHP guy, I even don't like PHP. But I like to watch your videos, how you approach to the solutions, optimize things step by step.
    Thanks for your videos. Keep making ❤️

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

    Thank you so much for this video, actually this was my first time using debugbar, and just following your tips I managed to reduce the queries in one of the pages from 800 queries to just 75 , and from 1400 models to 40 , also reduced memory from 26 mb to 15 mb.
    You are awesome. Thank you. Keep it up.

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

    6:47 wow! that was soo cool and satisfying

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

    this video is gold, thanks !

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

    Excellent video!! Thanks for this. Your explanation is perfect and solve a lot of questions for me. Thanks!

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

    $object->relation = returns results for the relationship as result of sql query
    $object->relation() = returns query builder / eloquent instance on which one can perform other stuff.
    Respect from Pakistan

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

    Amazing !!!

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

    This is best steps and very clear to refactoring query.
    Waw! Need more cases

  • @aldwynconjurer9449
    @aldwynconjurer9449 2 ปีที่แล้ว

    It's a wonderful sample that show what we do and what wee must do. Thanks

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

    Thank you, wonderful explanation, you save my time once I googled how to minimize the number of query in model, but I didn't success, now I got idea how to optimize my queries.

  • @Matoxina
    @Matoxina 3 ปีที่แล้ว

    Clean solution, good job

  • @2am_yt
    @2am_yt 3 ปีที่แล้ว

    I love these challenges. Very satisfiying.

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

    Good solution, we need more challenges.

  • @FririkurEllefsen
    @FririkurEllefsen 3 ปีที่แล้ว

    This is gold

  • @raj-kal
    @raj-kal 3 ปีที่แล้ว +1

    Thanks for the video. I have learnt a lot from your videos

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

    Lol love the fact that it says 1.42 sec and the vid takes 4 seconds to open the page hahahaha but thank this helped me a lot

  • @bugszone6098
    @bugszone6098 3 ปีที่แล้ว

    Best Video Ever

  • @belce1982
    @belce1982 3 ปีที่แล้ว

    Thanks as always. Mine is PR 48. And now I'm here to learn how to really improve it :)

  • @jimishukurow2286
    @jimishukurow2286 3 ปีที่แล้ว

    Perfect example, like frrom me, respect man!

  • @freeeasyprogramming5151
    @freeeasyprogramming5151 3 ปีที่แล้ว

    Thank you so much really you are the best

  • @dennisjmaina
    @dennisjmaina 2 ปีที่แล้ว

    First of all, allow me give my loud of applause for your great content on laravel and programming generally. Your videos have really shaped my laravel skills 👏. I do have a problem when it comes to memory(debugbar measurement) which even in a small page with just 3 queries (perfectly optimized) doesn't still go below 23mb and page load speed at/around 200ms. Thank you

    • @LaravelDaily
      @LaravelDaily  2 ปีที่แล้ว

      That can be because of your webserver

    • @dennisjmaina
      @dennisjmaina 2 ปีที่แล้ว

      @@LaravelDaily might be, because even the login page with no queries and just 2 views, still goes at 23mb. Thank you for the response. Is there a fix to this though?

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

      Use a different web-server?

    • @dennisjmaina
      @dennisjmaina 2 ปีที่แล้ว

      This sure did work. On my local machine I'm running xampp server which seems to be the one with this problem. When i tested on a staging server running Lightspeed web server, everything seems to be stable. Thanks alot 😊

  • @santiagos4290
    @santiagos4290 3 ปีที่แล้ว

    very helpfull , thanks

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

    Hi sir, learn more alot from your videos. u use withCount and withAvg to a single relationship. Can you please make a video on aggregate relation methods like these and withSum for relationship's to relations like withSum('movies.category', 'column_name'). Thanks

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

      Hi, I don't really see the goal of doing this, you just use the same functions and pass different relationships to them, and experiment. Not sure if my new video on a different scenario would add some significant content that is unclear.

  • @EduardR10
    @EduardR10 2 ปีที่แล้ว

    Espectacular. Muchas gracias :)

  • @ward7576
    @ward7576 3 ปีที่แล้ว

    That's a satisfying refactor.

  • @hiajayy
    @hiajayy 3 ปีที่แล้ว

    Very informative.❤

  • @LaravelLover
    @LaravelLover 3 ปีที่แล้ว

    Very important video thank you

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

    Thank you for your amazing video

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

    Thanks. Very interestin. I also forgot to check indexes.... :(

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

    That's some eloquent queries 😉

  • @taslimsuman
    @taslimsuman 3 ปีที่แล้ว

    Great

  • @karimqasem7523
    @karimqasem7523 3 ปีที่แล้ว

    Thank u nice challenge ❤️❤️

  • @ritomukherjee8389
    @ritomukherjee8389 3 ปีที่แล้ว

    Greatness 🙏🙏

  • @josiasvelazquez1095
    @josiasvelazquez1095 3 ปีที่แล้ว

    The best!

  • @_passby5399
    @_passby5399 3 ปีที่แล้ว

    Awesome!

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

    I didn't know there weren't foreign keys yet. I just assumed there would be. It was a mistake to do so hahaha

  • @satashanofficial
    @satashanofficial 3 ปีที่แล้ว

    Hi Povilas, I learned a lot from you, please confirm one thing, while adding a foreign key you did not drop the column, it isn't necessary to drop a foreign key column?

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

      There are two different things: column and foreign key.
      So column existed but foreign key didn't.
      So there was nothing to delete, I just added foreign key to existing column.

  • @abhinavsingh7859
    @abhinavsingh7859 3 ปีที่แล้ว

    Hello i am using laravel excel package and i have a little doubt, the excel file consist of students fee data, where they pay specific fee data. So suppose a student pay for tution fee, and hostel fee so there are two tables for that, one is parent table and another is child table. So suppose tution fee is 1000 and hostel is 500, the sum 1500 will be stored in parent table whose primary key will be stored in child table where fee details will be stored different rows, but it will be single row for parent table no matter how many payment student make it will only show the sum of amount

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

    Joss

  • @Thead78
    @Thead78 3 ปีที่แล้ว

    Parfait, merci :)

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

    sir what about do not use refrence id but just do this $table->bigInterger('category_id)->unsigned()->index(); and in model define relation ??? does it slow ????

    • @LaravelDaily
      @LaravelDaily  3 ปีที่แล้ว

      The index() itself will help you to get faster query, yes. But you do need a foreign key to avoid incorrect data, watch my video about it: th-cam.com/video/-77LnBSL2Zk/w-d-xo.html

  • @Edvard-Aliev
    @Edvard-Aliev 3 ปีที่แล้ว +2

    Первый! Спасибо!

    • @yusuperproactive9568
      @yusuperproactive9568 3 ปีที่แล้ว

      Круто, что тут русскоговорящие есть

  • @ujustwatch
    @ujustwatch 3 ปีที่แล้ว

    Any video is there on parsing huge xml data in Laravel

  • @franklintorres3717
    @franklintorres3717 3 ปีที่แล้ว

    Hi sir. Can I add a foreign key to the pivot table? if yes, how to do it?
    Thanks for advance

  • @user-rm4zo1mg5o
    @user-rm4zo1mg5o 3 ปีที่แล้ว

    In my case same exactly same code requires 19MB of RAM always. Maybe it depends of the system.

    • @LaravelDaily
      @LaravelDaily  3 ปีที่แล้ว

      Yes, I will talk about it in today's video.

  • @gssj-o8p
    @gssj-o8p ปีที่แล้ว

    Could you make it 1 query with a join on category? Is that good practise?

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

      Yes also ok, but then you lose Eloquent magic like accessors, as it becomes a Query Builder.

    • @gssj-o8p
      @gssj-o8p ปีที่แล้ว +1

      @@LaravelDaily yeah I just watched a video where you explained this, about the differences of using query(). I'm watching all your videos, I just did a test and got the answer that I passed after being lay off my last job, I can safely say that, if everything go as planned as it seems like, you are one the biggest helpers here. Thank you so much for sharing your massive knowledge with the world!

  • @saktipujoedi8898
    @saktipujoedi8898 3 ปีที่แล้ว

    Nice

  • @delaneywright4751
    @delaneywright4751 3 ปีที่แล้ว

    I prefer to write my own SQL queries as opposed to eloquent I feel like when working with complex systems having allot of relationships defined in the model gets very complicated as opposed to just SQL. What are your thought's on this is not using eloquent in Laravel a bad thing as i almost never use it if I can help it?

    • @youknowme9732
      @youknowme9732 3 ปีที่แล้ว

      "Eloquent models dispatch several events, allowing you to hook into the following moments in a model's lifecycle: retrieved, creating, created, updating, updated, saving, saved, deleting, deleted, restoring, restored, and replicating." (c) laravel.com

    • @youknowme9732
      @youknowme9732 3 ปีที่แล้ว

      If our app have many visitors, we can optimizate this code with events hooks (on created new rating we can calculate Avg rating and save to Movie model, or create model like MovieStats - and run this code on diffrent threads), or move complex query to another server.

  • @donnaarisson7926
    @donnaarisson7926 3 ปีที่แล้ว

    doesn’t ratings->count() automatically resolves to rating_count if the relation is eagerly loaded?

    •  3 ปีที่แล้ว

      but then you eager load all the ratings, and you don't want to load them here, you specifically only want the count

  • @UnknownUser-ud1es
    @UnknownUser-ud1es 3 ปีที่แล้ว +1

    What is the query and model monitor you are using in the video?

  • @abdulbasitsalah2918
    @abdulbasitsalah2918 3 ปีที่แล้ว

    thanks povilas for this great tips
    1-is there different between foreign and foreignId? because I always use foreignId I wonder which one is better to stick with it
    2- take() and paginate() are some or different? because they both return specific amount of data in my challenge I used pagination

    • @JuanMunoz-th3vn
      @JuanMunoz-th3vn 3 ปีที่แล้ว +1

      dude, with foreign you have to write unsignedbiginteger and foreign statement, but with foreignId, contains both lines ,
      take seems like a LIMIT , paginate, is like a function that returns how many rows are, what page you are, etc
      sorry my english

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

      1. Foreign() just created the foreign key, so you have to separately create the column itself. ForeignId() creates both column and foreign key.
      2. Well, they serve different purpose, take() adds LIMIT 100 to the SQL query, and paginate() takes the parameters from GET request, then adds similar LIMIT to SQL and then provides Laravel with features like ->links() to show in the Blade.

    • @abdulbasitsalah2918
      @abdulbasitsalah2918 3 ปีที่แล้ว

      @@LaravelDaily thank u
      and one little note, after i apply your solution, i notice that you did not add foreign key for category in movie table

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

      @@JuanMunoz-th3vn thank you, no your English is good

    • @LaravelDaily
      @LaravelDaily  3 ปีที่แล้ว

      Yes good catch, it wouldn't speed up much in this case, but also a good practice.

  • @firdavs.ibodullayev
    @firdavs.ibodullayev 3 ปีที่แล้ว

    What is the name of this Laravel debugger?

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

      composer require barryvdh/laravel-debugbar

    • @firdavs.ibodullayev
      @firdavs.ibodullayev 3 ปีที่แล้ว

      @@LaravelDaily thanks very much

  • @mushnikov35
    @mushnikov35 3 ปีที่แล้ว

    Where can I get such a debugger?

    • @LaravelDaily
      @LaravelDaily  3 ปีที่แล้ว

      composer require barryvdh/laravel-debugbar

    • @mushnikov35
      @mushnikov35 3 ปีที่แล้ว

      @@LaravelDaily Thanks

  • @Hello-mu1et
    @Hello-mu1et 3 ปีที่แล้ว

    What ide are you using?

    • @LaravelDaily
      @LaravelDaily  3 ปีที่แล้ว

      Phpstorm

    • @Hello-mu1et
      @Hello-mu1et 3 ปีที่แล้ว

      @@LaravelDaily and what theme pls?

    • @LaravelDaily
      @LaravelDaily  3 ปีที่แล้ว

      Material Darker

    • @Hello-mu1et
      @Hello-mu1et 3 ปีที่แล้ว

      @@LaravelDaily Thank you