I’ve been fighting memory limits more often than I thought I would have to. Giving me the thought that perhaps I’m building my query wrong. But there are a few helpful helpers that can keep the memory low. Two of them being chunk() and cursor().
This speed test is brought to you by @ryo511. His explantation is neat and easy to digest.
TL; DR
- cursor (): High Speed
- chunk (): Constant Memory Usage
10,000 records
Time (sec) | Memory (MB) | |
---|---|---|
get () | 0.17 | twenty two |
chunk (100) | 0.38 | Ten |
chunk (1000) | 0.17 | 12 |
cursor () | 0.16 | 14 |
100,000 records
Time (sec) | Memory (MB) | |
---|---|---|
get () | 0.8 | 132 |
chunk (100) | 19.9 | Ten |
chunk (1000) | 2.3 | 12 |
chunk (10000) | 1.1 | 34 |
cursor () | 0.5 | 45 |
What is Chunk and Cursor?
For a quick overview of chunk() and cursor() @JeanMarcos has a simplified answer.
Chunk: It will “paginate” your query, this way you use less memory.
- Uses less memory
- It takes longer
public function getData() {
Contact::chunk(1000, function ($contacts) {
foreach ($contacts as $contact) {
//rest of your code...
}
});
}
Cursor: You will use PHP Generators to search your query items one by one.
- It takes less time
- Uses more memory
public function getData() {
foreach (Contact::cursor() as $contact) {
//rest of your code...
}
}