Home Big Data Pace up queries with the cost-based optimizer in Amazon Athena

Pace up queries with the cost-based optimizer in Amazon Athena

Pace up queries with the cost-based optimizer in Amazon Athena


Amazon Athena is a serverless, interactive analytics service constructed on open supply frameworks, supporting open desk file codecs. Athena gives a simplified, versatile method to analyze petabytes of information the place it lives. You may analyze knowledge or construct functions from an Amazon Easy Storage Service (Amazon S3) knowledge lake and 30 knowledge sources, together with on-premises knowledge sources or different cloud programs 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 the moment, the Athena SQL engine makes use of a cost-based optimizer (CBO), a brand new characteristic that makes use of desk and column statistics saved within the AWS Glue Knowledge Catalog as a part of the desk’s metadata. By utilizing these statistics, CBO improves question run plans and boosts the efficiency of queries run in Athena. A few of the particular optimizations CBO can make use of embrace be 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 facility of the cost-based optimizer—queries run as much as 2x instances quicker with CBO enabled in comparison with working the identical TPC-DS queries with out CBO.

Efficiency and value comparability on TPC-DS benchmarks

We used the industry-standard TPC-DS 3 TB to signify totally different buyer use instances. These are consultant of workloads with 10 instances 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 Knowledge Catalog was used to retailer metadata for databases and tables. Truth tables have been partitioned on the date column used for be part of operations, and every reality desk consisted of two,000 partitions. To assist illustrate the efficiency of CBO, we examine the habits of assorted queries and spotlight the efficiency variations between working 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 among the cost-based optimization strategies that contributed to improved question efficiency.

Price-based be part of reordering

Be part of reordering, an optimization approach utilized by cost-based SQL optimizers, analyzes totally different be 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 internal joins on 4 tables: merchandise, stock, date_dim, and store_sales. The 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 incorporates 360,000 rows, and the date_dim desk holds 73,000 rows.

Question 82

choose  i_item_id ,i_item_desc ,i_current_price
from merchandise, stock, date_dim, store_sales
the place i_current_price between 30 and 30+30
and inv_item_sk = i_item_sk
and d_date_sk=inv_date_sk
and forged(d_date as date) between forged('2002-05-30' as date) and (forged('2002-05-30' as date) +  interval '60' day)
and i_manufact_id in (437,129,727,663)
and inv_quantity_on_hand between 100 and 500
and ss_item_sk = i_item_sk
group by i_item_id,i_item_desc,i_current_price
order by i_item_id
restrict 100

With out CBO

With out utilizing CBO, the engine will decide the be 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 internal joins). After passing by way of inner heuristics, Athena selected the be part of order as ((merchandise ⋈ (stockdate_dim)) ⋈ store_sales). Regardless of store_sales being the most important reality desk, it’s outlined final within the FROM clause and subsequently will get joined final. This plan fails to cut back the intermediate be part of sizes as early as potential, leading to an elevated question runtime. The next diagram exhibits the be part of order with out CBO and the variety of rows flowing by way of totally different phases.

With CBO

When utilizing CBO, the optimizer determines one of the best be part of order utilizing a wide range of knowledge, together with statistics in addition to be part of measurement estimation, be part of construct facet, and be part of sort. On this occasion, Athena’s chosen be part of order is ((store_salesmerchandise) ⋈ (stockdate_dim)). The biggest reality 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 part of with the dimension desk acts as a filter on the actual fact desk, which dramatically reduces the enter knowledge measurement of the be part of that follows. Observe that which facet a desk resides for a be part of is important in Athena, as a result of it’s the desk on the appropriate that can be constructed into reminiscence for the be part of operation. Subsequently, we at all times wish to preserve the bigger desk on the left and the smaller desk on the appropriate. 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 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 includes 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 site visitors.

