Fast select for fetch first N rows with order by

A method for fetching an ordered first N rows of a table, includes: reading a row in the table; determining that the read row qualifies as the first N rows of the table for rows read so far, and storing data of the read row; and determining an order of data of qualifying rows and storing the order. Only data in rows that qualify to be among the first N rows are ordered and stored. This provides a significantly more efficient processing. It eliminates tournament tree sorts, corresponding work file read and write I/O's, and associated CPU time. This reduces the time for the running of a query and improves the performance of other applications sharing the use of work files. Further, the improved performance is particular significant when the buffer sizes are within a page of records or rows, although multiple pages may be used.

FIELD OF THE INVENTION

The present invention relates to the sorting of rows in a table, and more particularly to the fetching of first N rows without sorting all the rows of the table.

BACKGROUND OF THE INVENTION

The Fetch First N Rows Only clause in DB2™ enables optimized processing, especially for queries with potentially large result sets when only a limited number of resulting rows is requested. For example, in Fetch First 10 Rows with Order By, it would generally be more efficient to use a non-matching non-clustering index rather than a tablespace scan followed by a sort, especially for a large table with many qualifying rows. However, there can be a severe performance problem if there is no index available on an order by key. In this case, for 100 million row table, up to 100 million rows must be read and sorted, which requires many work file read and write I/O's for multiple sort/merge passes. Then only 10 rows would be read from the sorted result work file. This can consume an enormous amount of resources from both the Input/Output device and processor, which not only delays the particular query, but also potentially many other applications which may share the work file data sets.

Accordingly, there exists a need for a method for a fast fetching of ordered first N rows of a table. The method should fetch the ordered first N rows of a table without requiring work file read and write I/O's, thus providing significantly higher efficiency. The present invention addresses such a problem.

SUMMARY OF THE INVENTION

A method for fetching an ordered first N rows of a table, includes: reading a row in the table; determining that the read row qualifies as the first N rows of the table for rows read so far, and storing data of the read row; and determining an order of data of qualifying rows and storing the order. Only data in rows that qualify to be among the first N rows are ordered and stored. This provides a significantly more efficient processing. It eliminates tournament tree sorts, corresponding work file read and write I/O's, and associated CPU time. This reduces the time for the running of a query and improves the performance of other applications sharing the use of work files. Further, the improved performance is particular significant when the buffer sizes are within a page of records or rows, although multiple pages may be used.

DETAILED DESCRIPTION

To more particularly describe the features of the present invention, please refer toFIGS. 1 through 4Iin conjunction with the discussion below.

FIG. 1is a flowchart illustrating an embodiment of a method for a fast fetching of ordered first N rows of a table in accordance with the present invention. In processing a query to fetch an ordered first N rows from a table, first a row in the table is read, via step101. It is then determined if the row qualifies to be one of the first N rows read so far, via step102. If the row qualifies, then the data in the row is stored along with the other qualifying data, via step103. In this embodiment, the qualifying data are stored in virtual memory instead of a work file, which is typically written to disk. The qualification of the row may mean that the data of a previously read row no longer qualifies. The data of this previously read row would then be discarded, while the data of the current qualifying row is stored per step103. Next, the order of the qualifying data is determined and stored, via step104. If the row data does not qualify, via step102, then it is discarded, via step106. If more rows in the tables remain to the read, then steps101through106are repeated for the next row.

FIG. 2illustrates the buffers used by the method in accordance with the present invention. The buffers comprise a data buffer201, an offset buffer202, and an order buffer203. The data buffer201stores the data and key of the qualifying rows. The length of the data buffer201depends upon the lengths of the data and keys, and the number of rows to be fetched. The offset buffer202stores the offsets from a base address of the data buffer201for each data and key stored therein. Each data/key offset corresponds to a position number, indicating the location in the data buffer201at which a data/key are stored. The order buffer203stores the order of the current qualifying data as an array of the position numbers. For example, if the first 10 rows are to be fetched, each position can be a simple integer between 1 and 10, with each integer corresponding to an offset, and each offset corresponding to a location in the data buffer201. As rows are read, via step101, and qualify, via step102, their data are stored in the data buffer201in one of the positions, via step103. The order of these qualifying data is then determined, via step104, and the position numbers stored in the order buffer203are shifted to reflect this order. With these buffers201-203, the data and keys stored in the data buffer201need not be moved each time the order of qualifying data change. Instead, only the position numbers stored in the order buffer203are changed.

