Faster Eloquent: Avoid Accessors with Foreach
Summary
TLDRIn this video, the presenter tackles performance issues in a Laravel project by optimizing database queries. The key issue was excessive PHP looping for user grouping instead of leveraging database capabilities. By using eager loading, modifying attribute logic, and restructuring queries, the page load time improved dramatically from 40 seconds to under a second. The presenter advises against relying on looping and attributes for calculations, recommending direct database queries and avoiding unnecessary data loading for optimal performance.
Takeaways
- 🚀 The video discusses optimizing a Laravel project with performance issues, focusing on Eloquent ORM's group by problems.
- 🔍 The initial problem was a slow page load time of 40 seconds due to inefficient data handling with 30,000 users and 10,000 relationship records.
- 🛠️ The main issue was not the large dataset but the PHP loop calling a model attribute for each user, leading to a high number of database queries.
- 📊 Using Laravel Ray to measure performance, it was found that even with 30,000 users and no relationship records, the time didn't significantly drop.
- 🔄 Eager loading relationships in the controller improved performance but still resulted in 5.8 seconds load time and high memory usage.
- 📈 Optimizing by counting related records directly in the database queries instead of using PHP loops led to a significant performance boost.
- 💡 It's advised to avoid looping through models and attributes in PHP for calculations; instead, let the database handle it.
- ❌ Relying on `withCount` in the model can lead to potential issues if the count is not always called, affecting performance and accuracy.
- 🔒 To ensure data accuracy and avoid performance pitfalls, calculations should be done in a single service or file without relying on looped attributes.
- 🏎️ The final optimization led to a query time of 0.2 seconds, demonstrating the effectiveness of letting the database handle calculations instead of PHP loops.
Q & A
What is the main issue with the Laravel project discussed in the video?
-The main issue is the poor performance due to excessive database queries when grouping users by a complex parameter that isn't a simple database column. This results in the page taking 40 seconds to load with 30,000 users and 10,000 relationship records.
What is the typical mistake people make with Eloquent in Laravel that leads to performance problems?
-People often perform grouping in PHP instead of using the database's grouping capabilities, leading to a large number of queries and slow performance.
How does the video demonstrate that loading all users and then processing them one by one is inefficient?
-The video shows that even with 30,000 users and no related records, the page still takes 34 seconds to load, indicating that the issue is not just the amount of data but the number of queries.
What tool is recommended in the video for measuring performance in Laravel?
-The video recommends using 'Spydie Laravel Ray' for performance measurement, although it also mentions 'Laravel Debug Bar' as a free alternative.
What is the impact of eager loading on the performance of the Laravel project?
-Eager loading improves performance significantly, reducing the page load time from 33 seconds to 5.8 seconds, but it still uses a considerable amount of RAM.
Why is using 'with count' in the user model not recommended in the video?
-Relying on 'with count' is not recommended because it may lead to incorrect data and unpredictable results if the attribute is called from different parts of the application where 'with count' has not been called.
What alternative approach is suggested in the video to avoid the pitfalls of 'with count'?
-The video suggests creating a separate calculator in a service or a specific file where you load only what you need without relying on 'with count' being called elsewhere in the application.
How does the video demonstrate the efficiency of separate queries for each group condition?
-By showing that using four separate queries for each group condition results in a page load time of 659 milliseconds, which is much faster than the original 30 seconds.
What is the final optimization step suggested in the video to improve performance?
-The final optimization step is to remove the loading of all users from the database if they are not needed, which reduces the page load time to 0.2 of a second.
What additional optimizations are hinted at in the video for further improving performance?
-The video suggests that introducing caching or other optimizations for calculations could further improve performance, although these are outside the scope of the video.
What article is mentioned in the video for more tips on efficient Eloquent usage?
-The video refers to a guest post written on Laravel News in 2018 for more tips on efficient Eloquent usage.
Outlines
هذا القسم متوفر فقط للمشتركين. يرجى الترقية للوصول إلى هذه الميزة.
قم بالترقية الآنMindmap
هذا القسم متوفر فقط للمشتركين. يرجى الترقية للوصول إلى هذه الميزة.
قم بالترقية الآنKeywords
هذا القسم متوفر فقط للمشتركين. يرجى الترقية للوصول إلى هذه الميزة.
قم بالترقية الآنHighlights
هذا القسم متوفر فقط للمشتركين. يرجى الترقية للوصول إلى هذه الميزة.
قم بالترقية الآنTranscripts
هذا القسم متوفر فقط للمشتركين. يرجى الترقية للوصول إلى هذه الميزة.
قم بالترقية الآنتصفح المزيد من مقاطع الفيديو ذات الصلة
MYSQL BACKEND: Tối ưu hoá phân trang từ 7s còn 1s với Table có 10.000.000 dữ liệu, SẾP tăng lương...
SQL Index |¦| Indexes in SQL |¦| Database Index
Query Monitor - 7 Important Tools for Modern WordPress Development #1
IF3140 Query Processing - Bagian 1. Pengantar
Data Loaders (the N+1 problem) - GRAPHQL API IN .NET w/ HOT CHOCOLATE #6
19# Всё о кешированиии в битриксе | Видеокурс: Создание сайта на 1С Битрикс
5.0 / 5 (0 votes)