Nonetheless, pushing down aggregation is just not at all times helpful. It will depend on the information 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 data into fewer data (simply male, feminine, for instance). Grouping after becoming a member of means numerous data must take part the be part of earlier than being aggregated. Then again, grouping on a excessive cardinality column is best achieved after joins. Doing it earlier than dangers pointless aggregation overhead as a result of every worth is probably going distinctive anyway and that step won’t end in an earlier discount within the quantity of information transferred between intermediate phases.

Subsequently, whether or not to push down aggregation ought to be a cost-based choice. Let’s take instance of the question 2 run on a 3TB TPC-DS dataset, exhibiting how the aggregation pushdown’s worth will depend on 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.

Question 2

with wscs as
 (choose sold_date_sk
  from (choose ws_sold_date_sk sold_date_sk
              ,ws_ext_sales_price sales_price
        from web_sales 
        union all
        choose cs_sold_date_sk sold_date_sk
              ,cs_ext_sales_price sales_price
        from catalog_sales)),
 wswscs as 
 (choose d_week_seq,
        sum(case when (d_day_name="Sunday") then sales_price else null finish) sun_sales,
        sum(case when (d_day_name="Monday") then sales_price else null finish) mon_sales,
        sum(case when (d_day_name="Tuesday") then sales_price else  null finish) tue_sales,
        sum(case when (d_day_name="Wednesday") then sales_price else null finish) wed_sales,
        sum(case when (d_day_name="Thursday") then sales_price else null finish) thu_sales,
        sum(case when (d_day_name="Friday") then sales_price else null finish) fri_sales,
        sum(case when (d_day_name="Saturday") then sales_price else null finish) sat_sales
 from wscs
 the place d_date_sk = sold_date_sk
 group by d_week_seq)
 choose d_week_seq1
 (choose wswscs.d_week_seq d_week_seq1
        ,sun_sales sun_sales1
        ,mon_sales mon_sales1
        ,tue_sales tue_sales1
        ,wed_sales wed_sales1
        ,thu_sales thu_sales1
        ,fri_sales fri_sales1
        ,sat_sales sat_sales1
  from wswscs,date_dim 
  the place date_dim.d_week_seq = wswscs.d_week_seq and
        d_year = 2001) y,
 (choose wswscs.d_week_seq d_week_seq2
        ,sun_sales sun_sales2
        ,mon_sales mon_sales2
        ,tue_sales tue_sales2
        ,wed_sales wed_sales2
        ,thu_sales thu_sales2
        ,fri_sales fri_sales2
        ,sat_sales sat_sales2
  from wswscs
  the place date_dim.d_week_seq = wswscs.d_week_seq and
        d_year = 2001+1) z
 the place d_week_seq1=d_week_seq2-53
 order by d_week_seq1

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.

With CBO

Athena makes use of one of many statistics values, the distinct worth depend, to judge the associated fee 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 web_sales and catalog_sales tables to 2,590 and three,590 rows, respectively. These aggregated data have been then unioned and used to affix with the tables. Evaluating to the plan with out CBO, the data taking part within the be 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, slicing the question time in half in comparison with not utilizing cost-based optimization.


On this put up, 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 part of reordering and mixture pushdown. Be part of reordering reduces intermediate knowledge by intelligently choosing the order to affix tables based mostly on statistics. Combination pushdown decreases intermediate knowledge by pushing aggregations earlier within the plan when helpful.

In abstract, Athena’s new cost-based optimizer considerably hastens queries by selecting superior run plans. CBO optimizes based mostly on desk statistics saved within the AWS Glue Knowledge Catalog. This computerized optimization improves productiveness for Athena customers by way of extra responsive question efficiency. To reap the benefits of optimization strategies of CBO, seek advice from working with column statistics to generate statistics on the tables and columns within the AWS Glue Knowledge Catalog.

In regards to the Authors

Darshit Thakkar is a Technical Product Supervisor with AWS and works with the Amazon Athena workforce 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 huge knowledge analytics house since then, serving to clients construct scalable and strong options utilizing AWS analytics providers.



Please enter your comment!
Please enter your name here