Reducing the domain of a subquery by retrieving constraints from the outer query
10642832 ยท 2020-05-05
Assignee
Inventors
Cpc classification
H03M7/02
ELECTRICITY
International classification
Abstract
A database engine receives a human-readable database query that includes a subquery, and parses the database query to build an operator tree. The operator tree includes a subtree corresponding to the subquery. The database engine estimates the number of rows that will accessed when the subtree is executed and estimates the fraction of the cardinality of rows that will be filtered out by subsequent operations in the operator tree. In accordance with a determination that the estimated fraction exceeds a first threshold, the database engine inserts a domain constraint into the subtree that restricts rows retrieved by execution of the subtree, thereby forming a modified operator tree. The database engine executes the modified operator tree to form a final result set corresponding to the database query and returns the final result set.
Claims
1. A database engine, comprising: one or more computing devices, each having one or more processors and memory, wherein the memory stores one or more programs configured for execution by the one or more processors, the one or more programs comprising instructions for: receiving a human-readable database query that includes a subquery; parsing the database query to build an operator tree, which includes a subtree corresponding to the subquery; estimating a cardinality of rows in database tables specified in the subtree; estimating a fraction of the estimated cardinality of rows that do not satisfy a filter condition specified in one or more subsequent operations in the operator tree; in accordance with a determination that the estimated fraction exceeds a first threshold, inserting, into the subtree, a domain constraint that includes an early-probe operator that specifies comparing rows generated from execution of the subtree to a hash table of a second subtree in the operator tree, the domain constraint corresponding to the filter condition, thereby forming a modified operator tree in which execution of the subtree restricts rows retrieved according to the filter condition; executing the modified operator tree to form a final result set corresponding to the database query; and returning the final result set.
2. The database engine of claim 1, wherein inserting the domain constraint into the subtree is further in accordance with a determination that the estimated cardinality of rows exceeds a second threshold.
3. The database engine of claim 1, wherein estimating the cardinality of rows in database tables specified in the subtree comprises estimating a number of rows in an intermediate result set that will be created by execution of the subtree.
4. The database engine of claim 3, wherein estimating the fraction of the estimated cardinality of rows that do not satisfy the filter condition comprises determining a number of rows in the intermediate result set of the subtree that will not be used to form the final result set.
5. The database engine of claim 1, wherein the second subtree is more than one operator ahead of the subtree in the operator tree.
6. The database engine of claim 1, wherein estimating the fraction of the estimated cardinality of rows that do not satisfy the filter condition comprises: selecting a sample of rows from the database tables specified in the subtree; executing at least a portion of the operator tree, including the subtree and operators in the operator tree that specify the filter condition, using the selected sample of rows; and in accordance with the execution using the selected sample of rows, determining a number of the sample rows that are filtered out in the execution of the filter condition.
7. The database engine of claim 1, wherein executing the modified operator tree creates, for the subquery, an intermediate result set whose cardinality is substantially less than the estimated cardinality of rows in database tables specified in the subtree.
8. The database engine of claim 1, further comprising, in accordance with a determination that the estimated fraction does not exceed the first threshold, forgoing insertion of the domain constraint into the subtree.
9. The database engine of claim 1, further comprising, in accordance with a determination that the estimated cardinality does not exceed a second threshold, forgoing insertion of the domain constraint into the subtree.
10. The database engine of claim 1, wherein estimating the cardinality of rows in database tables specified in the subtree comprises: identifying a plurality of database tables specified in the subquery; and determining a respective number of rows in each of the plurality of database tables according to statistics stored at the database.
11. A method of retrieving data from a database, comprising: at a computer system having one or more computing devices, each computing device having one or more processors and memory storing one or more programs configured for execution by the one or more processors: receiving a human-readable database query that includes a subquery; parsing the database query to build an operator tree, which includes a subtree corresponding to the subquery; estimating a cardinality of rows in database tables specified in the subtree; estimating a fraction of the estimated cardinality of rows that do not satisfy a filter condition specified in one or more subsequent operations in the operator tree; in accordance with a determination that the estimated fraction exceeds a first threshold, inserting, into the subtree, a domain constraint that includes an early-probe operator that specifies comparing rows generated from execution of the subtree to a hash table of a second subtree in the operator tree, the domain constraint corresponding to the filter condition, thereby forming a modified operator tree in which execution of the subtree restricts rows retrieved according to the filter condition; executing the modified operator tree to form a final result set corresponding to the database query; and returning the final result set.
12. The method of claim 11, wherein inserting the domain constraint into the subtree is further in accordance with a determination that the estimated cardinality of rows exceeds a second threshold.
13. The method of claim 11, wherein estimating the cardinality of rows in database tables specified in the subtree comprises estimating a number of rows in an intermediate result set that will be created by execution of the subtree.
14. The method of claim 13, wherein estimating the fraction of the estimated cardinality of rows that do not satisfy the filter condition comprises determining a number of rows in the intermediate result set of the subtree that will not be used to form the final result set.
15. The method of claim 11, wherein estimating the fraction of the estimated cardinality of rows that do not satisfy the filter condition comprises: selecting a sample of rows from the database tables specified in the subtree; executing at least a portion of the operator tree, including the subtree and operators in the operator tree that specify the filter condition, using the selected sample of rows; and in accordance with the execution using the selected sample of rows, determining a number of the sample rows that are filtered out in the execution of the filter condition.
16. The method of claim 11, wherein executing the modified operator tree creates, for the subquery, an intermediate result set whose cardinality is substantially less than the estimated cardinality of rows in database tables specified in the subtree.
17. The method of claim 11, wherein estimating the cardinality of rows in database tables specified in the subtree comprises: identifying a plurality of database tables specified in the subquery; and determining a respective number of rows in each of the plurality of database tables according to statistics stored at the database.
18. A non-transitory computer readable storage medium storing one or more programs configured for execution by a computer system having one or more processors and memory, the one or more programs comprising instructions for: receiving a human-readable database query that includes a subquery; parsing the database query to build an operator tree, which includes a subtree corresponding to the subquery; estimating a cardinality of rows in database tables specified in the subtree; estimating a fraction of the estimated cardinality of rows that do not satisfy a filter condition specified in one or more subsequent operations in the operator tree; in accordance with a determination that the estimated fraction exceeds a first threshold, inserting, into the subtree, a domain constraint that includes an early-probe operator that specifies comparing rows generated from execution of the subtree to a hash table of a second subtree in the operator tree, the domain constraint corresponding to the filter condition, thereby forming a modified operator tree in which execution of the subtree restricts rows retrieved according to the filter condition; executing the modified operator tree to form a final result set corresponding to the database query; and returning the final result set.
Description
BRIEF DESCRIPTION OF THE DRAWINGS
(1) For a better understanding of the aforementioned systems and methods that provide efficient database query processing, reference should be made to the Description of Implementations below, in conjunction with the following drawings in which like reference numerals refer to corresponding parts throughout the figures.
(2)
(3)
(4)
(5)
(6)
(7)
(8)
(9)
(10)
(11) Reference will now be made to implementations, examples of which are illustrated in the accompanying drawings. In the following description, numerous specific details are set forth in order to provide a thorough understanding of the present invention. However, it will be apparent to one of ordinary skill in the art that the present invention may be practiced without requiring these specific details.
DESCRIPTION OF IMPLEMENTATIONS
(12)
(13) In some cases, the personal device 102 connects over one or more communications networks 108 to one or more external database servers 106 and/or a data visualization server 104. The communication networks 108 may include local area networks and/or wide area networks, such as the Internet. In some implementations, the data visualization server 104 provides a data visualization web application that runs within a web browser 220 on the personal device 102. In some implementations, data visualization functionality is provided by a local application 222 with certain functions provided by the data visualization server 104. For example, the data visualization server 104 may be used for resource intensive operations. In some implementations, the one or more database servers 106 include a database engine 120, which provides access to one or more databases 122 that are stored at the database server 106. As illustrated in
(14)
(15) Each of the above identified executable modules, applications, or sets of procedures may be stored in one or more of the previously mentioned memory devices, and corresponds to a set of instructions for performing a function described above. The above identified modules or programs (i.e., sets of instructions) need not be implemented as separate software programs, procedures, or modules, and thus various subsets of these modules may be combined or otherwise rearranged in various implementations. In some implementations, the memory 214 stores a subset of the modules and data structures identified above. Furthermore, in some implementations, the memory 214 stores additional modules or data structures not described above.
(16) Although
(17) In a query language such as SQL, subqueries can be correlated to outer queries that further process the results of the subqueries. If the subquery is processed independently of the outer query (which uses the intermediate results of the subquery), the evaluation of the subquery may generate a very large intermediate result set. In many cases, only a small subset of the intermediate result set is actually needed for the further processing.
(18) Disclosed implementations introduce domain constraints from the outer query into the subquery in order to constrain the processing, producing relevant intermediate results that limit the results that are not actually needed for processing the outer query. In some implementations, this process uses early probes of a hash join table built for the outer query, thereby imposing a filter constraint on the subquery. Some implementations use a Bloom filter that is created from the hash join table for the outer query. A Bloom filter can filter out many rows from the execution of the subtree that would be filtered out later anyway. Note that a Bloom filter may not filter out all of the rows that will be filtered later. However, in some implementations, filtering out a large portion of the debris while incurring a low overhead cost is an effective way to reduce the overall processing time. \\Poozer\97386
(19) Standard relational database query engines rely on relational algebra trees (e.g., an operator tree 228) for evaluating logically optimized plans. A typical algebra tree 228 has the nice property that its leaves correspond to base relations and each node in the tree 228 can be evaluated based solely on nodes of its subtree. To evaluate a node in the tree, a typical iterator engine works by pulling intermediate results from the subtrees corresponding to children of the node.
(20) Although the simplicity of the pull-based model is nice, it has some fundamental limitations. A subtree cannot know which of its intermediate results are really needed for further processing the query. Therefore, each subtree computes all of its subquery results and passes them to the parent node. This is illustrated in
(21)
(22) The intermediate results on the rightmost branch of the operator tree 228 are represented by individual dots, each representing a row in the respective intermediate results. (Of course, the intermediate results in an actual query may have thousands, millions, or billions of rows.) The shading of the dots here indicates which of the results are actually used in subsequent operations to form the final result set for the query. The unfilled dots, such as the dots 352 and 358 in the first intermediate result set 320, are the ones that are subsequently used in the final result set. The filled dots, such as the dots 354 and 356 in the first intermediate result set 320, represent rows that will not be used to create the final result set for the query. In this example, the intermediate result set 320 for the first subtree 302 includes a large percentage of rows that will not ultimately be used.
(23) The lower operation 312 (a join between the intermediate results 320 and 322) produces another set of intermediate results 324 with a large number of rows that will not ultimately be used (e.g., represented by the dots 364 and 366). The number of rows that will ultimately be used (e.g., represented by the dots 362 and 368) is a much smaller number.
(24) The upper operation 314 (another join), produces a result set 328 with a much smaller number of rows (e.g., the row represented by the dot 372). In the result set 328 all of the dots are unfilled, indicating that all of the rows will be used.
(25) As illustrated here, sometimes an intermediate result set early in the operator tree 228 has a very large number of rows that will not ultimately be used. Disclosed implementations identify this situation, and modify the operator tree to reduce the overhead. This can reduce processing time as well as memory usage. In some implementations, the situation illustrated here is identified by two conditions. First, the intermediate result set has to be large enough to justify the additional cost of modifying the execution plan. Second, a substantial percentage of the rows in the intermediate result set have to be ones that will be filtered out later. In some implementations, a substantial percentage is 75%, 90%, or 95%.
(26) As illustrated in
(27) In accordance with some implementations, the database engine 120 (e.g., the optimizer 244 within the database engine) opportunistically detects the situation just described. In particular, the optimizer 244 detects those cases where a subtree in the query plan 228 generates many useless data objects (i.e., those that are filtered out in later processing stages).
(28) From a different perspective, the optimizer 244 looks for beneficial filters in other parts of the query tree 228 that can be used to reduce the cardinality of a particular subtree. The optimizer 244 inspects other subtrees for filter predicates that would later on in the tree reduce the intermediate results of the current subtree. Instead of waiting, these filters are applied at an early stage.
(29)
(30)
(31) The example query uses five tables, as specified in the from clause, and each of the tables is given a local alias: part, supplier, partsupp, nation, and region. The select clause specifies the selected data fields from three of these tables. The fields acctbal, name, address, phone, and comment are selected from the supplier table; the name field is selected from the nation table; and the fields partkey and mfgr are selected from the part table.
(32) The where clause specifies the join conditions between the five tables, specifies three filter conditions, and specifies a correlated subquery to identify the minimum supplycost for each part. The joins are based on the primary keys of the part table, the partsupp table, the nation table, and the region table (i.e., partkey, suppkey, nationkey, and regionkey).
(33) The example query 226 filters the results to a small set. In particular, the part size is limited to 15 and the part type is limited to those whose type names end with BRASS (i.e., like % BRASS). The parts are also limited to those available in the EUROPE region.
(34) The final filter condition uses a correlated subquery that finds the lowest cost for each part (i.e., min(ps.supplycost)). Note that there can be multiple instances of suppliers who are providing the same part at the lowest price. Also note that the joins in the correlated subquery are the same as the joins in the outer query.
(35) The order by clause specifies how the results are returned. Here, the results are ordered first according to the acctbal of the supplier in descending order. The results are further ordered according to the name field in the supplier table, the name field in the nation table, and the partkey field in the part table. The results are limited to at most 100 rows.
(36)
(37) Between each pair of nodes,
(38) The lower right part of this operator tree adds significant inefficiency because 800,000 rows from the PARTSUPP table are processed by the hash join 416 and the aggregate operator F 418 before finally being limited to a small number of relevant rows. In particular, the hash join 416 of PARTSUPP with the subtree from (REGION, NATION, SUPPLIER) generates a huge intermediate result set (i.e., supplier information for all PARTs that the eCommerce company has on sale). However, the PARTs of interest are highly filtered to only about 1% of all PARTS in later stages of the evaluation plan. This subsequent filtering is unknown in the lower subtree because it is independently evaluated.
(39)
(40) The early probe operator 450 makes use of the hash table that is already built for the hash join 412. Before processing a PARTSUPP row, the early probe operator 450 inspects (452) this hash table to determine whether or not this row is needed later on in the query evaluation. If the corresponding key partkey is not found in the hash table, the row is discarded from further processing. Based on the early probe operator 450, the number of rows (430) in the intermediate result set is much smaller than the 800,000 rows (430) that are in the corresponding intermediate result set without the injected early probe. The reduced cardinality 430 of this intermediate result set also creates reduced cardinalities 434 and 436 for the next two intermediate result sets.
(41)
(42) When there is at least one subtree in the operator tree 228, the process identifies (508) one of the subtrees, and then estimates (510) the cardinality of the intermediate result set that will be created by execution of the subtree. In some implementations, estimating the cardinality is based on stored statistical data about the tables accessed in the subquery (e.g., how many rows are in each of the tables). In some implementations, estimating the cardinality is based on saved information of previously executed queries for the same or similar tables. In some implementations, estimating the cardinality includes computing the product of the cardinalities of the tables included in the subtree (e.g., assuming a Cartesian product). In some implementations, estimating the cardinality of the intermediate result set includes computing a sum of the cardinalities of the tables included in the subtree. In some implementations, estimating the cardinality of the intermediate result set includes computing a maximum of the cardinalities of the tables included in the subtree. In some implementations, estimating the cardinality uses machine learning based on stored historical data of the same or similar tables (e.g., using a trained neural network or support vector machine).
(43) The optimizer 244 then determines (512) whether the estimated cardinality exceeds a first threshold 246. When the estimated cardinality does not exceed the first threshold 246, the process 500 proceeds to execute (520) the operator tree 228 (e.g., the original operator tree). When the cardinality does exceed the first threshold, the process estimates (514) the fraction of the cardinality that will be filtered out by subsequent operations in the operator tree. The optimizer 244 determines (516) whether the fraction exceeds the second threshold 248. When the fraction of the cardinality that will be filtered out subsequently does not exceed the second threshold 248, the original operator tree is executed (520). When the fraction does exceed the second threshold 248, the optimizer inserts (518) a domain constraint into the operator tree 228. The inserted domain constraint filters out at least some of the superfluous rows that would have been in the intermediate result set for the subtree. This is illustrated above in
(44) When later operations in an operator tree will not use a substantial fraction of the results (e.g., the results are filtered out), the inserted domain constraint can reduce the overall cost of identifying the results responsive to the query. By executing the modified operator tree (e.g., with the domain constraint), the database engine may decrease processing time and decrease the resources required to return the result set.
(45) In some cases, an operator tree 228 has two or more subtrees. In general, each of the subtrees is evaluated as described by steps 508-518 in the process 500. In some cases, the analysis of each subtree occurs independently of the other subtrees, in which case the analysis of the subtrees can proceed at least partially in parallel. However, in some cases, the results of one subtree can have a direct effect on a subsequent subtree. For example, by inserting a domain constraint into subtree A 302 in
(46) In some implementations, the first threshold 246 and the second threshold 248 are stored as threshold parameters 530. In some implementations, the first threshold 246 and the second threshold 248 are applied independently as illustrated by the process 500. In other implementations, these thresholds are effectively combined into a single threshold and a computed function of the two estimates is compared to the combined threshold. For example, determine whether f(estimate 1, estimate 2)>combined threshold, where f is a function of the two estimates. Some implementations also evaluate the probable accuracy of the estimates, and only insert a domain constraint when the probability is sufficiently high to justify the overhead.
(47) The final steps in
(48)
(49) The alternative process 550 identifies (552) a filter condition in the operator tree that executes after the identified subtree. For example, in
(50) The selectivity of the filter condition with respect to the subtree measures the fraction of rows from the subtree that survive applying the filter condition. Selectivity values range from 0 (no rows survive the filter condition) to 1 (all rows survive the filter condition). A highly selective filter condition is one with a value closer to 0 (e.g., 0.05, 0.01, or lower).
(51) The process 550 estimates (554) the selectivity of applying the filter condition to the subtree. In some implementations, the process 550 uses sampling to estimate the selectivity. For example, some implementations identify a small random (or pseudo-random) sample of rows from the tables specified in the subtree. The operators in the operator tree are applied to the sample to determine how many of the rows are retained instead of being filtered out.
(52) The process then determines (556) whether the selectivity is less than a predefined threshold (e.g., a first threshold 246). If so, the process 550 inserts (558) a domain constraint into the subtree that applies the filter condition. In this alternative process 550, only one threshold is used, and it is compared against the selectivity.
(53) As illustrated by
(54)
(55) The database engine 120 receives (606) a human-readable database query 226, which includes a subquery. The database engine 120 (or the query parser 228 within the database engine) parses (608) the database query 226 to form an operator tree 228. The operator tree 228 includes a subtree that corresponds to the subquery. In some instances, the database 226 query includes a plurality of subqueries and the operator tree 228 includes a plurality of subtrees, each corresponding to a respective one of the subqueries.
(56) The logical optimizer 244 estimates (610) the cardinality of rows in database tables specified in the subtree. In some implementations, the optimizer 244 estimates (612) the number of rows in the intermediate result set that will be created by execution of the subtree. In some implementations, the optimizer 244 identifies (614) a plurality of database tables specified in the subquery. The optimizer 244 then determines the respective number of rows in each of the plurality of database tables according to statistics stored at the database.
(57) The logical optimizer 244 also estimates (616) the fraction of the estimated cardinality of rows that do not satisfy a filter condition specified in one or more subsequent operations in the operator tree. This represents what percentage of rows created by the subtree are superfluous (e.g., illustrated by the filled dots in
(58) In some implementations, the fraction is estimated using a sampling of data. For example, some implementations select (620) a sample of rows from the database tables specified in the subtree, and execute (622) at least a portion of the operator tree, including both the subtree and operators in the operator tree that specify the filter condition. By executing the portion of the operator tree using the selected sample of rows, the process 600 determines (624) the number of the sample rows that are filtered out in the execution of the filter condition. In some implementations, the executed portion is simplified so that only relevant operators (e.g., the filtering operators) are executed.
(59) When the estimated fraction exceeds (626) the first threshold, the logical optimizer 244 inserts (626) a domain constraint into the subtree. The domain constraint corresponds (626) to the filter condition. This forms (626) a modified execution tree in which execution of the subtree restricts rows retrieved according to the filter condition. In some implementations, whether to insert a domain constraint is based on just the estimated fraction (or the estimated selectivity). In some implementations, whether to insert a domain constraint is further based on (628) determining that the estimated cardinality of rows from the subtree exceeds a second threshold. Some implementations combine these two estimated values (and/or other estimated values), and compare the combined value to a predetermined threshold to determine whether to insert a domain constraint.
(60) In some implementations, the inserted domain constraint uses an early-probe operator that specifies (630) comparing rows generated from execution of the subtree to a hash table of a second subtree in the operator tree. This is illustrated by the early probe operator 450 in
(61) An early-probe operator is one technique used to implement domain constraints in the subtree. In many cases this is efficient because the hash table for a subsequent operation has already been created. Some implementations impose domain constraints in alternative ways, such as Bloom filters. In some implementations, the process builds a Bloom filter during construction of the hash table, and the Bloom filter is used later rather than probing the hash table. Some implementations identify the operators that form the subsequent filter and recreate those operators locally as a domain constraint within the subtree.
(62) In some implementations, when the estimated cardinality does not exceed the second threshold 248, the optimizer 244 forgoes (634) insertion of the domain constraint into the subtree. In some implementations, when the estimated fraction does not exceed the first threshold 246, the optimizer 244 forgoes (636) insertion of the domain constraint into the subtree. Thus, whether one or both of the first threshold 246 or the second threshold 248 is not satisfied, the optimizer 244 does not modify the operator tree. In some implementations, the database engine 120 executes the original (e.g., unmodified) operator tree as created in step 608.
(63) In some implementations, the database engine 120 executes (638) the modified operator tree (e.g., using query execution module 260) to form a final result set corresponding to the database query 226. In some implementations, executing the modified operator tree creates (640) an intermediate result set for the subquery whose cardinality is substantially less than the estimated cardinality of rows in database tables specified in the subtree. In some implementations, substantially less is 1%, 5%, 10%, or 25%. The database engine 120 returns (642) the final result set.
(64)
(65) Early probe filtering introduces its own overhead cost, but usually pays off well by reducing intermediate result set cardinalities. The thresholds 246 and 248 described above insure that domain constraints are only inserted when there is sufficient return on the investment.
(66) The terminology used in the description of the invention herein is for the purpose of describing particular implementations only and is not intended to be limiting of the invention. As used in the description of the invention and the appended claims, the singular forms a, an, and the are intended to include the plural forms as well, unless the context clearly indicates otherwise. It will also be understood that the term and/or as used herein refers to and encompasses any and all possible combinations of one or more of the associated listed items. It will be further understood that the terms comprises and/or comprising, when used in this specification, specify the presence of stated features, steps, operations, elements, and/or components, but do not preclude the presence or addition of one or more other features, steps, operations, elements, components, and/or groups thereof.
(67) The foregoing description, for purpose of explanation, has been described with reference to specific implementations. However, the illustrative discussions above are not intended to be exhaustive or to limit the invention to the precise forms disclosed. Many modifications and variations are possible in view of the above teachings. The implementations were chosen and described in order to best explain the principles of the invention and its practical applications, to thereby enable others skilled in the art to best utilize the invention and various implementations with various modifications as are suited to the particular use contemplated.