Skip to content

Commit 884b933

Browse files
committed
Improve docs related to memory usage and results buffering
1 parent a712fb2 commit 884b933

File tree

2 files changed

+25
-45
lines changed

2 files changed

+25
-45
lines changed

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

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -230,34 +230,25 @@ ORM
230230
Known Issues
231231
------------
232232

233-
**Memory Usage with extract() on Large Result Sets**
233+
**Memory usage with large result sets**
234234

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.
235+
Compared to CakePHP 4.x, when working with large data sets (especially when
236+
calling collection methods like ``extract()`` on the result set), you may encounter
237+
high memory usage due to the entire result set being buffered in memory.
238238

239-
If you encounter memory issues when extracting values from large result sets,
240-
use one of these workarounds:
239+
You can work around this issue by disabling results buffering for the query::
241240

242-
**Option 1: Disable hydration and iterate manually**::
241+
$results = $articles->find()
242+
->disableBufferedResults()
243+
->all();
243244

244-
$query = $articles->find()
245-
->select(['id'])
246-
->disableHydration();
245+
Depending on your use case, you may also consider using disabling hydration::
247246

248-
foreach ($query as $row) {
249-
$id = $row['id'];
250-
// Process each value
251-
}
252-
253-
**Option 2: Build your list while iterating**::
247+
$results = $articles->find()
248+
->disableHydration()
249+
->all();
254250

255-
$query = $articles->find()->select(['id', 'title'])->disableHydration();
256-
257-
$ids = [];
258-
foreach ($query as $row) {
259-
$ids[] = $row['id'];
260-
}
251+
The above will disable creation of entity objects and return rows as arrays instead.
261252

262253
Routing
263254
-------

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

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -983,34 +983,23 @@ section show how you can add calculated fields, or replace the result set.
983983

984984
.. warning::
985985

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:
986+
When working with large data sets (especially when calling collection methods
987+
like ``extract()`` on the result set), you may encounter high memory usage
988+
due to the entire result set being buffered in memory.
989989

990-
**Option 1: Use disableHydration() with manual extraction**::
990+
You can work around this issue by disabling results buffering for the query::
991991

992-
$query = $articles->find()
993-
->select(['id'])
994-
->disableHydration();
992+
$results = $articles->find()
993+
->disableBufferedResults()
994+
->all();
995995

996-
foreach ($query as $row) {
997-
$id = $row['id'];
998-
// Process individual values
999-
}
1000-
1001-
**Option 2: Select only the fields you need**::
996+
Depending on your use case, you may also consider using disabling hydration::
1002997

1003-
$query = $articles->find()
1004-
->select(['id', 'title'])
1005-
->disableHydration();
1006-
1007-
$ids = [];
1008-
foreach ($query as $row) {
1009-
$ids[] = $row['id'];
1010-
}
998+
$results = $articles->find()
999+
->disableHydration()
1000+
->all();
10111001

1012-
These approaches avoid loading unnecessary data and provide better memory
1013-
efficiency for large result sets.
1002+
The above will disable creation of entity objects and return rows as arrays instead.
10141003

10151004
Getting the First & Last Record From a ResultSet
10161005
------------------------------------------------

0 commit comments

Comments
 (0)