Exact Selectivity Computation for Modern In-Memory Database Query Optimization

01/06/2019 ∙ by Jun Hyung Shin, et al. ∙ University of California, Merced MapD Technologies, Inc. 0

Selectivity estimation remains a critical task in query optimization even after decades of research and industrial development. Optimizers rely on accurate selectivities when generating execution plans. They maintain a large range of statistical synopses for efficiently estimating selectivities. Nonetheless, small errors -- propagated exponentially -- can lead to severely sub-optimal plans---especially, for complex predicates. Database systems for modern computing architectures rely on extensive in-memory processing supported by massive multithread parallelism and vectorized instructions. However, they maintain the same synopses approach to query optimization as traditional disk-based databases. We introduce a novel query optimization paradigm for in-memory and GPU-accelerated databases based on exact selectivity computation (ESC). The central idea in ESC is to compute selectivities exactly through queries during query optimization. In order to make the process efficient, we propose several optimizations targeting the selection and materialization of tables and predicates to which ESC is applied. We implement ESC in the MapD open-source database system. Experiments on the TPC-H and SSB benchmarks show that ESC records constant and less than 30 milliseconds overhead when running on GPU and generates improved query execution plans that are as much as 32X faster.

READ FULL TEXT VIEW PDF
POST COMMENT

Comments

There are no comments yet.

Authors

page 1

page 2

page 3

page 4

This week in AI

Get the week's most popular data science and artificial intelligence research sent straight to your inbox every Saturday.

1 Introduction

Consider the following SQL query:


  • SELECT R.A, S.B, R.C, R.D
    FROM R, S, T
    WHERE R.A = S.A AND S.B = T.B AND
         R.B = x AND R.C BETWEEN (y1, y2) AND
         (R.D = z1 OR R.D > z2) AND udf(R.B,R.D) > w

The tuples from table R that participate in the join are selected by a complex predicate , over three attributes with exact, range, and OR conditions, and a user-defined function udf. When computing the optimal execution plan, i.e., join ordering, the query optimizer has to estimate the selectivity of . When available, this is done with precomputed synopses [3], e.g., histograms, samples, sketches, stored in the metadata catalog (Figure 0(a)). Otherwise, an arbitrary guess is used, e.g., for udf

. Synopses are typically built for a single attribute and assume uniformity and/or independence when they are combined across multiple attributes. These are likely to miss correlations between attributes over skewed or sparse data and result in inaccurate estimates which produce highly sub-optimal query execution plans 

[7]. We argue that – despite the large volume of work on the topic [2, 10, 14] – selectivity estimation is still an open problem [8].

(a) Synopses-driven selectivity estimation
(b) Exact selectivity computation (ESC)
Figure 1: Query optimization strategies.

Database systems for modern computing architectures rely on extensive in-memory processing supported by massive multithread parallelism and vectorized instructions. GPUs represent the pinnacle of such architectures, harboring thousands of SMT threads which execute tens of vectorized SIMD instructions simultaneously. MapD111https://www.mapd.com/, Ocelot222https://bitbucket.org/msaecker/monetdb-opencl, and CoGaDB333http://cogadb.dfki.de/ are a few examples of modern in-memory databases with GPU support. They provide relational algebra operators and pipelines for GPU architectures [5, 1, 4] that optimize memory access and bandwidth. However, they maintain the same synopses approach to query optimization as traditional disk-based databases.

We introduce a novel query optimization paradigm for in-memory and GPU-accelerated databases based on exact selectivity computation (ESC). The central idea in ESC is to compute selectivities exactly through queries during query optimization. As illustrated in Figure 0(b), our approach interacts with the query execution engine, while the synopses-driven solution only accesses the system catalog. For the given query example, the optimizer instructs the execution engine to first perform the selectivity sub-query:


  • SELECT R.A, R.C, R.D
    FROM R
    WHERE R.B = x AND R.C BETWEEN (y1, y2) AND
         (R.D = z1 OR R.D > z2) AND udf(R.B,R.D) > w

