Performance Best Practices - Iterate only once

Performance Best Practices - Iterate only once

On the Performance Best Practices document it says:

"Iterating more than once over a query result is not a good practice. By doing so, the query results will be copied into the memory. The same applies when using direct indexers (like query[2].value expressions)"

Does this means that all collumns will be copied into memory? Or just that all rows will be copied into memory? By other words, will the code optimizer not do its magic if a record list is iterated more than 1 time?

nice catch!

could some explanation.

it's not uncommon to iterate twice on lists....

In any case, the columns are still optimized.

However, if you iterate a query only once, the platform is smart enough to feed your iterations with an opened ResultSet, so in fact you are reading the results from the database as they arrive. This has the added benefit that you get to store just a single row in memory, throughout your iterations.

When you iterate the same record list more than once (or access it in random order with the indexer), the platform must copy all rows (with just the optimized columns) and store it in a buffer. That's why it has an increased memory overhead.

In practice, it's an optimization that only makes sense if you are dealing with a record list with thousands of records. Of course, this will never happen if you design pagination properly! So J, multiple iterations might be a common thing, but you should not lose your sleep over it.