In this manner, only data in rows that qualify to be among the first N rows are ordered and stored. This provides a significantly more efficient processing. It eliminates tournament tree sorts, corresponding work file read and write I/O's, and associated CPU time. This not only reduces the time for the running of a query, it also improves the performance of other applications sharing the use of work files by preventing performance disruption due to the monopolization of shared work files. Further, the improved performance is particularly significant when the buffer sizes are within a page of records or rows, although multiple pages may be usedEfficiency can be further increased by using a binary search of the order buffer203in sorting qualified rows until the search area is less than 5 entries, and then using a sequential search thereafter.

FIG. 3is a flowchart illustrating in more detail the method for a fast fetching of ordered first N rows of a table in accordance with the present invention. First, a data buffer201is set up, via step301. In this embodiment, the length of the data buffer201is (data length+key length)×number of rows. Once the data buffer201is set up, the offsets from the base address of the data buffer201are stored in the offset buffer202, via step302. An order buffer203is also set up to store position numbers, with its length based on the number of rows to be fetched, via step303.

Next, a row of a table is read, via step304. If the data buffer201is not yet full, via step305, then the data and key of the current row is stored in the data buffer201, via306, in the next available position. If the position number of the data/key of the current row is the first entry into the order buffer203, via step307, then the position number for the data/key is simply stored in the order buffer203, via step311. If the position number of the data/key of the current row is not the first entry into the order buffer203, via step307, then the entries in the order buffer303are searched to determine the new order for the keys in the data buffer201, via step308. Optionally, if duplicates are to be avoided, the qualifying row is discarded if an equal key is found in the order buffer203. The position numbers in the order buffer303are then shifted to reflect the new order, via step309. If there are more rows in the table to be read, via step310, the method repeats steps304through311until the data buffer201is full.

When a row is read and the data buffer is full, via steps304and305, the key of the current row is compared with the key of the last element in the order buffer203, via step312. The last element would be the position number of the highest key in the current order. If the key of the current row is higher than the key of the last element, via step313, then it does not qualify as one of the first N rows. This row is discarded, via step314, and the next row is read, via step304. If the key of the current row is lower than the key of the last element, via step313, then the data and key in the data buffer201corresponding to the last element is replaced with the data and key of the current row, via step315. The data/key of the current row thus will have the same position number as the data/key of the last element it replaced. A search of the order buffer203is then performed to determine the new order for the keys in the data buffer201, via step308, and the position numbers in the order buffer203are shifted accordingly, via step309. Steps304through315are then repeated until all rows of the table have been read.

For example, as illustrated inFIGS. 4A-4I, assume that the following Structured Query Language (SQL) query is processed:

Assume that C1is a char(1) not null and C2is a char(2) not null.

First, the data buffer201is set up, via step301. Here, the buffer length=(3+1)×10=40 bytes, where 3 is the length of C1and C2, 1 is the length of the key C1, and 10 is the number of rows to be fetched. The offsets are stored in the offset buffer202, via step302, and the order buffer203is set up, via step303.

Assume that a row is read, via step304, with C1=F, C2=01 and key=F. As illustrated inFIG. 4A, since the data buffer201is not full, via step305, the data/key F01F is stored in the data buffer201at position1, via step306. Since the position number of the data/key F01F is the first entry in the order buffer203, ‘1’ is simply stored in the order buffer203, via step311.

As illustrated inFIG. 4A, assume that the next row read has C1=L, C2=02 and key=L. Since the data buffer201is not full, via step305, the data/key L02L is stored in the data buffer201at position2, via step306. The position number of the data/key L02L is not the first entry in the order buffer203, via step307, so the order buffer203is searched to determine the new order for the keys in the data buffer301, via step308. In this embodiment, a sequential search is performed with 4 or less element in the order buffer203. With more than 4 elements, a binary search is performed. Here, the new order is 1,2.

As illustrated inFIG. 4B, assume that the next row read has C1=C, C2=03 and key =C. Since the data buffer201is not full, via step305, the data/key C03C is stored in the data buffer201at position3, via step306. The position number of the data/key C03C is not the first entry in the order buffer203, via step307, so the order buffer203is sequentially searched to determine the new order for the keys in the data buffer301, via step308. Here, the new order is 3, 1, 2. The position numbers in the order buffer203are then shifted to reflect this new order, via step309.

