Skip to content

opt: reoptimize main query with scalar subquery results #51820

@xinyuliu12

Description

@xinyuliu12

Hello everyone!

First off, I am sorry for gestures vaguely all of this. Second, I think we have found a performance bug related to a suboptimal plan selection when evaluating predicate.

Here is a pair of TPC-H queries that exhibit this behavior. They both ask the database system to project a column in the orders table only if the predicate on the nation table is true.

SELECT o_orderpriority
FROM   orders
WHERE  (SELECT n_comment
        FROM   nation
        ORDER  BY n_comment
        LIMIT  1) IS NOT DISTINCT FROM 'random string';

SELECT o_orderpriority
FROM  orders
LEFT OUTER JOIN
                (SELECT   *
                 FROM     nation
                 ORDER BY n_comment limit 1) AS t ON TRUE
WHERE t.n_comment IS NOT DISTINCT FROM 'random string'; 

Actual Behavior
The first query takes ~8.3 seconds on v20.1.3, while the second query only takes ~44 milliseconds.
For the first query, the database system scans the large orders table (7500000 rows), even when the predicate on the nation table can be evaluated within a few milliseconds. (only 25 rows). Its "EXPLAIN ANALYZE" result is here.
For the second query, the database system evaluates the predicate on the nation table first, which can avoid the unnecessary scan of the orders table if the predicate turns out to be false. Its "EXPLAIN ANALYZE" result is here.

Expected Behavior
I would have expected the database system run these two queries with similar execution time, given that they both have the same semantics. To this end, the optimizer should generate a better execution plan for the first query, such as the plan generated for the second query.
Here are the steps for reproducing our observations:

Test environment

  • Ubuntu 20.04 machine "Linux panda 5.4.0-40-generic add weighted reservoir sampling (AE-S) #44-Ubuntu SMP Tue Jun 23 00:01:04 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux"
  • Cockroachdb v20.1.3
  • Database: TPC-H benchmark (with scale factor 5)

Reproduce Bug

  1. Install cockroachdb v20.1.3
    $ wget -qO- https://binaries.cockroachdb.com/cockroach-v20.1.3.src.tgz | tar  xvz        
    $ cd cockroach-v20.1.3
    $ make build
    $ sudo make install
  • start a cockroachdb node in your preferred working directory
    $ cockroach start --insecure --store=node1 --listen-addr=localhost:26257 --http-addr=localhost:8080 --join=localhost:26257,localhost:26258,localhost:26259 --background
    $ cockroach init --insecure --host=localhost:26257
  1. Set up TPC-H test benchmark (if you already have a TPC-H benchmark set up, you can skip step2; starting from step2, all commands should run in the directory ./tpch5 after downloading and extracting it)
    $ tar xzvf tpch5.tar.gz
    $ cd tpch5
    $ chmod +x setup.sh
  • Create DB and TPC-H schema
    $ cockroach sql --insecure --host=localhost:26257
    In CockroachDB's built-in SQL client to create database:
    $ create database tpch5;
    $ exit
  • Create TPC-H Schema
    $ cockroach sql --insecure --host=localhost:26257 -d tpch5 < dss.ddl
  • Import benchmark
    $ ./setup.sh
  1. Test SQL query that exhibits performance issue
  • Execute the queries
    $ cockroach sql --insecure --host=localhost:26257 -d tpch5 < query.sql

Jira issue: CRDB-4007

Epic CRDB-1491

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-apolloC-investigationFurther steps needed to qualify. C-label will change.O-communityOriginated from the communityT-sql-queriesSQL Queries TeamX-blathers-triagedblathers was able to find an owner

    Type

    No type

    Projects

    Status

    Backlog

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions