Understanding the GetRows Batch Read Mechanism in Data Access Layers: Principles and Practices
This article explains how the GetRows batch read mechanism works in classic data access components and when it pays off. It covers the Rows, Start, and Fields parameters, the column-first two-dimensional array layout, cursor movement rules, and the memory and type risks of loading large result sets. It also shows paging and windowed read patterns, security constraints in browser environments, and how to migrate to a disconnected data model with server-side paging.
1. Why Batch Reading Matters
In relational database access, row-by-row cursor reading is the most straightforward approach: fetch one record, process it, repeat. It keeps memory usage flat, but every loop iteration adds a method call and a type conversion. Once the record count climbs into the tens or hundreds of thousands, that per-row interaction overhead becomes the dominant cost.
Batch reading compresses many round trips into a single fetch. The result set lands in one contiguous block of memory, and the caller then iterates, aggregates, or bulk-writes over it.
GetRows is a classic implementation of this idea in traditional data access components. It drops the entire query result into a two-dimensional array variable. After that, the caller only works with array indices and no longer interacts repeatedly with the data access layer. The value of GetRows is not the syntax itself, but the trade-off it embodies: trading memory for fewer round trips. That trade-off holds under some conditions and backfires under others.
2. Syntax and Parameter Semantics
The call can be abstracted as:
varArray = rs.GetRows([Rows], [Start], [Fields])
Each parameter controls a different aspect:
- Rows: the number of records to read. Omit it or pass -1 to read all remaining records from the current position. If the value exceeds the number of remaining records, all remaining records are returned without an error.
- Start: a bookmark for the starting position. It accepts enumeration values such as the current record, the first record, or the last record, enabling windowed reads that pull a segment forward from a given point.
- Fields: column filtering. It accepts a single field name, a single field index, or an array of names or indices. Its purpose is to avoid moving unused columns, which matters most for wide tables.
One easily overlooked side effect is how the cursor moves. When Rows is smaller than the total record count, the first unread record becomes the new current record, so consecutive calls naturally form pages. When a read reaches the end, the recordset enters an end-of-file state but throws no error. Callers therefore cannot rely on exceptions to detect completion; they must check the state explicitly or track a running count.
3. Array Dimension Convention and Iteration Pattern
The dimension order of the returned array runs counter to intuition: the first dimension maps to fields, the second to record positions. Access is values(fieldIndex, recordIndex). This layout keeps all records of the same field adjacent in memory, which suits column-wise statistics, but it forces row-wise traversal code to swap its loop nesting.
A sketch of the pattern:
Dim values As Variant, fldIndex As Integer, recIndex As Integervalues = rs.GetRows(, , Array("LastName", "FirstName", "BirthDate"))For recIndex = 0 To UBound(values, 2)For fldIndex = 0 To UBound(values)Print values(fldIndex, recIndex),NextNext
In practice, boundary retrieval must use the upper-bound function with a dimension argument for each dimension separately. Hardcoding the column count breaks immediately after fields are trimmed. The return value is also a variant array whose elements retain their original subtypes: numeric columns may be integers or floats, date columns may be date variants, and nulls appear as empty variants. Null checks belong after value retrieval, not before.
4. Memory and Type Risks
The biggest risk of batch reading is turning an efficiency optimization into a memory incident. Three high-risk scenarios deserve separate attention:
- Unbounded result set size. Reading a whole table without filters makes the array grow linearly with the record count and can exhaust memory. The reliable approach is to fetch the total record count first, apply a threshold check, and then decide the read window size.
- Large object fields. Binary or long-text columns can reach megabytes per record, and loading them all inflates usage quickly. A sound strategy is to exclude those columns in Fields, fetch only the primary key and required fields, and look up the rest by primary key when needed.
- Overhead of variant arrays. Variant wrapping carries type and state information, so it occupies more space than a strongly typed array of the same type. The advantage therefore shrinks on very large result sets.
This yields a general principle: batch reading fits medium-sized, field-trimmed scenarios that require multiple passes. For very large volumes or streaming needs, switch to forward-only row-by-row reading combined with server-side paging.
5. Paging and Windowed Read Patterns
Combining Rows and Start produces stable paged reading: read a fixed count each round, continue from the current record position, and stop when the state indicates end-of-file or the accumulated count reaches the target. Compared with loading everything at once, this caps peak memory at a single page while preserving the round-trip savings of batch reading.
Two engineering details matter. First, the sort order must be deterministic; otherwise pages can duplicate or skip records. Second, filtering should happen in the query rather than in the application layer after retrieval, which only adds memory pressure without reducing data transfer.
6. Environment Differences and Security Constraints
Later versions of classic data access components introduced security restrictions that block scripts from non-trusted sites from accessing capabilities related to the local file system. This directly affects the usability of such batch reads in browser script environments. The practical implication is to keep data access logic inside server-side processes, leaving the browser to handle presentation and interaction only, and to obtain already-trimmed result sets through an interface.
7. Migrating to a Disconnected Data Model
The modern data access paradigm defaults to short-lived connections with data materialized locally: the connection closes right after the query, and the result lives in an in-memory table object that upper layers can freely iterate, filter, and serialize. Migration usually focuses on three things:
- Structurally equivalent replacement: the two-dimensional array becomes a table object with column definitions and a row collection. Column names and types become explicit, and access moves from indices to column names or column indexes.
- Adjusted traversal: the outer loop walks the row collection and the inner loop reads values by column name, avoiding the misalignment caused by dimension order.
- Resources and serialization: table objects support filtering, sorting, and format conversion, making it easy to emit a common interchange format for downstream consumers.
If the data volume keeps growing, replace “fetch everything” with server-side paging plus incremental pulls. Use object storage for large objects, message queues for asynchronous decoupling, and monitoring and alerting to watch memory and latency metrics, forming a data access approach that scales sustainably.
8. Summary
GetRows essentially buys cheap repeated traversal with a single call. The benefit comes from fewer interactions; the cost is paid in memory. Mastering parameter semantics, the dimension convention, cursor movement rules, and threshold control yields stable performance gains in batch processing, report export, and bulk validation. At larger scales or in more constrained environments, switching promptly to paged reads and a disconnected data model is the safer engineering choice.
