Enterprise Edition Home | Express Edition Home | Previous Page | Next Page   Queries and the Query Optimizer > Time Costs of a Query >

Sort-Time Costs

A sort requires in-memory work as well as disk work. The in-memory work depends on the number of columns that are sorted, the width of the combined sort key, and the number of row combinations that pass the query filter. Use the following formula to calculate the in-memory work that a sort operation requires:

Wm = (c * Nfr) + (w * Nfrlog2(Nfr)) 
Wm
is the in-memory work.
c
is the number of columns to order and represents the costs to extract column values from the row and concatenate them into a sort key.
w
is proportional to the width of the combined sort key in bytes and stands for the work to copy or compare one sort key. A numeric value for w depends strongly on the computer hardware in use.
Nfr
is the number of rows that pass the query filter.

Sorting can involve writing information temporarily to disk if the amount of data to sort is large. You can direct the disk writes to occur in the operating-system file space or in a dbspace that the database server manages. For details, see Configuring Dbspaces for Temporary Tables and Sort Files.

The disk work depends on the number of disk pages where rows appear, the number of rows that meet the conditions of the query predicate, the number of rows that can be placed on a sorted page, and the number of merge operations that must be performed. Use the following formula to calculate the disk work that a sort operation requires:

Wd = p + (Nfr/Nrp) * 2 * (m - 1)) 
Wd
is the disk work.
p
is the number of disk pages.
Nfr
is the number of rows that pass the filters.
Nrp
is the number of rows that can be placed on a page.
m
represents the number of levels of merge that the sort must use.

The factor m depends on the number of sort keys that can be held in memory. If there are no filters, Nfr/Nrp is equivalent to p.

When all the keys can be held in memory, m=1 and the disk work is equivalent to p. In other words, the rows are read and sorted in memory.

For moderate to large tables, rows are sorted in batches that fit in memory, and then the batches are merged. When m=2, the rows are read, sorted, and written in batches. Then the batches are read again and merged, resulting in disk work proportional to the following value:

Wd = ooop + (2 * (Nfr/Nrp)) 

The more specific the filters, the fewer the rows that are sorted. As the number of rows increases, and the amount of memory decreases, the amount of disk work increases.

To reduce the cost of sorting, use the following methods:

Enterprise Edition Home | Express Edition Home | [ Top of Page | Previous Page | Next Page | Contents | Index ]