Optimizing database queries using reusable subqueries
First Claim
1. A computer implemented method for optimizing database queries, the method comprising:
- receiving, by a database system, a database query for optimization, the database query comprising a first subquery and a second subquery, the first subquery specifying a first where clause comprising a first condition and the second subquery specifying a second where clause comprising a second condition, wherein the first where clause is distinct from the second where clause and wherein the first condition evaluates to true for a first set of input rows and the second condition evaluates to true for a second set of input rows;
comparing the first subquery and the second subquery based on input tables processed by each of the first and second subqueries;
responsive to determining that the first subquery and the second subquery match based on the comparison;
generating a first database query specifying a where clause comprising a condition that evaluates to true for a superset of the first set of input rows and the second set of input rows;
generating a statement comprising the first database query, the statement storing result of execution of the first database query in a result table;
generating a first expression equivalent to the first subquery and a second expression equivalent to the second subquery, the first and the second expressions based on the result table;
modifying the database query to use the result table, the modifying comprising, replacing the first subquery with the first expression and the second subquery with the second expression; and
replacing an execution of the database query with an execution of the statement followed by an execution of the modified database query.
6 Assignments
0 Petitions
Accused Products
Abstract
A database system optimizes database queries that comprise subqueries. The database system factors out computation from two or more subqueries to a reusable subquery. The reusable subquery returns a result set. The subqueries are rewritten to use the result set and the database query modified to use the rewritten subqueries. The modified database query performs efficiently since the result set is computed only once for each subquery that uses the reusable query. The database system also composes nested subqueries that compute aggregate values. The database system identifies an inner subquery nested within an outer subquery, each subquery computing an aggregate value. The database system determines a new aggregate function corresponding to a composition of aggregate functions of the inner and the outer queries. The database system determines a composed query using the inner query with the outer query. The composed query uses the new aggregate function.
29 Citations
20 Claims
-
1. A computer implemented method for optimizing database queries, the method comprising:
-
receiving, by a database system, a database query for optimization, the database query comprising a first subquery and a second subquery, the first subquery specifying a first where clause comprising a first condition and the second subquery specifying a second where clause comprising a second condition, wherein the first where clause is distinct from the second where clause and wherein the first condition evaluates to true for a first set of input rows and the second condition evaluates to true for a second set of input rows; comparing the first subquery and the second subquery based on input tables processed by each of the first and second subqueries; responsive to determining that the first subquery and the second subquery match based on the comparison; generating a first database query specifying a where clause comprising a condition that evaluates to true for a superset of the first set of input rows and the second set of input rows; generating a statement comprising the first database query, the statement storing result of execution of the first database query in a result table; generating a first expression equivalent to the first subquery and a second expression equivalent to the second subquery, the first and the second expressions based on the result table; modifying the database query to use the result table, the modifying comprising, replacing the first subquery with the first expression and the second subquery with the second expression; and replacing an execution of the database query with an execution of the statement followed by an execution of the modified database query. - View Dependent Claims (2, 3, 4, 5, 6, 7, 8, 9)
-
-
10. A non-transitory computer-readable storage medium storing computer-executable code for optimizing queries of a database system, the code, when executed by a processor, causing the processor to:
-
receive a database query for optimization, the database query comprising a first subquery and a second subquery, the first subquery specifying a first where clause comprising a first condition and the second subquery specifying a second where clause comprising a second condition, wherein the first where clause is distinct from the second where clause and wherein the first condition evaluates to true for a first set of input rows and the second condition evaluates to true for a second set of input rows; compare the first subquery and the second subquery based on input tables processed by each of the first and second subqueries; responsive to determining that the first subquery and the second subquery match based on the comparison; generate a first database query specifying a where clause comprising a condition that evaluates to true for a superset of the first set of input rows and the second set of input rows; generate a statement comprising the first database query, the statement storing result of execution of the first database query in a result table; generate a first expression equivalent to the first subquery and a second expression equivalent to the second subquery, the first and the second expressions based on the result table; modify the database query to use the result table, the modifying comprising, replacing the first subquery with the first expression and the second subquery with the second expression; and replace an execution of the database query with an execution of the statement followed by an execution of the modified database query. - View Dependent Claims (11, 12, 16, 17, 18, 19, 20)
-
-
13. A computer-implemented system for optimizing queries, the system comprising:
-
a computer processor; and a non-transitory computer-readable storage medium storing computer program modules configured to execute on the computer processor, the computer program modules comprising; an optimizer module configured to; receive a database query for optimization, the database query comprising a first subquery and a second subquery, the first subquery specifying a first where clause comprising a first condition and the second subquery specifying a second where clause comprising a second condition, wherein the first where clause is distinct from the second where clause and wherein the first condition evaluates to true for a first set of input rows and the second condition evaluates to true for a second set of input rows; compare the first subquery and the second subquery based on input tables processed by each of the first and second subqueries; responsive to determining that the first subquery and the second subquery match based on the comparison; generate a first database query specifying a where clause comprising a condition that evaluates to true for a superset of the first set of input rows and the second set of input rows; generate a statement comprising the first database query, the statement storing result of execution of the first database query in a result table; generate a first expression equivalent to the first subquery and a second expression equivalent to the second subquery, the first and the second expressions based on the result table; modify the database query to use the result table, the modifying comprising, replacing the first subquery with the first expression and the second subquery with the second expression; and replace an execution of the database query with an execution of the statement followed by an execution of the modified database query. - View Dependent Claims (14, 15)
-
Specification