in order to compute the cardinality of exactly. This value is used together with the cardinalities of S and T to compute the best join order in the optimal query plan. Moreover, the result of the selectivity sub-query is temporarily materialized and reused instead of R in the optimal execution plan. This avoids re-executing the selection during the complete query execution. In order to make the process efficient, we propose optimizations targeting the selection of tables and predicates to which ESC is applied.

While it is clear that the plan computed by ESC is better – or at least as good – the impact on query execution time depends on the ratio between the execution time for the selectivity sub-query and the original plan. The assumption we make is that the sub-query execution is relatively negligible—valid for in-memory databases. We have to consider two cases. First, if the new query plan is improved by a larger margin than the sub-query time, the total execution time is reduced. We argue that exact selectivities are likely to achieve this for queries over many tables and with complex predicates. In the second case, the optimal query plan, i.e., join order, does not change even when selectivities are exact. Materialization minimizes the overhead incurred by the sub-query through subsequent reuse further up in the plan. In-memory databases prefer materialization over pipelining [11, 4] due to better cache access.

Our contributions are summarized as follows:

  • We introduce ESC as a novel paradigm to query optimization that is applicable independent of the predicate complexity and data distribution. ESC does not rely on any database synopses. It can be applied to any modern in-memory and GPU-accelerated database.

  • We implement ESC in the open-source MapD database with minimal changes to the query optimizer. The code is adopted by the MapD core engine.

  • We perform extensive experiments over the TPC-H and Star-Schema (SSB) benchmarks. The results show that ESC records constant and less than 30 milliseconds overhead when running on GPU and generates improved query execution plans that are as much as 32X faster for queries with complex predicates.

2 Exact Selectivity Computation

Figure 2 depicts the exact selectivity computation workflow corresponding to the example query given in the introduction. Once the query reaches the query compiler, it is first parsed into an abstract syntax tree representation. The compiler searches this representation for selection predicates that can be pushed-down to reduce the cost of subsequent join operations. In the example query, the only selection predicate is on table R. The query optimizer has to determine whether the identified selection(s) should be pushed-down or not based on their cardinality. We assume a materialized query execution strategy since predicates are always pushed-down with pipelining. At this point, the cardinality of has to be estimated to determine the optimal plan. However, this is rather problematic in the case of the complex conditions in and errors may lead to a sub-optimal plan. This is because all the existing selectivity estimation methods either make simplifying assumptions about the distribution of an attribute and the correlations across attributes [2], or – when such assumptions are not made – they are applicable to a limited class of (conjunctive) predicates [14].

Figure 2: Exact selectivity computation (ESC) workflow.

The idea in ESC is to focus entirely on the accuracy of the predicate cardinality. Thus, we compute this value exactly with a simple aggregate query:


  • SELECT COUNT(*)
    FROM R
    WHERE R.B = x AND R.C BETWEEN (y1, y2) AND
         (R.D = z1 OR R.D > z2) AND udf(R.B,R.D) > w

There are several steps to generate the exact selectivity computation query from the original relational algebra tree (RA). First, the compiler pushes-down an additional projection with the attributes required by subsequent operators in the plan—in this case, A, C, and D. Although this is not necessary for the selectivity computation itself, it reduces data access in column-wise in-memory databases. If no column is included in the projection, the execution engine accesses unnecessary columns, e.g., B, which incurs overhead. Hence, the compiler has to find all the columns required for further processing and push them all down. At this point, we have a simple SELECT-FROM-WHERE sub-tree corresponding to the selectivity sub-query. To compute its cardinality, the compiler adds a temporary aggregate COUNT operator, as shown at the bottom-left of Figure 2. The generated tree is then passed to the query engine for execution. Upon completion, the exact cardinality is returned to the query optimizer instead of an estimate computed from synopses stored in the catalog. The optimizer uses the computed cardinality to determine whether pushing-down the selection is effective or not. Since intermediate results are materialized, this depends on the selectivity of . If the selection is pushed-down, the result of the selectivity sub-query is materialized at optimization. Otherwise, it is discarded entirely. The same procedure is applied to each table having selection predicates. After all the selectivities are computed, the query optimizer proceeds to computing join ordering in the optimal plan.

