GPORCA Features and Enhancements

GPORCA includes enhancements for specific types of queries and operations. GPORCA also includes these optimization enhancements:

  • Improved join ordering
  • Join-Aggregate reordering
  • Sort order optimization
  • Data skew estimates included in query optimization

Queries Against Partitioned Tables

GPORCA includes these enhancements for queries against partitioned tables:

  • Partition elimination is improved.
  • Query plan can contain the Partition selector operator.
  • Partitions are not enumerated in EXPLAIN plans.

    For queries that involve static partition selection where the partitioning key is compared to a constant, GPORCA lists the number of partitions to be scanned in the EXPLAIN output under the Partition Selector operator. This example Partition Selector operator shows the filter and number of partitions selected:

    1. Partition Selector for Part_Table (dynamic scan id: 1)
    2. Filter: a > 10
    3. Partitions selected: 1 (out of 3)

    For queries that involve dynamic partition selection where the partitioning key is compared to a variable, the number of partitions that are scanned will be known only during query execution. The partitions selected are not shown in the EXPLAIN output.

  • Plan size is independent of number of partitions.

  • Out of memory errors caused by number of partitions are reduced.

This example CREATE TABLE command creates a range partitioned table.

  1. CREATE TABLE sales(order_id int, item_id int, amount numeric(15,2),
  2. date date, yr_qtr int)
  3. RANGE PARTITIONED BY yr_qtr;

GPORCA improves on these types of queries against partitioned tables:

  • Full table scan. Partitions are not enumerated in plans.

    1. SELECT * FROM sales;
  • Query with a constant filter predicate. Partition elimination is performed.

    1. SELECT * FROM sales WHERE yr_qtr = 201201;
  • Range selection. Partition elimination is performed.

    1. SELECT * FROM sales WHERE yr_qtr BETWEEN 201301 AND 201404 ;
  • Joins involving partitioned tables. In this example, the partitioned dimension table date_dim is joined with fact table catalog_sales:

    1. SELECT * FROM catalog_sales
    2. WHERE date_id IN (SELECT id FROM date_dim WHERE month=12);

Queries that Contain Subqueries

GPORCA handles subqueries more efficiently. A subquery is query that is nested inside an outer query block. In the following query, the SELECT in the WHERE clause is a subquery.

  1. SELECT * FROM part
  2. WHERE price > (SELECT avg(price) FROM part);

GPORCA also handles queries that contain a correlated subquery (CSQ) more efficiently. A correlated subquery is a subquery that uses values from the outer query. In the following query, the price column is used in both the outer query and the subquery.

  1. SELECT * FROM part p1
  2. WHERE price > (SELECT avg(price) FROM part p2
  3. WHERE p2.brand = p1.brand);

GPORCA generates more efficient plans for the following types of subqueries:

  • CSQ in the SELECT list.

    1. SELECT *,
    2. (SELECT min(price) FROM part p2 WHERE p1.brand = p2.brand)
    3. AS foo
    4. FROM part p1;
  • CSQ in disjunctive (OR) filters.

    1. SELECT FROM part p1 WHERE p_size > 40 OR
    2. p_retailprice >
    3. (SELECT avg(p_retailprice)
    4. FROM part p2
    5. WHERE p2.p_brand = p1.p_brand)
  • Nested CSQ with skip level correlations

    1. SELECT * FROM part p1 WHERE p1.p_partkey
    2. IN (SELECT p_partkey FROM part p2 WHERE p2.p_retailprice =
    3. (SELECT min(p_retailprice)
    4. FROM part p3
    5. WHERE p3.p_brand = p1.p_brand)
    6. );

    Note: Nested CSQ with skip level correlations are not supported by the legacy query optimizer.

  • CSQ with aggregate and inequality. This example contains a CSQ with an inequality.

    1. SELECT * FROM part p1 WHERE p1.p_retailprice =
    2. (SELECT min(p_retailprice) FROM part p2 WHERE p2.p_brand <> p1.p_brand);
  • CSQ that must return one row.

    1. SELECT p_partkey,
    2. (SELECT p_retailprice FROM part p2 WHERE p2.p_brand = p1.p_brand )
    3. FROM part p1;

Queries that Contain Common Table Expressions

GPORCA handles queries that contain the WITH clause. The WITH clause, also known as a common table expression (CTE), generates temporary tables that exist only for the query. This example query contains a CTE.

  1. WITH v AS (SELECT a, sum(b) as s FROM T WHERE c < 10 GROUP BY a)
  2. SELECT *FROM v AS v1 , v AS v2
  3. WHERE v1.a <> v2.a AND v1.s < v2.s;

As part of query optimization, GPORCA can push down predicates into a CTE. For example query, GPORCA pushes the equality predicates to the CTE.

  1. WITH v AS (SELECT a, sum(b) as s FROM T GROUP BY a)
  2. SELECT *
  3. FROM v as v1, v as v2, v as v3
  4. WHERE v1.a < v2.a
  5. AND v1.s < v3.s
  6. AND v1.a = 10
  7. AND v2.a = 20
  8. AND v3.a = 30;

GPORCA can handle these types of CTEs:

  • CTE that defines one or multiple tables. In this query, the CTE defines two tables.

    1. WITH cte1 AS (SELECT a, sum(b) as s FROM T
    2. where c < 10 GROUP BY a),
    3. cte2 AS (SELECT a, s FROM cte1 where s > 1000)
    4. SELECT *
    5. FROM cte1 as v1, cte2 as v2, cte2 as v3
    6. WHERE v1.a < v2.a AND v1.s < v3.s;
  • Nested CTEs.

    1. WITH v AS (WITH w AS (SELECT a, b FROM foo
    2. WHERE b < 5)
    3. SELECT w1.a, w2.b
    4. FROM w AS w1, w AS w2
    5. WHERE w1.a = w2.a AND w1.a > 2)
    6. SELECT v1.a, v2.a, v2.b
    7. FROM v as v1, v as v2
    8. WHERE v1.a < v2.a;

DML Operation Enhancements with GPORCA

GPORCA contains enhancements for DML operations such as INSERT.

  • A DML node in a query plan is a query plan operator.
    • Can appear anywhere in the plan, as a regular node (top slice only for now)
    • Can have consumers
  • New query plan operator Assert is used for constraints checking.

    This example plan shows the Assert operator.

    1. QUERY PLAN
    2. ------------------------------------------------------------
    3. Insert (cost=0.00..4.61 rows=3 width=8)
    4. -> Assert (cost=0.00..3.37 rows=3 width=24)
    5. Assert Cond: (dmlsource.a > 2) IS DISTINCT FROM
    6. false
    7. -> Assert (cost=0.00..2.25 rows=3 width=24)
    8. Assert Cond: NOT dmlsource.b IS NULL
    9. -> Result (cost=0.00..1.14 rows=3 width=24)
    10. -> Table Scan on dmlsource

Queries with Distinct Qualified Aggregates (DQA)

GPORCA improves performance for queries that contain distinct qualified aggregates (DQA) without a grouping column and when the table is not distributed on the columns used by the DQA. When encountering these types of queries, GPORCA uses an alternative plan that evaluates the aggregate functions in three stages (local, intermediate, and global aggregations).

See optimizer_prefer_scalar_dqa_multistage_agg for information on the configuration parameter that controls this behavior.