US20080288495A1 - Fast select for fetch first n rows with order by - Google Patents
Fast select for fetch first n rows with order by Download PDFInfo
- Publication number
- US20080288495A1 US20080288495A1 US12/185,102 US18510208A US2008288495A1 US 20080288495 A1 US20080288495 A1 US 20080288495A1 US 18510208 A US18510208 A US 18510208A US 2008288495 A1 US2008288495 A1 US 2008288495A1
- Authority
- US
- United States
- Prior art keywords
- data
- rows
- buffer
- key
- order
- Prior art date
- Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
- Abandoned
Links
Images
Classifications
-
- G—PHYSICS
- G06—COMPUTING OR CALCULATING; COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F16/00—Information retrieval; Database structures therefor; File system structures therefor
- G06F16/20—Information retrieval; Database structures therefor; File system structures therefor of structured data, e.g. relational data
- G06F16/24—Querying
- G06F16/245—Query processing
- G06F16/2455—Query execution
- G06F16/24553—Query execution of query operations
Definitions
- 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.
- the Fetch First N Rows Only clause in DB2TM enables optimized processing, especially for queries with potentially large result sets when only a limited number of resulting rows is requested.
- Fetch First 10 Rows with Order 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.
- there can be a severe performance problem if there is no index available on an order by key.
- up to 100 million rows must be read and sorted, which requires many work files 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.
- the present invention addresses such a problem.
- a system and computer readable medium 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.
- FIG. 1 is 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.
- FIG. 2 illustrates the buffers used by the method in accordance with the present invention.
- FIG. 3 is 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.
- FIG. 4A through 4I illustrate an example of the method in accordance with the present invention. I would like to make the following changes in FIGS. 4A-4I
- the present invention provides a method for a fast fetching of ordered first N rows of a table.
- the following description is presented to enable one of ordinary skill in the art to make and use the invention and is provided in the context of a patent application and its requirements. Various modifications to the preferred embodiment will be readily apparent to those skilled in the art and the generic principles herein may be applied to other embodiments. Thus, the present invention is not intended to be limited to the embodiment shown but is to be accorded the widest scope consistent with the principles and features described herein.
- FIGS. 1 through 4I in conjunction with the discussion below.
- FIG. 1 is 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.
- a row in the table is read, via step 101 . It is then determined if the row qualifies to be one of the first N rows read so far, via step 102 . If the row qualifies, then the data in the row is stored along with the other qualifying data, via step 103 .
- 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.
- step 104 the order of the qualifying data is determined and stored, via step 104 . If the row data does not qualify, via step 102 , then it is discarded, via step 106 . If more rows in the tables remain to the read, then steps 101 through 106 are repeated for the next row.
- FIG. 2 illustrates the buffers used by the method in accordance with the present invention.
- the buffers comprise a data buffer 201 , an offset buffer 202 , and an order buffer 203 .
- the data buffer 201 stores the data and key of the qualifying rows.
- the length of the data buffer 201 depends upon the lengths of the data and keys, and the number of rows to be fetched.
- the offset buffer 202 stores the offsets from a base address of the data buffer 201 for each data and key stored therein. Each data/key offset corresponds to a position number, indicating the location in the data buffer 201 at which a data/key are stored.
- the order buffer 203 stores the order of the current qualifying data as an array of the position numbers.
- 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 buffer 201 .
- rows are read, via step 101 , and qualify, via step 102 , their data are stored in the data buffer 201 in one of the positions, via step 103 .
- the order of these qualifying data is then determined, via step 104 , and the position numbers stored in the order buffer 203 are shifted to reflect this order.
- the data and keys stored in the data buffer 201 need not be moved each time the order of qualifying data change. Instead, only the position numbers stored in the order buffer 203 are changed.
- FIG. 3 is 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.
- a data buffer 201 is set up, via step 301 .
- the length of the data buffer 201 is (data length+key length) ⁇ number of rows.
- the offsets from the base address of the data buffer 201 are stored in the offset buffer 202 , via step 302 .
- An order buffer 203 is also set up to store position numbers, with its length based on the number of rows to be fetched, via step 303 .
- a row of a table is read, via step 304 . If the data buffer 201 is not yet full, via step 305 , then the data and key of the current row is stored in the data buffer 201 , via 306 , in the next available position. If the position number of the data/key of the current row is the first entry into the order buffer 203 , via step 307 , then the position number for the data/key is simply stored in the order buffer 203 , via step 311 . If the position number of the data/key of the current row is not the first entry into the order buffer 203 , via step 307 , then the entries in the order buffer 303 are searched to determine the new order for the keys in the data buffer 201 , via step 308 .
- the qualifying row is discarded if an equal key is found in the order buffer 203 .
- the position numbers in the order buffer 303 are then shifted to reflect the new order, via step 309 . If there are more rows in the table to be read, via step 310 , the method repeats steps 304 through 311 until the data buffer 201 is full.
- the key of the current row is compared with the key of the last element in the order buffer 203 , via step 312 .
- 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 step 313 , then it does not qualify as one of the first N rows. This row is discarded, via step 314 , and the next row is read, via step 304 . If the key of the current row is lower than the key of the last element, via step 313 , then the data and key in the data buffer 201 corresponding to the last element is replaced with the data and key of the current row, via step 315 .
- a search of the order buffer 203 is then performed to determine the new order for the keys in the data buffer 201 , via step 308 , and the position numbers in the order buffer 203 are shifted accordingly, via step 309 . Steps 304 through 315 are then repeated until all rows of the table have been read.
- SQL Structured Query Language
- the data buffer 201 is set up, via step 301 .
- the offsets are stored in the offset buffer 202 , via step 302 , and the order buffer 203 is set up, via step 303 .
- the data/key L02L is stored in the data buffer 201 at position 2 , via step 306 .
- the position number of the data/key L02L is not the first entry in the order buffer 203 , via step 307 , so the order buffer 203 is searched to determine the new order for the keys in the data buffer 301 , via step 308 .
- a sequential search is performed with 4 or less element in the order buffer 203 . With more than 4 elements, a binary search is performed.
- the new order is 1, 2.
- step 305 the data/key C03C is stored in the data buffer 201 at position 3 , via step 306 .
- the position number of the data/key C03C is not the first entry in the order buffer 203 , via step 307 , so the order buffer 203 is sequentially searched to determine the new order for the keys in the data buffer 301 , via step 308 .
- the new order is 3, 1, 2.
- the position numbers in the order buffer 203 are then shifted to reflect this new order, via step 309 .
- step 305 the data/key X04X is stored in the data buffer 201 at position 4 , via step 306 .
- the position number of the data/key X04X is not the first entry in the order buffer 203 , via step 307 , so the order buffer 203 is sequentially searched to determine the new order for the keys in the data buffer 201 , via step 308 .
- 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 buffer 203 are then shifted to reflect this new order, via step 309 .
- the data/key Q05Q is stored in the data buffer 201 at position 5 , via step 306 .
- the position number of the data/key Q05Q is not the first entry in the order buffer 203 , via step 307 , so the order buffer 203 illustrated in FIG. 4C is sequentially searched to determine the new order for the keys in the data buffer 201 , via step 308 .
- the new order is 3, 1, 2, 5, 4.
- the position numbers in the order buffer 203 illustrated in FIG. 4C are then shifted, and the position number ‘5’ is stored, to reflect this new order, via step 309 .
- the resulting order buffer 203 is illustrated in FIG. 4D .
- the data/key M06M is placed into the data buffer 201 at position 6 , via step 306 .
- the position number of the data/key M06M is not the first entry in the order buffer 203 , via step 307 , so the order buffer 203 illustrated in FIG. 4D is binary searched to determine the new order for the keys in the data buffer 201 , via step 308 .
- M is compared with the key for the position in the middle of the order.
- the middle position is ‘2’.
- the key in position 2 is 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.
- the new order is 3, 1, 2, 6, 5, 4.
- the position numbers in the order buffer 203 illustrated in FIG. 4D are then shifted, and the position number ‘6’ is stored, to reflect this new order, via step 309 .
- the resulting the order buffer 203 is illustrated in FIG. 4E .
- the resulting data buffer 201 and order buffer 203 are illustrated in FIG. 4F .
- the key E is compared with the key of the last element in the order buffer 203 illustrated in FIG. 4F , via step 312 .
- the last element is ‘4’, and its key is X.
- E is lower than X, via step 313 , so E11 E qualifies.
- the data/key E11 E then replaces X04X at position 4 in the data buffer 201 , via step 315 .
- E is compared with the key for the position in the middle of the order.
- the middle position is ‘2’.
- the key in position 2 is L.
- E is lower than L.
- a second binary search is performed, and the middle position is ‘9’.
- the key in position 9 is 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.
- the new order is 8, 3, 9, 4, 1, 2, 6, 5, 10, 7.
- the position numbers in the order buffer 203 illustrated in FIG. 4F are shifted to reflect this new order, via step 309 .
- the resulting order buffer 203 is illustrated in FIG. 4G .
- step 305 the key Z is compared with the key of the last element in the order buffer 203 illustrated in FIG. 4G , via step 312 .
- the last element is ‘7’, and its key is T.
- Z is higher than T, via step 213 , so this row is discarded, via step 314 .
- the order buffer 203 is not changed.
- step 305 the key R is compared with the key of the last element in the order buffer 203 illustrated in FIG. 4H , via step 312 .
- the last element is ‘7’, and its key is T.
- R is lower than T, via step 313 , so R13R qualifies.
- the data/key R13R then replaces T07T at position 7 in the data buffer 201 , via step 315 .
- R is compared with the key for the position in the middle of the order.
- the middle position is ‘1’.
- the key in position 1 is 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.
- the new order is 8, 3, 9, 4, 1, 2, 6, 5, 7, 10.
- the position numbers in the order buffer 203 illustrated in FIG. 4H are shifted to reflect this new order, via step 309 .
- the resulting order buffer 203 is illustrated in FIG. 4I .
- a method for a fast fetching of ordered first N rows of a table is disclosed.
Landscapes
- Engineering & Computer Science (AREA)
- Theoretical Computer Science (AREA)
- Computational Linguistics (AREA)
- Data Mining & Analysis (AREA)
- Databases & Information Systems (AREA)
- Physics & Mathematics (AREA)
- General Engineering & Computer Science (AREA)
- General Physics & Mathematics (AREA)
- Information Retrieval, Db Structures And Fs Structures Therefor (AREA)
Abstract
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.
Description
- This application claims the benefit of priority under 35 USC 120 as a continuation application to U.S. application Ser. No. 11/222,894, entitled “FAST SELECT FOR FETCH FIRST N ROWS WITH ORDER BY,” filed on Sep. 8, 2005, the contents of which are hereby incorporated by reference in its entirety.
- 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.
- 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 files 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.
- A system and computer readable medium 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.
-
FIG. 1 is 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. -
FIG. 2 illustrates the buffers used by the method in accordance with the present invention. -
FIG. 3 is 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. -
FIG. 4A through 4I illustrate an example of the method in accordance with the present invention. I would like to make the following changes inFIGS. 4A-4I - The present invention provides a method for a fast fetching of ordered first N rows of a table. The following description is presented to enable one of ordinary skill in the art to make and use the invention and is provided in the context of a patent application and its requirements. Various modifications to the preferred embodiment will be readily apparent to those skilled in the art and the generic principles herein may be applied to other embodiments. Thus, the present invention is not intended to be limited to the embodiment shown but is to be accorded the widest scope consistent with the principles and features described herein.
- To more particularly describe the features of the present invention, please refer to
FIGS. 1 through 4I in conjunction with the discussion below. -
FIG. 1 is 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, viastep 101. It is then determined if the row qualifies to be one of the first N rows read so far, viastep 102. If the row qualifies, then the data in the row is stored along with the other qualifying data, viastep 103. 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 perstep 103. Next, the order of the qualifying data is determined and stored, viastep 104. If the row data does not qualify, viastep 102, then it is discarded, viastep 106. If more rows in the tables remain to the read, thensteps 101 through 106 are repeated for the next row. -
FIG. 2 illustrates the buffers used by the method in accordance with the present invention. The buffers comprise adata buffer 201, anoffset buffer 202, and anorder buffer 203. Thedata buffer 201 stores the data and key of the qualifying rows. The length of thedata buffer 201 depends upon the lengths of the data and keys, and the number of rows to be fetched. Theoffset buffer 202 stores the offsets from a base address of thedata buffer 201 for each data and key stored therein. Each data/key offset corresponds to a position number, indicating the location in thedata buffer 201 at which a data/key are stored. Theorder buffer 203 stores 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 thedata buffer 201. As rows are read, viastep 101, and qualify, viastep 102, their data are stored in thedata buffer 201 in one of the positions, viastep 103. The order of these qualifying data is then determined, viastep 104, and the position numbers stored in theorder buffer 203 are shifted to reflect this order. With these buffers 201-203, the data and keys stored in thedata buffer 201 need not be moved each time the order of qualifying data change. Instead, only the position numbers stored in theorder buffer 203 are 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 used Efficiency can be further increased by using a binary search of the
order buffer 203 in sorting qualified rows until the search area is less than 5 entries, and then using a sequential search thereafter. -
FIG. 3 is 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, adata buffer 201 is set up, viastep 301. In this embodiment, the length of thedata buffer 201 is (data length+key length)×number of rows. Once thedata buffer 201 is set up, the offsets from the base address of thedata buffer 201 are stored in the offsetbuffer 202, viastep 302. Anorder buffer 203 is also set up to store position numbers, with its length based on the number of rows to be fetched, viastep 303. - Next, a row of a table is read, via
step 304. If thedata buffer 201 is not yet full, viastep 305, then the data and key of the current row is stored in thedata buffer 201, via 306, in the next available position. If the position number of the data/key of the current row is the first entry into theorder buffer 203, viastep 307, then the position number for the data/key is simply stored in theorder buffer 203, viastep 311. If the position number of the data/key of the current row is not the first entry into theorder buffer 203, viastep 307, then the entries in theorder buffer 303 are searched to determine the new order for the keys in thedata buffer 201, viastep 308. Optionally, if duplicates are to be avoided, the qualifying row is discarded if an equal key is found in theorder buffer 203. The position numbers in theorder buffer 303 are then shifted to reflect the new order, viastep 309. If there are more rows in the table to be read, viastep 310, the method repeatssteps 304 through 311 until thedata buffer 201 is full. - When a row is read and the data buffer is full, via
304 and 305, the key of the current row is compared with the key of the last element in thesteps order buffer 203, viastep 312. 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, viastep 313, then it does not qualify as one of the first N rows. This row is discarded, viastep 314, and the next row is read, viastep 304. If the key of the current row is lower than the key of the last element, viastep 313, then the data and key in thedata buffer 201 corresponding to the last element is replaced with the data and key of the current row, viastep 315. 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 theorder buffer 203 is then performed to determine the new order for the keys in thedata buffer 201, viastep 308, and the position numbers in theorder buffer 203 are shifted accordingly, viastep 309.Steps 304 through 315 are then repeated until all rows of the table have been read. - For example, as illustrated in
FIGS. 4A-4I , assume that the following Structured Query Language (SQL) query is processed: - SELECT C1,C2 FROM T3 ORDER BY C1 FETCH FIRST 10 ROWS ONLY;.
- Assume that C1 is a char(1) not null and C2 is a char(2) not null.
- First, the
data buffer 201 is set up, viastep 301. Here, the buffer length=(3+1)×10=40 bytes, where 3 is the length of C1 and 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 offsetbuffer 202, viastep 302, and theorder buffer 203 is set up, viastep 303. - Assume that a row is read, via
step 304, with C1=F, C2=01 and key=F. As illustrated inFIG. 4A , since thedata buffer 201 is not full, viastep 305, the data/key F01F is stored in thedata buffer 201 atposition 1, viastep 306. Since the position number of the data/key F01F is the first entry in theorder buffer 203, ‘1’ is simply stored in theorder buffer 203, viastep 311. - As illustrated in
FIG. 4A , assume that the next row read has C1=L, C2=02 and key=L. Since thedata buffer 201 is not full, viastep 305, the data/key L02L is stored in thedata buffer 201 atposition 2, viastep 306. The position number of the data/key L02L is not the first entry in theorder buffer 203, viastep 307, so theorder buffer 203 is searched to determine the new order for the keys in thedata buffer 301, viastep 308. In this embodiment, a sequential search is performed with 4 or less element in theorder buffer 203. With more than 4 elements, a binary search is performed. Here, the new order is 1, 2. - As illustrated in
FIG. 4B , assume that the next row read has C1=C, C2=03 and key=C. Since thedata buffer 201 is not full, viastep 305, the data/key C03C is stored in thedata buffer 201 atposition 3, viastep 306. The position number of the data/key C03C is not the first entry in theorder buffer 203, viastep 307, so theorder buffer 203 is sequentially searched to determine the new order for the keys in thedata buffer 301, viastep 308. Here, the new order is 3, 1, 2. The position numbers in theorder buffer 203 are then shifted to reflect this new order, viastep 309. - As illustrated in
FIG. 4C , assume that the next row read has C1=X, C2=04 and key=X. Since thedata buffer 201 is not full, viastep 305, the data/key X04X is stored in thedata buffer 201 atposition 4, viastep 306. The position number of the data/key X04X is not the first entry in theorder buffer 203, viastep 307, so theorder buffer 203 is sequentially searched to determine the new order for the keys in thedata buffer 201, viastep 308. 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 theorder buffer 203 are then shifted to reflect this new order, viastep 309. - As illustrated in
FIG. 4D , assume that the next row read has C1=Q, C2=05 and key=Q. Since thedata buffer 201 is not full, viastep 305, the data/key Q05Q is stored in thedata buffer 201 atposition 5, viastep 306. The position number of the data/key Q05Q is not the first entry in theorder buffer 203, viastep 307, so theorder buffer 203 illustrated inFIG. 4C is sequentially searched to determine the new order for the keys in thedata buffer 201, viastep 308. Here, the new order is 3, 1, 2, 5, 4. The position numbers in theorder buffer 203 illustrated inFIG. 4C are then shifted, and the position number ‘5’ is stored, to reflect this new order, viastep 309. The resultingorder buffer 203 is illustrated inFIG. 4D . - As illustrated in
FIG. 4E , assume that the next row read has C1=M, C2=06 and key=M. Since thedata buffer 201 is not full, viastep 305, the data/key M06M is placed into thedata buffer 201 atposition 6, viastep 306. The position number of the data/key M06M is not the first entry in theorder buffer 203, viastep 307, so theorder buffer 203 illustrated inFIG. 4D is binary searched to determine the new order for the keys in thedata buffer 201, viastep 308. 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 inposition 2 is 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 theorder buffer 203 illustrated inFIG. 4D are then shifted, and the position number ‘6’ is stored, to reflect this new order, viastep 309. The resulting theorder buffer 203 is illustrated inFIG. 4E . -
Steps 304 through 311 are repeated for the next rows, with C1=T, A, D, and S and C2=07, 08, 09, 10 respectively. The resultingdata buffer 201 andorder buffer 203 are illustrated inFIG. 4F . - As illustrated in
FIG. 4G , assume now that the next row read has C1=E, C2=11 and key=E. Since thedata buffer 201 is full, viastep 305, the key E is compared with the key of the last element in theorder buffer 203 illustrated inFIG. 4F , viastep 312. The last element is ‘4’, and its key is X. E is lower than X, viastep 313, so E11 E qualifies. The data/key E11 E then replaces X04X atposition 4 in thedata buffer 201, viastep 315. 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 inposition 2 is L. E is lower than L. In this case, a second binary search is performed, and the middle position is ‘9’. The key inposition 9 is 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 theorder buffer 203 illustrated inFIG. 4F are shifted to reflect this new order, viastep 309. The resultingorder buffer 203 is illustrated inFIG. 4G . - As illustrated in
FIG. 4H , assume now that the next row read has C1=Z, C2=12 and key=Z. Since thedata buffer 201 is full, viastep 305, the key Z is compared with the key of the last element in theorder buffer 203 illustrated inFIG. 4G , viastep 312. The last element is ‘7’, and its key is T. Z is higher than T, via step 213, so this row is discarded, viastep 314. As illustrated inFIG. 4H , theorder buffer 203 is not changed. - As illustrated in
FIG. 4I , assume now that the next row read has C1=R, C2=13 and key=R. Since thedata buffer 201 is full, viastep 305, the key R is compared with the key of the last element in theorder buffer 203 illustrated inFIG. 4H , viastep 312. The last element is ‘7’, and its key is T. R is lower than T, viastep 313, so R13R qualifies. The data/key R13R then replaces T07T atposition 7 in thedata buffer 201, viastep 315. 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 inposition 1 is 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 theorder buffer 203 illustrated inFIG. 4H are shifted to reflect this new order, viastep 309. The resultingorder buffer 203 is 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.
- Although the present invention has been described in accordance with the embodiments shown, one of ordinary skill in the art will readily recognize that there could be variations to the embodiments and those variations would be within the spirit and scope of the present invention. Accordingly, many modifications may be made by one of ordinary skill in the art without departing from the spirit and scope of the appended claims.
Claims (8)
1. A computer readable medium encoded with program instructions for fetching an ordered first N rows of a table, wherein N is a number of rows to the fetched, the program instructions configured for:
(a) reading a row in the table;
(b) 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
(c) determining an order of data of qualifying rows and storing the order, comprising:
(c1) searching an order buffer to determine a new order of keys for the qualifying rows, wherein the order buffer stores position numbers corresponding to data and keys of the qualifying rows, and
(c2) shifting the position numbers in the order buffer to reflect the new order.
2. The computer readable medium of claim 1 , wherein the reading (a) comprises:
(a1) setting up a data buffer for storing data and keys for the qualifying rows;
(a2) storing in an offset buffer offsets to a base address of the data buffer corresponding to the data and the keys stored in the data buffer; and
(a3) setting up an order buffer for storing position numbers corresponding to the data and the keys stored in the data buffer.
3. The computer readable medium of claim 2 , wherein a length of the data buffer is equal to the lengths of the data and the key for a qualifying row multiplied by N.
4. The computer readable medium of claim 1 , wherein the determining (b) comprises:
(b1) determining that a data buffer is not full, wherein the data buffer stores data and keys for the qualifying rows; and
(b2) storing the data and a key of the read row in the data buffer.
5. The computer readable medium of claim 1 , wherein the determining (b) comprises:
(b1) determining that a data buffer is full, wherein the data buffer stores data and keys for the qualifying rows;
(b2) comparing a key of the read row with a key of a last element in an order buffer, wherein the order buffer stores position numbers corresponding to the data and the keys stored in the data buffer; and
(b3) replacing a data and the key in the data buffer corresponding to the last element with the data and the key of the read row, if the key of the read row is lower than the key of the last element.
6. The computer readable medium of claim 5 , wherein the determining (b) further comprises:
(b4) discarding the read row if the key of the read row is higher than the key of the last element.
7. The computer readable medium of claim 1 , wherein the determining (c) comprises:
(c1) determining a position number is a first or second entry in an order buffer, wherein the position number corresponds to the data and a key of the read row, wherein the order buffer stores position numbers corresponding to data and keys of the qualifying rows; and
(c2) storing the position number in the order buffer.
8. The computer readable medium of claim 1 , further comprising:
(d) discarding the read row if the read row does not qualify as the first N rows of the table for rows read so far.
Priority Applications (1)
| Application Number | Priority Date | Filing Date | Title |
|---|---|---|---|
| US12/185,102 US20080288495A1 (en) | 2005-09-08 | 2008-08-03 | Fast select for fetch first n rows with order by |
Applications Claiming Priority (2)
| Application Number | Priority Date | Filing Date | Title |
|---|---|---|---|
| US11/222,894 US7792825B2 (en) | 2005-09-08 | 2005-09-08 | Fast select for fetch first N rows with order by |
| US12/185,102 US20080288495A1 (en) | 2005-09-08 | 2008-08-03 | Fast select for fetch first n rows with order by |
Related Parent Applications (1)
| Application Number | Title | Priority Date | Filing Date |
|---|---|---|---|
| US11/222,894 Continuation US7792825B2 (en) | 2005-09-08 | 2005-09-08 | Fast select for fetch first N rows with order by |
Publications (1)
| Publication Number | Publication Date |
|---|---|
| US20080288495A1 true US20080288495A1 (en) | 2008-11-20 |
Family
ID=37856488
Family Applications (2)
| Application Number | Title | Priority Date | Filing Date |
|---|---|---|---|
| US11/222,894 Expired - Fee Related US7792825B2 (en) | 2005-09-08 | 2005-09-08 | Fast select for fetch first N rows with order by |
| US12/185,102 Abandoned US20080288495A1 (en) | 2005-09-08 | 2008-08-03 | Fast select for fetch first n rows with order by |
Family Applications Before (1)
| Application Number | Title | Priority Date | Filing Date |
|---|---|---|---|
| US11/222,894 Expired - Fee Related US7792825B2 (en) | 2005-09-08 | 2005-09-08 | Fast select for fetch first N rows with order by |
Country Status (1)
| Country | Link |
|---|---|
| US (2) | US7792825B2 (en) |
Families Citing this family (5)
| Publication number | Priority date | Publication date | Assignee | Title |
|---|---|---|---|---|
| US7724920B2 (en) | 1995-05-08 | 2010-05-25 | Digimarc Corporation | Digital authentication with analog documents |
| US7756892B2 (en) | 2000-05-02 | 2010-07-13 | Digimarc Corporation | Using embedded data with file sharing |
| US20090063458A1 (en) * | 2007-08-31 | 2009-03-05 | International Business Machines Corporation | method and system for minimizing sorting |
| US9582540B2 (en) | 2014-08-21 | 2017-02-28 | International Business Machines Corporation | Feedback mechanism providing row-level filtering earlier in a plan |
| US12298911B2 (en) * | 2023-07-06 | 2025-05-13 | Sap Se | Spill-to-disk in projection operations |
Citations (10)
| Publication number | Priority date | Publication date | Assignee | Title |
|---|---|---|---|---|
| US4209845A (en) * | 1977-01-25 | 1980-06-24 | International Business Machines Corporation | File qualifying and sorting system |
| US4417321A (en) * | 1981-05-18 | 1983-11-22 | International Business Machines Corp. | Qualifying and sorting file record data |
| US6546382B1 (en) * | 1999-11-03 | 2003-04-08 | Oracle Corporation | Finding the TOP N values through the execution of a query |
| US20040044683A1 (en) * | 2000-07-31 | 2004-03-04 | Furusho Shinji | Data compiling method |
| US6708273B1 (en) * | 1997-09-16 | 2004-03-16 | Safenet, Inc. | Apparatus and method for implementing IPSEC transforms within an integrated circuit |
| US20050004924A1 (en) * | 2003-04-29 | 2005-01-06 | Adrian Baldwin | Control of access to databases |
| US20050076018A1 (en) * | 2003-10-07 | 2005-04-07 | Neidecker-Lutz Burkhard K. | Sorting result buffer |
| US20060101086A1 (en) * | 2004-11-08 | 2006-05-11 | Sas Institute Inc. | Data sorting method and system |
| US20060248079A1 (en) * | 2005-04-28 | 2006-11-02 | Freescale Semiconductor Incorporated | Method and apparatus for finding a perfect hash function and making minimal hash table for a given set of keys |
| US7191171B2 (en) * | 2003-12-12 | 2007-03-13 | Alpine Electronics, Inc. | Data representation and retrieval method and apparatus for high speed data search and retrieval |
Family Cites Families (3)
| Publication number | Priority date | Publication date | Assignee | Title |
|---|---|---|---|---|
| US5666525A (en) * | 1995-09-21 | 1997-09-09 | The Trustees Of Columbia University In The City Of New York | System and method for performing an efficient join operation on large tables with a small main memory |
| US7017005B2 (en) * | 2002-08-28 | 2006-03-21 | Hywire Ltd. | Implementation of a content addressable memory using a RAM-cell structure |
| US6907328B2 (en) * | 2003-06-10 | 2005-06-14 | Motorola, Inc. | Switch apparatus for a driver information interface |
-
2005
- 2005-09-08 US US11/222,894 patent/US7792825B2/en not_active Expired - Fee Related
-
2008
- 2008-08-03 US US12/185,102 patent/US20080288495A1/en not_active Abandoned
Patent Citations (10)
| Publication number | Priority date | Publication date | Assignee | Title |
|---|---|---|---|---|
| US4209845A (en) * | 1977-01-25 | 1980-06-24 | International Business Machines Corporation | File qualifying and sorting system |
| US4417321A (en) * | 1981-05-18 | 1983-11-22 | International Business Machines Corp. | Qualifying and sorting file record data |
| US6708273B1 (en) * | 1997-09-16 | 2004-03-16 | Safenet, Inc. | Apparatus and method for implementing IPSEC transforms within an integrated circuit |
| US6546382B1 (en) * | 1999-11-03 | 2003-04-08 | Oracle Corporation | Finding the TOP N values through the execution of a query |
| US20040044683A1 (en) * | 2000-07-31 | 2004-03-04 | Furusho Shinji | Data compiling method |
| US20050004924A1 (en) * | 2003-04-29 | 2005-01-06 | Adrian Baldwin | Control of access to databases |
| US20050076018A1 (en) * | 2003-10-07 | 2005-04-07 | Neidecker-Lutz Burkhard K. | Sorting result buffer |
| US7191171B2 (en) * | 2003-12-12 | 2007-03-13 | Alpine Electronics, Inc. | Data representation and retrieval method and apparatus for high speed data search and retrieval |
| US20060101086A1 (en) * | 2004-11-08 | 2006-05-11 | Sas Institute Inc. | Data sorting method and system |
| US20060248079A1 (en) * | 2005-04-28 | 2006-11-02 | Freescale Semiconductor Incorporated | Method and apparatus for finding a perfect hash function and making minimal hash table for a given set of keys |
Also Published As
| Publication number | Publication date |
|---|---|
| US7792825B2 (en) | 2010-09-07 |
| US20070061280A1 (en) | 2007-03-15 |
Similar Documents
| Publication | Publication Date | Title |
|---|---|---|
| US7743060B2 (en) | Architecture for an indexer | |
| US10114908B2 (en) | Hybrid table implementation by using buffer pool as permanent in-memory storage for memory-resident data | |
| US6185557B1 (en) | Merge join process | |
| US6678687B2 (en) | Method for creating an index and method for searching an index | |
| US9043310B2 (en) | Accessing a dimensional data model when processing a query | |
| Stone | Parallel querying of large databases: A case study | |
| US20040205044A1 (en) | Method for storing inverted index, method for on-line updating the same and inverted index mechanism | |
| US6415375B2 (en) | Information storage and retrieval system | |
| US20030097354A1 (en) | Method and system for index sampled tablescan | |
| US10545960B1 (en) | System and method for set overlap searching of data lakes | |
| US20170046394A1 (en) | Fast incremental column store data loading | |
| US20080288495A1 (en) | Fast select for fetch first n rows with order by | |
| US7987205B1 (en) | Integrated search engine devices having pipelined node maintenance sub-engines therein that support database flush operations | |
| US20080114752A1 (en) | Querying across disparate schemas | |
| US7685138B2 (en) | Virtual cursors for XML joins | |
| US7533245B2 (en) | Hardware assisted pruned inverted index component | |
| US20150058351A1 (en) | Queries for thin database indexing | |
| US20110231404A1 (en) | File storage and retrieval method | |
| WO2008085358A1 (en) | Accelerating queries using temporary enumeration representation | |
| US20080114780A1 (en) | Efficient database data type for large objects | |
| US20050055364A1 (en) | Hardware assisted pruned inverted index component | |
| Kamath et al. | Bucket skip merge join: A scalable algorithm for join processing in very large databases using indexes | |
| Hentschel et al. | Entropy-learned hashing: 10x faster hashing with controllable uniformity | |
| US20250321946A1 (en) | System and Method for Data Record Organization, Searching, and Retrieval | |
| US7822736B2 (en) | Method and system for managing an index arrangement for a directory |
Legal Events
| Date | Code | Title | Description |
|---|---|---|---|
| AS | Assignment |
Owner name: INTERNATIONAL BUSINESS MACHINES CORPORATION, NEW Y Free format text: ASSIGNMENT OF ASSIGNORS INTEREST;ASSIGNORS:HOSHIKAWA, AKIKO;LEBOVITZ, ALLAN B.;SHIBAMIYA, AKIRA;REEL/FRAME:021830/0015 Effective date: 20051011 |
|
| STCB | Information on status: application discontinuation |
Free format text: EXPRESSLY ABANDONED -- DURING EXAMINATION |