A Practical Approach to Groupjoin and Nested Aggregates

A Practical Approach to Groupjoin and Nested Aggregates

A Practical Approach to Groupjoin and Nested Aggregates Philipp Fent Thomas Neumann Technische Universität München Technische Universität München [email protected] [email protected] ABSTRACT Improved Cardinality Groupjoins, the combined execution of a join and a subsequent Estimates of Aggregates group by, are common in analytical queries, and occur in about 1/8 Plan of the queries in TPC-H and TPC-DS. While they were originally SQL � invented to improve performance, efficient parallel execution of Query Γ Γ Result groupjoins can be limited by contention, which limits their useful- A B C Γ ness in a many-core system. Having an efficient implementation � of groupjoins is highly desirable, as groupjoins are not only used Contention-free Parallel Groupjoin Execution to fuse group by and join but are also introduced by the unnesting component of the query optimizer to avoid nested-loops evalu- Figure 1: Missing components for practical groupjoins. Our ation of aggregates. Furthermore, the query optimizer needs be improvements to estimation and parallel execution enable able to reason over the result of aggregation in order to sched- efficient evaluation of queries with nested aggregates. ule it correctly. Traditional selectivity and cardinality estimations quickly reach their limits when faced with computed columns from nested aggregates, which leads to poor cost estimations and thus, suboptimal query plans. The primary reason to use a groupjoin, is its performance. We In this paper, we present techniques to efficiently estimate, plan, spend less time building hash tables, use less memory, and improve and execute groupjoins and nested aggregates. We propose two the responsiveness of this query. However, groupjoins are also novel techniques, aggregate estimates to predict the result distribu- more capable than regular group-bys, as we can create the groups tion of aggregates, and parallel groupjoin execution for a scalable explicitly. Consider the following nested query, with subtly different execution of groupjoins. The resulting system has significantly bet- semantics: ter estimates and a contention-free evaluation of groupjoins, which can speed up some TPC-H queries up to a factor of 2. SELECT cust.id, cnt, s FROM customer cust, ( PVLDB Reference Format: SELECT COUNT(*) AS cnt, SUM(s.value) as s Philipp Fent and Thomas Neumann. A Practical Approach to Groupjoin FROM sales s and Nested Aggregates. PVLDB, 14(11): 2383 - 2396, 2021. WHERE cust.id= s.c_id doi:10.14778/3476249.3476288 ) 1 INTRODUCTION Here, nested the query calculates a COUNT(*) over the inner table, which evaluates to zero when there are no join partners. Answering Joins and aggregations are the backbone of query engines. A com- that query without nested-loop evaluation of the inner query is mon query pattern, which we observe in many benchmarks [7, 45] tricky, as a regular join plus group-by will produce wrong results and industry applications [58], is a join with grouped aggregation for empty subqueries, which is known as the COUNT bug [44]. A on the same key: groupjoin directly supports such queries by evaluating the static SELECT cust.id, COUNT(*), SUM(s.value) aggregate for the nested side of the join, taking the groups from FROM customer cust, sales s the other side. WHERE cust.id= s.c_id Despite their benefits, groupjoins are not widely in use. We GROUPBY cust.id identify two problems and propose solutions that make groupjoins In a traditional implementation, we answer the query by building more practical: First, existing algorithms for groupjoins do not scale two hash tables on the same key, one for the hash join and one for well for parallel execution. Since the groupjoin hash table contains the group-by. However, we can speed up this query by reusing the shared aggregation state, parallel updates of these need synchro- join’s hash table to also store the aggregate values. This combined nization, and can cause heavy memory contention. Furthermore, execution of join and group-by is called a groupjoin [42]. current estimation techniques deal poorly with results of groupjoins from unnested aggregates. This work is licensed under the Creative Commons BY-NC-ND 4.0 International The unnesting of inner aggregation subqueries is very prof- License. Visit https://creativecommons.org/licenses/by-nc-nd/4.0/ to view a copy of this license. For any use beyond those covered by this license, obtain permission by itable, since it eliminates nested-loops evaluation and improves emailing [email protected]. Copyright is held by the owner/author(s). Publication rights the asymptotic complexity of the query. However, this causes the licensed to the VLDB Endowment. aggregates to be part of a bigger query tree, mangled between Proceedings of the VLDB Endowment, Vol. 14, No. 11 ISSN 2150-8097. doi:10.14778/3476249.3476288 joins, predicates and other relational operators. Query optimiza- tion, specifically join ordering, depends on the quality of cardinality 2383 and selectivity estimates [37]. With unnested aggregates, the es- ΓR.id timation includes group-by operations and aggregates, which are 1 notoriously hard [22, 32]. Consider the following nested aggregate R.id = S.r_id ΓR.id = S.r_id with a predicate: ⇒ 2 R S R S SELECT ... GROUPBY x HAVING SUM(value)> 100 R.id {R.*} → The result might have vastly different cardinality, depending on the Figure 2: Preconditions to introduce a groupjoin. selectivity, which in turn influences the optimal execution order of the query. In our paper, we work on techniques that make combined join Figure 2 are satisfied, and we can separately evaluate the aggregates: and aggregation more efficient, e.g., with eager aggregation [54, 59] 1 The join and aggregation keys need to be equivalent, and 2 and hash table sharing via groupjoins [19, 42]. In addition, we these keys are a superkey w.r.t. functional dependencies of the left propose a novel estimation framework for computed aggregate build side. In this case, introducing a groupjoin is usually considered columns, which improves the plan quality with nested aggregates. to be a net win [14, 19] and can reduce the cost of those operators We introduce this here as part of our work in groupjoins, but the by up to 50% by eliminating intermediate results. estimation framework is useful for queries with regular group- by operators, too. We integrate our work in the high-performance 2.2 Correlated Subquery Unnesting compiling query engine of our research database system Umbra [47]. Figure 1 shows a high-level overview of our query optimizer. On the The groupjoin also supports the challenging edge cases of whole ta- way from an SQL query from a relational algebra query plan to the ble aggregates in a correlated subquery. Consider the correlated sub- query result, we focus on efficiently evaluating nested aggregates query from Section 1, where we calculate a whole-table COUNT(*) with computed column estimates and parallel groupjoin execution. on sales that is correlated with the outer query’s customer. Con- The rest of this paper is structured as follows: First, we introduce ceptually, we need to calculate a whole table aggregate for each the groupjoin and its use in general unnesting in Section 2. Then, we customer, but ideally want to introduce a more efficient join. How- discuss and evaluate three parallel groupjoin execution strategies in ever, using an outer join is tricky, since we cannot directly translate Section 3, and propose a cost model to choose the optimal execution whole table aggregates to the join result. A groupjoin can instead strategy. Afterwards, we introduce our estimations for computed evaluate the left and right sides separately, where a careful initial- columns in Section 4. Section 5 shows our experimental results ization can produce equivalent results to whole table aggregates. based on the well-known TPC-H and TPC-DS benchmarks, before For the COUNT(*) example, we initialize empty groups (e.g., cus- we discuss related work in Section 6. tomers with no sales) as zero, and increment it with whole-table tuple counting logic1. 2 GROUPJOIN FOR NESTED AGGREGATES Apart from better performance, the semantics of groupjoins are Dependent join conceptual useful to compute nested aggregates. Due to the versatile subqueries unnesting introduced groupjoin in SQL, aggregates can appear in various places of the query plan. To Γ efficiently calculate such aggregates, it is important to unnest and R Γ Γ not evaluate them in nested-loops [4, 26, 48]. However, unnested and decorrelated aggregates need a careful implementation and are S R � S R Γ: S D challenging in further query planning. 2.1 Groupjoin Figure 3: General Unnesting: Decorrelation of dependent sub- queries containing an aggregation can introduce a groupjoin. We define a groupjoin Γ [42] as an equi-join with separate aggre- gates over its binary inputs grouped by the join key. For the general case, we deliberately introduce a groupjoin to separately calculate the aggregates of the correlated subquery, filter ' Γ ( fA ◦ »6 : 퐺 ¼ ◦ »6 : 퐺 ¼ j A 2 ', 01 = 02 : 066 B A ' B ( unnecessary tuples, and avoid the COUNT bug [48]. Figure 3 shows 퐺( = 066¹fB j B 2 ( ^ A.01 = B.02gº, this unnesting for two arbitrary tables R and S, with the dependent 퐺' = 066¹fA j A 2 ' ^ A.01 = B.02gºg subquery-join on top and a nested whole table aggregate Γ in the correlated right subtree. To decorrelate this aggregate, we first We further require that 0 ! ', i.e., that the join condition 1 compute the magic set º of relevant tuples for the correlated sub- functionally determines R. With this definition, we compute an query [56]. To compute the set, we eliminate any duplicates of the equi-join between R and S on a key of R, and compute aggregates outer-side join key with a group-by Γ and get the precise domain separately over the matching tuples, which can be beneficial since D of potentially equivalent keys for which we need to calculate the we can avoid duplicate tuples of R and building a duplicate hash inner aggregate.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    14 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us