IP Library Granted Patent US 7,567,952
Granted Patent B2
US 7,567,952 · App. 11/941,772 · Granted Jul 28, 2009

Optimizing a computer database query that fetches n rows

View Patent ↗
Loading inventors, assignments & file history…
Monitor This Case
Get email alerts when status or documents change.
Order Certified Copies
Most orders are placed with the USPTO same day — all within 24 business hours.
Order via The Patent Place →
Pre-filled with this patent's details
Quick Facts
Patent No.
US 7,567,952
App. No.
11/941,772
Granted
Jul 28, 2009
Kind
B2
Abstract

An apparatus and method to optimize a query access plan in a computer database system. In preferred embodiments, the query optimizer determines the query can be optimized and generates for the query an optimized access plan that eliminates records defined by a Where clause and one or more ordering criteria prior to ordering the records (sorting or grouping) and then returning a first n rows.

Claims (38)

1. An apparatus comprising:

at least one processor;

a memory coupled to the at least one processor containing a database of records;

a query residing in the memory that includes a Select statement with a Fetch First n Rows Only clause, where n is an integer variable;

a query optimizer residing in the memory and executed by the at least one processor, wherein the query optimizer determines the query can be optimized and generates for the query an optimized access plan that eliminates records defined by a Where clause prior to ordering the records and then return a first n rows; and wherein the query optimizer further determines the query contains an Order By clause, that an index exists for each predicate in the Where clause, and the field of the Order By clause exists in each index; and

the query optimizer generates an access plan that eliminates records prior to a sort by fetching only n rows from each index and then returning n rows after sorting a set of records that includes the n rows from each index.

2. The apparatus of claim 1 wherein the Fetch First n Rows Only clause is implied and the query is an interactive query.

3. The apparatus of claim 1 wherein the query optimizer further determines the query contains an Order By clause, and that there is a determinable value of a field of the Order By clause; and

the query optimizer eliminates records prior to a sort by creating an access plan that fetches fewer rows by adding the field with the determinable value to each predicate of the Where clause.

4. The apparatus of claim 1 wherein the query optimizer further determines the query contains an Order By clause, that an index exists for the leftmost column but not all the columns; and

the query optimizer generates an access plan that eliminates records prior to a sort by fetching n rows from the index over the leftmost column and additional rows until a unique value of the index is encountered.

5. An apparatus comprising:

at least one processor;

a memory coupled to the at least one processor containing a database of records;

a query residing in the memory that includes a Select statement with a Fetch First n Rows Only clause, where n is an integer variable; and

a query optimizer residing in the memory and executed by the at least one processor, wherein the query optimizer determines the query can be optimized and generates for the query an optimized access plan that eliminates records defined by a Where clause prior to grouping the records and then return a first n rows;

wherein the query optimizer further determines the query contains a Group By clause, that an index exists for each predicate in the Where clause, and the field of the Group By clause exists in each index; and

the query optimizer generates an access plan that eliminates records prior to grouping by fetching only n rows from each index and then returning the n rows after grouping the n rows from each index.

6. The apparatus of claim 5 wherein the query optimizer further determines the query contains a Group By clause, and that there is a determinable value of a field of the Group by Clause; and

the query optimizer eliminates records prior to grouping by creating an access plan that fetches fewer rows by adding the field with the determinable value to each predicate of the Where clause.

7. The apparatus of claim 5 wherein the query optimizer further determines the query contains a Group By clause, that an index exists for the leftmost column but not all the columns; and

the query optimizer generates an access plan that eliminates records prior to grouping by fetching n rows from the index over the leftmost column and additional rows until a unique value of the index is encountered.

8. A program product comprising:

(A) a query optimizer that analyzes and optimizes a query by performing the following steps:

analyzing the query to determine if the query can be optimized that includes a Select statement with a Fetch First n Rows Only Clause and the query contains an Order By clause, that an index exists for each predicate in the Where clause, and the field of the Order By clause exists in each index, where n is an integer variable;

generating for the query an optimized access plan that eliminates records defined by a Where clause prior to a sort by fetching only n rows from each index and then returning the n rows after sorting a set of records that includes the n rows from each index; and

(B) computer-recordable media bearing the query optimizer.

9. The program product of claim 8 wherein the Fetch First n Rows Only clause is implied and the query is an interactive query.

10. The program product of claim 8 wherein the query optimizer further determines the query contains an Order By clause, and that there is a determinable value of a field of the Order by Clause; and

the query optimizer eliminates records prior to a sort by creating an access plan that fetches fewer rows by adding the field with the determinable value to each predicate of the Where clause.

11. The program product of claim 8 wherein the query optimizer further determines the query contains an Order By clause, that an index exists for the leftmost column but not all the columns; and

the query optimizer generates an access plan that eliminates records prior to a sort by fetching n rows from the index over the leftmost column and additional rows until a unique value of the index is encountered.

12. The program product of claim 8 wherein the query optimizer further determines the query contains a Group By clause, that an index exists for each predicate in the Where clause, and the field of the Group By clause exists in each index; and

the query optimizer generates an access plan that eliminates records prior to grouping by fetching only n rows from each index and then returning n rows after grouping the n rows from each index.

13. The program product of claim 8 wherein the query optimizer further determines the query contains a Group By clause, and that there is a determinable value of a field of the Group by Clause; and

the query optimizer eliminates records prior to grouping by creating an access plan that fetches fewer rows by adding the field with the determinable value to each predicate of the Where clause.

14. The program product of claim 8 wherein the query optimizer further determines the query contains a Group By clause, that an index exists for the leftmost column but not all the columns; and

the query optimizer generates an access plan that eliminates records prior to grouping by fetching n rows from the index over the leftmost column and additional rows until a unique value of the index is encountered.

Assignments (3)
ASSIGNMENT OF ASSIGNOR'S INTEREST Recorded Aug 24, 2016
From: MURAS, BRIAN ROBERT; NELSON, ROBERT RUSSELL; SANTOSUOSSO, JOHN MATTHEW
To: INTERNATIONAL BUSINESS MACHINES CORPORATION
Reel/Frame 039525/0522 →
ASSIGNMENT OF ASSIGNOR'S INTEREST Recorded Apr 1, 2016
From: MIDWAY TECHNOLOGY COMPANY LLC
To: SERVICENOW, INC.
Reel/Frame 038324/0816 →
ASSIGNMENT OF ASSIGNOR'S INTEREST Recorded Feb 5, 2016
From: INTERNATIONAL BUSINESS MACHINES CORPORATION
To: MIDWAY TECHNOLOGY COMPANY LLC
Reel/Frame 037704/0257 →