A method to achieve acceptable query performance even when a database
query optimizer chooses an inefficient query plan due to poor cardinality
(row count) estimation includes generating a batch sort query plan which
includes a row count operation and a reorder operation. The batch sort,
and in particular the reorder operation, is not required to satisfy the
query but without these operations the query plan may prove to be
inefficient. When the row count operation determines that the query plan
is inefficient, the row reordering operation reorders rows for more I/O
efficient processing. The results of both the row count operation and the
reorder operation are used to produce the desired rowset to satisfy the
input query without discarding any retrieved rows. The combination of the
row count operation and reorder operation allows a graceful degradation
of query performance where the original query plan selected by a query
optimizer without the batch sort or reorder operation would execute in an
I/O inefficient manner.