Figure 3: Materialization of during optimization (left) vs. execution (right).

2.1 Selection Materialization

Exact selectivity computation with selection push-down introduces two problems for materialized execution in memory databases: When to materialize? and Where to materialize? The materialization incurred by selection push-down is useful only when a significant number of tuples are pruned-out. Otherwise, the function call overhead coupled with the additional memory read/write access do not provide a significant reduction over the non-optimized plan. Moreover, materialization makes sense only for large tables which have significant margin for cardinality reduction. Our initial solution allows the user to specify two parameters that control materialization: the minimum table size and the maximum selectivity threshold. ESC is applied only to tables having more tuples than the minimum size parameter. If this condition is satisfied, materialization is performed only when the predicate selectivity is below the maximum threshold. Setting the value of these parameters has an important impact on performance. Currently, this is done by the DBA, however, in the future, we plan to devise adaptive algorithms that determine the optimal values automatically.

If a selection is pushed-down, it eventually generates an intermediate result that is materialized at some point during execution. In ESC, as shown in Figure 3, materialization is performed by the query optimizer instead of the execution engine. This facilitates the immediate reuse of the selectivity sub-query output which is already cached in (GPU) memory. If we delay materialization to execution, we cannot guarantee that the sub-query output is still cached, thus additional memory access being necessary. The result set is materialized as a temporary table.

(a) Left-deep plan
(b) ESC on the probe input
(c) ESC on the build input
Figure 4: Interaction between ESC and join ordering.

2.2 Join Ordering

Based on the computed exact selectivities, the optimizer finds an optimal join order that minimizes the intermediate join output sizes. We start from a typical left-deep tree (Figure 3(a)) in which all the joins are implemented with hashing. Hash tables are built for all the relations except the largest, which is used for probing the built hash tables. The reason we probe on the largest relation is limited memory capacity—only a few GB on GPU. Even if there is sufficient memory, building a hash table for the largest relation is more expensive. First, we join the smallest relation that shares a join predicate with the probed input in order to avoid expensive Cartesian products, and so on. All selection predicates are evaluated during probing since no selection is pushed down. Code generation techniques are applied to combine all the selections and probings in a single operator that does not materialize any intermediate results during execution.

We consider the effect of materialization from each exact selectivity computation and decide whether to push-down the selection or not. A selection push-down clearly reduces the number of tuples to be joined, however, it also adds materialization which incurs extra read/write access and can result in performance degradation overall. We have two cases to consider—selection push-down on the probing relation and on the building relation, respectively. In the former case, as depicted in Figure 3(b), selection push-down is not beneficial because it splits probing into two separate condition evaluations with a materialization in-between. This impacts negatively the memory access. Therefore, we do not perform selection push-down on the (largest) probing relation. In the latter case, as depicted in Figure 3(c), selections are pushed-down since materialization reduces their size and this can result in an improved join ordering. This happens only for selective predicates, though. Selections with high selectivity, however, are discarded when the cost to process the materialization is more expensive than the benefit of selection push-down. The optimal execution plan pushes-down only those selections that overcome the materialization overhead.

2.3 Advantages and Limitations

The ESC strategy has several strengths over the synopses-driven approach:

  • Exact: Needless to say, the core of ESC stems from computing the exact selectivity by running an additional aggregate query on demand. ESC is no longer a cardinality estimation method; it is an exact operation. Thus, its accuracy is no longer affected by the number of attributes or their distribution. Moreover, the computed selectivity is also consistent no matter how frequent the database is updated—no maintenance is required.

  • Comprehensible: ESC is rather simple since it does not require any complicate synopses or statistical comprehension for estimation. All we need is building an aggregate query and its execution to retrieve the exact selectivity.

  • Easy to integrate: From the perspective of an actual implementation, ESC requires only a handful of modifications to the query optimizer in order to compute the exact selectivity. The execution engine is kept unchanged.