As illustrated inFIG. 4C, assume that the next row read has C1=X, C2=04 and key =X. Since the data buffer201is not full, via step305, the data/key X04X is stored in the data buffer201at position4, via step306. The position number of the data/key X04X is not the first entry in the order buffer203, via step307, so the order buffer203is sequentially searched to determine the new order for the keys in the data buffer201, via step308. Here, the new order is 3, 1, 2, 4. Since position number ‘4’ is the largest value at this time, no “shift” per se is needed. The position numbers in the order buffer203are then shifted to reflect this new order, via step309.

As illustrated inFIG. 4D, assume that the next row read has C1=Q, C2=05 and key =Q. Since the data buffer201is not full, via step305, the data/key Q05Q is stored in the data buffer201at position5, via step306. The position number of the data/key Q05Q is not the first entry in the order buffer203, via step307, so the order buffer203illustrated inFIG. 4Cis sequentially searched to determine the new order for the keys in the data buffer201, via step308. Here, the new order is 3, 1, 2, 5, 4. The position numbers in the order buffer203illustrated inFIG. 4Care then shifted, and the position number ‘5’ is stored, to reflect this new order, via step309. The resulting order buffer203is illustrated inFIG. 4D.

As illustrated inFIG. 4E, assume that the next row read has C1=M, C2=06 and key =M. Since the data buffer201is not full, via step305, the data/key M06M is placed into the data buffer201at position6, via step306. The position number of the data/key M06M is not the first entry in the order buffer203, via step307, so the order buffer203illustrated inFIG. 4Dis binary searched to determine the new order for the keys in the data buffer201, via step308. In a binary search, M is compared with the key for the position in the middle of the order. Here, the middle position is ‘2’. The key in position2is L. M is higher than L, so M is next compared with the next higher position, ‘5’, which has key Q. M is lower than Q. Thus, the new order is 3, 1, 2, 6, 5, 4. The position numbers in the order buffer203illustrated inFIG. 4Dare then shifted, and the position number ‘6’ is stored, to reflect this new order, via step309. The resulting the order buffer203is illustrated inFIG. 4E.

As illustrated inFIG. 4G, assume now that the next row read has C1=E, C2=11 and key=E. Since the data buffer201is full, via step305, the key E is compared with the key of the last element in the order buffer203illustrated inFIG. 4F, via step312. The last element is ‘4’, and its key is X. E is lower than X, via step313, so E11E qualifies. The data/key E11E then replaces X04X at position4in the data buffer201, via step315. In a binary search, E is compared with the key for the position in the middle of the order. Here, the middle position is ‘2’. The key in position2is L. E is lower than L. In this case, a second binary search is performed, and the middle position is ‘9’. The key in position9is D. E is higher then D and is next compared sequentially with the next higher position, ‘1’, which has key F. E is lower than F. Thus, the new order is 8, 3, 9, 4, 1, 2, 6, 5, 10, 7. The position numbers in the order buffer203illustrated inFIG. 4Fare shifted to reflect this new order, via step309. The resulting order buffer203is illustrated inFIG. 4G.

As illustrated inFIG. 4H, assume now that the next row read has C1=Z, C2=12 and key=Z. Since the data buffer201is full, via step305, the key Z is compared with the key of the last element in the order buffer203illustrated inFIG. 4G, via step312. The last element is ‘7’, and its key is T. Z is higher than T, via step213, so this row is discarded, via step314. As illustrated inFIG. 4H, the order buffer203is not changed.

As illustrated inFIG. 41, assume now that the next row read has C1=R, C2=13 and key=R. Since the data buffer201is full, via step305, the key R is compared with the key of the last element in the order buffer203illustrated inFIG. 4H, via step312. The last element is ‘7’, and its key is T. R is lower than T, via step313, so R13R qualifies. The data/key R13R then replaces T07T at position7in the data buffer201, via step315. In a binary search, R is compared with the key for the position in the middle of the order. Here, the middle position is ‘1’. The key in position1is F. R is higher than F, so R is compared with the next higher position, ‘2’, which has key L. R is higher than L, so R is compared with the next higher position, ‘6’, which has key M. R is higher than M, so R is compared with the next higher position, ‘5’, which has key Q. R is higher than Q, so R is compared with the next higher position, ‘10’, which has key S. R is lower than S. Thus, the new order is 8, 3, 9, 4, 1, 2, 6, 5, 7, 10. The position numbers in the order buffer203illustrated inFIG. 4Hare shifted to reflect this new order, via step309. The resulting order buffer203is illustrated inFIG. 4I.

