Use the PRESELECT keyword to get data in advance
- Last Updated: January 16, 2024
- 2 minute read
- OpenEdge
- Version 12.8
- Documentation
One typical use of the REPEAT block that is
still valuable is when you use it with a construct called a PRESELECT. To understand this usage, you need to think a little about what
happens when an iterating block like a FOR EACH block
begins. The AVM evaluates the record retrieval the FOR
EACH statement defines. It then goes out to the database and retrieves the
first record of the set of related records that satisfies the statement and makes the
record available to the block. When the block iterates, the AVM goes and gets the next
record. As long as it is possible to identify what the first and the next records are by
using one or more indexes, the AVM does not bother reading all the records in advance.
It just goes out and gets them when the block needs them.
If you specify a BY clause that requires a
search of the database that an index cannot satisfy, the AVM has no choice but to
retrieve all the records in advance and build up a list in sort order. But this is not
ordinarily the case. It is much more efficient simply to get the records when you need
them.
Sometimes, though, you need to force the AVM to get all the records that
satisfy the FOR EACH statement in advance, even when
the sort order itself does not require it. For example, if it is possible that you will
modify an indexed field in some of the records in such a way that they would appear
again later in the retrieval process, you need to make sure that the set of records you
are working with is predetermined. The PRESELECT
keyword tells the AVM to build up a list of pointers to all the records that satisfy the
selection criteria before it starts iterating through the block. This assures you that
each record is accessed only once.
In summary, a REPEAT block does everything a
FOR block does, but it does not automatically
advance to the next record as it iterates. You should use a REPEAT block in cases where you want to control the record navigation
yourself, typically using the FIND statement described
in the next section. It provides you with record, frame, and transaction scoping.
Because it provides all these services, a REPEAT block is
relatively expensive compared to a DO block. Use
the simpler DO block instead of a REPEAT block,
unless you need the record-oriented services provided by the REPEAT block.