Skip to content

Commit a712fb2

Browse files
authored
Document iterator memory usage workarounds. (#8116)
1 parent c0cfa5c commit a712fb2

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

en/appendices/5-0-migration-guide.rst

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,38 @@ ORM
227227
``$this->getSchema()`` inside the ``initialize()`` method.
228228
- ``SaveOptionsBuilder`` has been removed. Use a normal array for options instead.
229229

230+
Known Issues
231+
------------
232+
233+
**Memory Usage with extract() on Large Result Sets**
234+
235+
When using ``extract()`` on large query results, you may experience higher memory
236+
usage compared to CakePHP 4.x. The collection iterator may materialize the entire
237+
result set into memory instead of processing results lazily.
238+
239+
If you encounter memory issues when extracting values from large result sets,
240+
use one of these workarounds:
241+
242+
**Option 1: Disable hydration and iterate manually**::
243+
244+
$query = $articles->find()
245+
->select(['id'])
246+
->disableHydration();
247+
248+
foreach ($query as $row) {
249+
$id = $row['id'];
250+
// Process each value
251+
}
252+
253+
**Option 2: Build your list while iterating**::
254+
255+
$query = $articles->find()->select(['id', 'title'])->disableHydration();
256+
257+
$ids = [];
258+
foreach ($query as $row) {
259+
$ids[] = $row['id'];
260+
}
261+
230262
Routing
231263
-------
232264

en/orm/retrieving-data-and-resultsets.rst

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -981,6 +981,37 @@ The :doc:`/core-libraries/collections` chapter has more detail on what can be
981981
done with result sets using the collections features. The :ref:`format-results`
982982
section show how you can add calculated fields, or replace the result set.
983983

984+
.. warning::
985+
986+
When working with large datasets, ``extract()`` may materialize the entire
987+
result set into memory. If you encounter memory issues with large queries,
988+
consider these alternatives:
989+
990+
**Option 1: Use disableHydration() with manual extraction**::
991+
992+
$query = $articles->find()
993+
->select(['id'])
994+
->disableHydration();
995+
996+
foreach ($query as $row) {
997+
$id = $row['id'];
998+
// Process individual values
999+
}
1000+
1001+
**Option 2: Select only the fields you need**::
1002+
1003+
$query = $articles->find()
1004+
->select(['id', 'title'])
1005+
->disableHydration();
1006+
1007+
$ids = [];
1008+
foreach ($query as $row) {
1009+
$ids[] = $row['id'];
1010+
}
1011+
1012+
These approaches avoid loading unnecessary data and provide better memory
1013+
efficiency for large result sets.
1014+
9841015
Getting the First & Last Record From a ResultSet
9851016
------------------------------------------------
9861017

0 commit comments

Comments
 (0)