Although the method is described in the context of the buffers above, one of ordinary skill in the art will understand that other implementations are possible without departing from the spirit and scope of the present invention. For example, only a data buffer may be used, where the data/keys stored in the data buffer are moved each time the order changes. Alternatively, only the data buffer and the offset buffer may be used, where the offsets moved each time the order changes.

A method for a fast fetching of ordered first N rows of a table is disclosed.

In a particular embodiment, a method for fetching an ordered first N rows of a table is disclosed, where N indicates a number of rows to be fetched. The method includes reading a row of the table. The method includes creating a data buffer to store data and to store keys of qualifying rows. The method includes creating an offset buffer to store offsets to a base address of the data buffer corresponding to the data and the keys stored at the data buffer. The method also includes creating an order buffer to store position numbers corresponding to the data and the keys of qualifying rows stored at the data buffer. A length of the data buffer is based on the lengths of the data and the key of a qualifying row. The method also includes discarding a read row when the read row does not qualify as the first N rows of the table.

The method also includes determining that the read row qualifies as the first N rows of the table for rows read so far and storing data of the read row when the read row qualifies. The method also includes storing the data and a key of the read row at the data buffer when the data buffer is not full. When the data buffer is full, the method also includes comparing a key of the read row with a key of a last element of the order buffer, and replacing a data and the key of the data buffer corresponding to the last element with the data and the key of the read row when the key of the read row is lower than the key of the last element. The method also includes discarding the read row when the key of the read row is higher than the key of the last element.

The method also includes determining an order of data of qualifying rows and storing the order at the order buffer. The method also includes determining whether a position number is a first or a second entry of the order buffer, where the position number corresponds to the data and the key of the read row. The method includes searching the order buffer to determine an order of keys for the qualifying rows and shifting the position numbers in the order buffer to reflect the determined order. The method includes storing the position number at the order buffer.

In a particular embodiment, a computer readable medium includes program instructions, that when executed by a processor, cause the processor to fetch an ordered first N rows of a table, where N is a number of rows to be fetched. The computer readable medium also includes program instructions, that when executed by the processor, cause the processor to read a row of the table. The program instructions to read the row include program instructions to create a data buffer to store data and keys of qualifying rows. The program instructions to read the row include program instructions to store, at an offset buffer, offsets to a base address of the data buffer corresponding to the data and the keys stored in the data buffer. The program instructions to read the row include program instructions to create an order buffer to store position numbers corresponding to the data and the keys stored of the qualifying row at the data buffer. The length of the data buffer is based on the length of the data and based on the key for a qualifying row.

The computer readable medium also includes program instructions, that when executed by the processor, cause the processor to determine that the read row qualifies as the first N rows of the table for rows read so far and to store data of the read row. The program instructions to determine that the read row qualifies include program instructions to determine that the data buffer is not full and storing the data and a key of the read row in the data buffer when the data buffer is not full. The program instructions to determine that the read row qualifies include program instructions to determine that the data buffer is full, to compare a key of the read row with a key of a last element in an order buffer, and to replace data and the key in the data buffer corresponding to the last element with the data and the key of the read row when the key of the read row is lower than the key of the last element. The program instructions to determine that the read row qualifies include program instructions to discard the read row when the key of the read row is higher than the key of the last element.

The computer readable medium also includes program instructions, that when executed by the processor, cause the processor to determine an order of data of qualifying rows and to store the order. The program instructions to determine the order include program instructions to determine whether a position number is a first or second entry of the order buffer. The position number corresponds to the data and a key of the read row, and the program instructions store the position number in the order buffer. The program instructions to determine the order include program instructions to search an order buffer to determine a new order of keys of the qualifying rows and to shift the position numbers of the order buffer to reflect the new order. The computer readable medium also includes program instructions, that when executed by the processor, cause the processor to discard the read row if the read row does not qualify as the first N rows.