Nevertheless, the time to execute a query to retrieve the exact selectivity is larger than synopses-driven estimation. If the total overhead is larger than the improvement gained from a better query execution plan, ESC does not provide any benefit. Given that the overhead is mostly due to I/O access, ESC is highly-dependent on the underlying database system architecture. For example, ESC does not work well on a disk-based database. Modern in-memory and GPU-accelerated databases, however, have a much lower overhead for executing simple aggregate queries.

2.4 Related Work

Synopses-driven selectivity estimation. A large variety of synopses have been proposed for selectivity estimation. Many of them are also used in approximate query processing [3]. Histograms are the most frequently implemented in practice. While they provide accurate estimation for a single attribute, they cannot handle correlations between attributes as the dimensionality increases [12]. The same holds for sketches [13]. Samples are sensitive to skewed and sparse data distributions [15]. Although graphical models [14] avoid all these problems, they are limited to conjunctive queries and perform best for equality predicates. Since ESC is exact, it achieves both accuracy and generality.

Query re-optimization. The LEO optimizer [9] monitors relational operator selectivities during query execution. These exact values are continuously compared with the synopses-based estimates used during optimization. When the two have a large deviation, synopses are adjusted to automatically correct their error for future queries. Mid-query re-optimization [6] takes immediate action and updates the execution plan for the running query. This, however, requires dropping already computed partial results which introduces considerable overhead. ESC computes the exact cardinalities at optimization and applies them to the current query which has the best plan from the beginning.

3 Experimental Evaluation

The purpose of the experimental evaluation is to quantify the impact ESC has on query execution performance in modern in-memory and GPU-accelerated databases. Specifically, we measure the time overhead incurred by the selectivity sub-query and the overall query execution time with and without ESC. To this end, we implement ESC in the query optimizer of open-source MapD [11]—a highly-parallel database engine with extensive GPU support. We name our implementation MapD+ESC. MapD has a materialized query execution strategy which does not perform selection push-down in order to minimize memory access. Moreover, query compilation is applied to fuse non-blocking operators together. The MapD optimizer does not perform any selectivity estimation. Thus, the ESC extension is entire overhead. ESC does not modify the join cardinality estimation which is standard in MapD.

We perform the experiments on a dual-socket server with two 14-core Intel Xeon E5-2660 v4 CPUs (56 threads overall, 256 GB memory) and an NVIDIA Tesla K80 GPU with 2496 cores, a 32-wide SIMD unit, and 24 GB memory. The server runs Ubuntu 16.04 SMP with CUDA 9.1. All the experiments are performed both on CPU and GPU. Switching between the two implementations is done with a command-line parameter in MapD. The results are collected by running each query at least 5 times and reporting the median value. Caches are always hot. Time is measured with the MapD built-in function.

(a) GPU
(b) CPU
Figure 5: Execution time (seconds) for the TPC-H (scale 50) queries used in [14].

3.1 ESC Overhead

In order to determine if ESC is feasible in practice, we measure the overhead introduced by exact selectivity computation as a function of the data size, the selectivity of the predicate, and the number of attributes in the selection. We report the execution time of the selectivity sub-query(ies). This means that MapD+ESC executes an additional sub-query for each table having selection predicates. We deactivate push-down materialization to guarantee that MapD+ESC uses exactly the same execution plan as MapD. This allows us to isolate the overhead incurred by ESC. All the experiments in this section are performed on TPC-H data at different scales.

Overhead by scale factor. We execute the following query template:


  • SELECT COUNT(*)
    FROM lineitem, [orders O] [part P] [supplier S]
    [WHERE l_orderkey=o_orderkey AND o_orderkey=x]
    [WHERE l_partkey=p_partkey AND p_partkey=y]
    [WHERE l_suppkey=s_suppkey AND s_suppkey=z]

