Amazon Athena is a serverless, interactive analytics service constructed on open supply frameworks, supporting open desk file codecs. Athena supplies a simplified, versatile approach to analyze petabytes of information the place it lives. You’ll be able to analyze knowledge or construct purposes from an Amazon Easy Storage Service (Amazon S3) knowledge lake and 30 knowledge sources, together with on-premises knowledge sources or different cloud methods utilizing SQL or Python. Athena is constructed on open supply Trino and Presto engines and Apache Spark frameworks, with no provisioning or configuration effort required.
Beginning at this time, the Athena SQL engine makes use of a cost-based optimizer (CBO), a brand new function that makes use of desk and column statistics saved within the AWS Glue Information Catalog as a part of the desk’s metadata. Through the use of these statistics, CBO improves question run plans and boosts the efficiency of queries run in Athena. A number of the particular optimizations CBO can make use of embrace be a part of reordering and pushing aggregations down based mostly on the statistics obtainable for every desk and column.
TPC-DS benchmarks These benchmarks display the ability of the cost-based optimizer—queries run as much as 2x occasions quicker with CBO enabled in comparison with operating the identical TPC-DS queries with out CBO.
Efficiency and price comparability on TPC-DS benchmarks
We used the industry-standard TPC-DS 3 TB to symbolize completely different buyer use instances. These are consultant of workloads with 10 occasions the acknowledged benchmark measurement. This implies a 3 TB benchmark dataset precisely represents buyer workloads on 30–50 TB datasets.
In our testing, the dataset was saved in Amazon S3 in non-compressed Parquet format and the AWS Glue Information Catalog was used to retailer metadata for databases and tables. Reality tables have been partitioned on the date column used for be a part of operations, and every truth desk consisted of two,000 partitions. To assist illustrate the efficiency of CBO, we evaluate the habits of assorted queries and spotlight the efficiency variations between operating with CBO enabled vs. disabled.
The next graph illustrates the runtime of queries on the engine with and with out CBO.
The next graph presents the highest 10 queries from the TPC-DS benchmark with the best efficiency enchancment.
Let’s talk about a few of the cost-based optimization strategies that contributed to improved question efficiency.
Price-based be a part of reordering
Be part of reordering, an optimization approach utilized by cost-based SQL optimizers, analyzes completely different be a part of sequences to pick the order that minimizes question runtime by decreasing intermediate knowledge processed at every step, decreasing reminiscence and CPU necessities.
Let’s discuss question 82 of the TPC-DS 3TB dataset. The question performs interior joins on 4 tables:
store_sales desk has 8.6 billion rows and is partitioned by date. The
stock desk has 1 billion rows and can be partitioned by date. The
merchandise desk comprises 360,000 rows, and the
date_dim desk holds 73,000 rows.
With out CBO
With out utilizing CBO, the engine will decide the be a part of order based mostly on the sequence of tables outlined within the enter question with inner heuristics. The FROM clause of the enter question is
"from merchandise, stock, date_dim, store_sales" (all interior joins). After passing by inner heuristics, Athena selected the be a part of order as ((
merchandise ⋈ (
store_sales). Regardless of
store_sales being the biggest truth desk, it’s outlined final within the FROM clause and subsequently will get joined final. This plan fails to cut back the intermediate be a part of sizes as early as doable, leading to an elevated question runtime. The next diagram reveals the be a part of order with out CBO and the variety of rows flowing by completely different phases.
When utilizing CBO, the optimizer determines one of the best be a part of order utilizing a wide range of knowledge, together with statistics in addition to be a part of measurement estimation, be a part of construct facet, and be a part of kind. On this occasion, Athena’s chosen be a part of order is ((
merchandise) ⋈ (
date_dim)). The biggest truth desk,
store_sales, with out being shuffled, is first joined with the
merchandise dimension desk. The opposite partitioned desk,
stock, can be first joined in-place with the
date_dim dimension desk. The be a part of with the dimension desk acts as a filter on the very fact desk, which dramatically reduces the enter knowledge measurement of the be a part of that follows. Notice that which facet a desk resides for a be a part of is important in Athena, as a result of it’s the desk on the fitting that shall be constructed into reminiscence for the be a part of operation. Due to this fact, we all the time need to maintain the bigger desk on the left and the smaller desk on the fitting. CBO selected a plan that the left facet was 8.6 billion earlier than, and now it’s 13.6 million.
With CBO, the question runtime improved by 25% (from 15 seconds all the way down to 11 seconds) by selecting the optimum be a part of order.
Subsequent, let’s talk about one other CBO approach.
Price-based aggregation pushdown
Aggregation pushdown is an optimization approach utilized by question optimizers to enhance efficiency. It entails pushing aggregation operations like SUM, COUNT, and AVG into an earlier stage within the question plan, whereas sustaining the identical question semantics. This reduces the quantity of information transferred between the phases. By minimizing knowledge processing, aggregation pushdown decreases reminiscence utilization, I/O prices, and community visitors.
Nonetheless, pushing down aggregation just isn’t all the time useful. It is dependent upon the info distribution. For instance, grouping on a column with many rows however few distinct values (like gender) earlier than joins works higher. Grouping first means aggregating numerous information into fewer information (simply male, feminine, for instance). Grouping after becoming a member of means numerous information need to take part the be a part of earlier than being aggregated. Alternatively, grouping on a excessive cardinality column is healthier carried out after joins. Doing it earlier than dangers pointless aggregation overhead as a result of every worth is probably going distinctive anyway and that step is not going to end in an earlier discount within the quantity of information transferred between intermediate phases.
Due to this fact, whether or not to push down aggregation ought to be a cost-based resolution. Let’s take instance of the question 2 run on a 3TB TPC-DS dataset, exhibiting how the aggregation pushdown’s worth is dependent upon knowledge distribution. The
web_sales desk has 2.1 billion rows and the
catalog_sales desk has 4.23 billion rows. Each tables are partitioned on the date column.
With out CBO
Athena first joins the results of the union all operation on the
web_sales desk and the
catalog_sales desk with one other desk. Solely then does it carry out aggregation on the joined outcomes. On this instance, the quantity of information that wanted to be joined was large, leading to an extended question runtime.
Athena makes use of one of many statistics values, the distinct worth rely, to guage the price implications of pushing down the aggregation vs. not doing so. When a column has many rows however few distinct values, CBO is extra more likely to push aggregation down. This shrank the certified rows from
catalog_sales tables to 2,590 and three,590 rows, respectively. These aggregated information have been then unioned and used to hitch with the tables. Evaluating to the plan with out CBO, the information taking part within the be a part of from the 2 massive tables dropped from 6.33 billion rows (2.1 billion + 4.23 billion) to only 6,180 rows (2,590 + 3,590). This considerably decreased question runtime.
With CBO, the question runtime improved by 50% (from 37 seconds all the way down to 18 seconds). In abstract, CBO helped Athena select an optimum aggregation pushdown plan, chopping the question time in half in comparison with not utilizing cost-based optimization.
On this publish, we mentioned how Athena makes use of a cost-based optimizer (CBO) in its engine v3 to make use of desk statistics for producing extra environment friendly question run plans. Testing on the TPC-DS benchmark confirmed an 11% enchancment in general question efficiency when utilizing CBO in comparison with with out it.
Two key optimization employed by CBO are be a part of reordering and mixture pushdown. Be part of reordering reduces intermediate knowledge by intelligently selecting the order to hitch tables based mostly on statistics. Mixture pushdown decreases intermediate knowledge by pushing aggregations earlier within the plan when useful.
In abstract, Athena’s new cost-based optimizer considerably quickens queries by selecting superior run plans. CBO optimizes based mostly on desk statistics saved within the AWS Glue Information Catalog. This computerized optimization improves productiveness for Athena customers by extra responsive question efficiency. To benefit from optimization strategies of CBO, check with working with column statistics to generate statistics on the tables and columns within the AWS Glue Information Catalog.
In regards to the Authors
Darshit Thakkar is a Technical Product Supervisor with AWS and works with the Amazon Athena group based mostly out of Boston, Massachusetts.
Wei Zheng is a Sr. Software program Growth Engineer with Amazon Athena. He joined AWS in 2021 and has been engaged on a number of efficiency enhancements on Athena.
Chuho Chang is a Software program Growth Engineer with Amazon Athena. He has been engaged on question optimizers for over a decade.
Pathik Shah is a Sr. Analytics Architect on Amazon Athena. He joined AWS in 2015 and has been focusing within the massive knowledge analytics area since then, serving to clients construct scalable and strong options utilizing AWS analytics companies.