Patent ID: 7756861

Claim:
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 performs the steps comprising: determining that the query is optimizable; generating 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; determining that the query contains an Order By clause, wherein the Order By clause contains a reasonable predicate that is determined using a previous query history, wherein the reasonable predicate specifies a size that guarantees a sufficiently large enough subset of data is returned by a sort; and eliminating records prior to the sort by creating an access plan that fetches fewer rows by adding the reasonable predicate to every AND group of the Where clause.