which joins lineitem with one of the orders, part, or supplier tables. An equality selection predicate with a constant x, y, or z is applied to the variable table. We run these queries at scale factors 1, 10, and 50. Thus, MapD+ESC performs an additional selectivity sub-query on tables having an increasing number of tuples. Table 1 contains the results. We observe that the overhead incurred by MapD+ESC is at most 70 ms. On GPU, the overhead is always below 30 ms. This is a very small price for getting the exact selectivity. When the data size increases, there is almost no difference on the GPU, while on the CPU there is only a factor of 3.5 increase—compared to 50 on data size. This shows that the GPU throughput is much larger due to its considerably higher parallelism. The reason is the different memory hierarchy across the two architectures—the cache overhead increases with data size on CPU.

scale 1 10 50
table O P S O P S O P S
GPU 21 22 22 20 23 28 21 21 19
CPU 21 24 21 49 25 21 70 66 57
Table 1: Overhead by scale factor (ms).

Overhead by selectivity. We run the following query:


  • SELECT COUNT(*)
    FROM lineitem, orders
    WHERE l_orderkey=o_orderkey AND o_orderkey<u

in order to measure the impact of predicate selectivity on overhead. u is a parameter that controls the number of tuples from orders participating in the join. u takes values such that the selectivity varies from 0.001% to 100%. These correspond to a range from 7.5K to 7.5M on TPC-H scale 50. The results are included in Table 2. As expected, we observe that the overhead increases with selectivity. However, the increase is almost negligible. This is because the selectivity sub-query is not impacted by the predicate selectivity—it is a simple aggregate. Selectivity changes only the number of tuples used to build the hash table on orders. Since MapD+ESC preserves the same plan, the overhead is due entirely to the additional sub-query—similar to Table 1.

selectivity (%) 0.001 0.01 0.1 1 10 100
GPU 18 24 20 24 24 30
CPU 69 73 76 71 72 75
Table 2: Overhead by selectivity (ms).
(a) GPU
(b) CPU
Figure 6: Execution time (seconds) for the SSB (scale 80) queries.

Overhead by number of attributes. In order to quantify the impact of the number of attributes and their type on ESC, we run the following set of queries which increase the number of attributes in the predicate progressively:


  • SELECT COUNT(*)
    FROM lineitem, orders
    WHERE l_orderkey=o_orderkey AND o_orderkey=x
        [AND o_custkey=y]
        [AND o_orderstatus=z]
        [AND o_totalprice=t]

Moreover, the type of o_orderstatus and o_totalprice is TEXT and DECIMAL(15,2), increasing the evaluation time of the condition compared to INTEGER. All the predicates are equality with constants x, y, z, and t, respectively. We set their values such that the result is always positive. Table 3 shows the results for TPC-H scale 50. For 1 attribute, we obtain results that are consistent with Table 1. As the number of attributes increases, the overhead stays constant on GPU because the simple operations in the predicate do not fill the available processing throughput or memory bandwidth. This is not the case on CPU, where we observe a significant increase as we go from 2 to 3 attributes. The main reason is the degradation in cache performance due to a larger number of attributes being accessed. Nonetheless, an overhead of 300 ms may be offset by a better plan computed with exact selectivities.

# attributes 1 2 3 4
GPU 21 23 23 18
CPU 70 91 273 339
Table 3: Overhead by number of attributes (ms).

3.2 Benchmark Queries

We perform an end-to-end comparison between MapD and our extension MapD+ESC on several queries from standard benchmarks. We report the complete query execution time which – for MapD+ESC – includes the additional sub-queries and their materialization. In this case, the join order can be different across the two systems since they do not execute the same plan.

TPC-H scale 50. We execute the four queries introduced in [14] (Appendix B). Each of them is defined over 3 tables and includes a selection for each table. These queries have selectivities that are difficult to estimate because of correlations across tables. ESC eliminates this issue completely. Figure 5 depicts the results. With a single exception on CPU, MapD+ESC always outperforms standard MapD. The improvement ranges from 1.06X to 32.27X. It is larger when MapD+ESC selects a better execution plan. This is directly correlated with the predicate selectivity on each of the tables. While there is not much difference between MapD running on CPU and GPU, MapD+ESC is always superior on GPU—by as much as 4.88X. This is because the overhead on GPU is much smaller. Moreover, materialization on CPU can interfere with caching and this can have unexpected consequences—the case for query 2.

