Mengenal Chunk dan Cursor pada Laravel – Penting untuk performance
Function chunk dan cursor ini digunakan untuk get data besar, untuk meminimalkan penggunakan memory ram
Perbandingan performance :
We have a comparison: chunk() vs cursor()
- cursor(): High Speed
- chunk(): Constant Memory Usage
10,000 records:
+-------------+-----------+------------+
| | Time(sec) | Memory(MB) |
+-------------+-----------+------------+
| get() | 0.17 | 22 |
| chunk(100) | 0.38 | 10 |
| chunk(1000) | 0.17 | 12 |
| cursor() | 0.16 | 14 |
+-------------+-----------+------------+
100,000 records:
+--------------+------------+------------+
| | Time(sec) | Memory(MB) |
+--------------+------------+------------+
| get() | 0.8 | 132 |
| chunk(100) | 19.9 | 10 |
| chunk(1000) | 2.3 | 12 |
| chunk(10000) | 1.1 | 34 |
| cursor() | 0.5 | 45 |
+--------------+------------+------------+
- TestData: users table of Laravel default migration
- Homestead 0.5.0
- PHP 7.0.12
- MySQL 5.7.16
- Laravel 5.3.22
Secara kesimpulan seperti ini :
Therefore, when processing a large amount of data, it seems better to use cursor () when speed is prioritized, and chunk () when memory usage does not exceed a certain value.
https://stackoverflow.com/questions/45464676/what-is-the-difference-between-laravel-cursor-and-laravel-chunk-method
https://qiita-com.translate.goog/ryo511/items/ebcd1c1b2ad5addc5c9d?_x_tr_sl=auto&_x_tr_tl=en&_x_tr_hl=en&_x_tr_pto=nui