SSB scale 80. We execute all the queries from the Star-Schema Benchmark (SSB) that are defined over at least two tables. Unlike TPC-H, the fact table lineorder in SSB is much larger and takes 99% of the total dataset. Consequently, the dimension tables are relatively small and this can be a problem for ESC. Figure 6 presents the results. MapD+ESC provides faster execution times across all the queries, both on CPU and GPU. As the number of tables and predicates in a query increases, the gap becomes larger. The largest improvement – more than 7X – is gained for query 4.3 which is more selective. As before, the main reason for the speedup is the ability of MapD+ESC to identify better query plans because of exact selectivities. Interestingly enough, the CPU implementation is faster than the GPU in this case. The larger scale factor is the main culprit. The amount of data that has to be moved to the GPU saturates the PCIe bus and this increases execution time for all the queries.

4 Conclusions and Future Work

In this paper, we introduce a novel query optimization paradigm ESC for in-memory and GPU databases. The central idea in ESC is to compute selectivities exactly through queries during query optimization. We propose several optimizations targeting the selection and materialization of tables and predicates. We implement ESC in the MapD open-source database system. Experiments on the TPC-H and SSB benchmarks show that ESC records constant and less than 30 milliseconds overhead when running on GPU and generates improved query execution plans that are as much as 32X faster. ESC improves the execution both on CPU as well as on GPU. In future work, we plan to design adaptive algorithms that can detect automatically when materialization is feasible. We also plan to investigate strategies that split predicate selectivity evaluation.

References

  • [1] S. Breß, M. Heimel, N. Siegmund, L. Bellatreche, and G. Saake. GPU-accelerated Database Systems: Survey and Open Challenges. In TLDKS, pages 1–35. 2014.
  • [2] S. Chaudhuri. An Overview of Query Optimization in Relational Systems. In PODS 1998.
  • [3] G. Cormode, M. Garofalakis, P. Haas, and C. Jermaine. Synopses for Massive Data: Samples, Histograms, Wavelets, Sketches. Found. Trends Databases, 4(1), 2012.
  • [4] H. Funke, S. Breß, S. Noll, V. Markl, and J. Teubner. Pipelined Query Processing in Coprocessor Environments. In SIGMOD 2018.
  • [5] B. He, M. Lu, K. Yang, R. Fang, N. Govindaraju, Q. Luo, and P. Sander. Relational Query Coprocessing on Graphics Processors. ACM TODS, 34(4), 2009.
  • [6] N. Kabra and D. DeWitt. Efficient Mid-query Re-optimization of Sub-optimal Query Execution Plans. In SIGMOD 1998.
  • [7] V. Leis, A. Gubichev, A. Mirchev, P. Boncz, A. Kemper, and T. Neumann. How Good Are Query Optimizers, Really? PVLDB, 9(3), 2015.
  • [8] G. Lohman. Is Query Optimization a “Solved” Problem?, 2014. [Online at: http://wp.sigmod.org/?p=1075].
  • [9] V. Markl, G. Lohman, and V. Raman. LEO: An Autonomic Query Optimizer for DB2. IBM Syst. J., 42(1), 2003.
  • [10] G. Moerkotte, T. Neumann, , and G. Steidl. Preventing Bad Plans by Bounding the Impact of Cardinality Estimation Errors. PVLDB, 2(1), 2009.
  • [11] T. Mostak. An Overview of MapD (Massively Parallel Database). MIT white paper, 2013.
  • [12] V. Poosala and Y. Ioannidis. Selectivity Estimation Without the Attribute Value Independence Assumption. In VLDB 1997.
  • [13] F. Rusu and A. Dobra. Statistical Analysis of Sketch Estimators. In SIGMOD 2007.
  • [14] K. Tzoumas, A. Deshpande, and C. S. Jensen. Lightweight Graphical Models for Selectivity Estimation Without Independence Assumptions. PVLDB, 4(11), 2011.
  • [15] W. Wu. Sampling-Based Cardinality Estimation Algorithms: A Survey and An Empirical Evaluation, 2012.