Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

opt: introduce placeholder fast path #63807

Merged
merged 3 commits into from
Apr 21, 2021

Commits on Apr 20, 2021

  1. sql: support EXPLAIN ANALYZE EXECUTE

    We currently cannot EXPLAIN an EXECUTE statement (the syntax doesn't
    even allow it). EXECUTE is handled with special code at the top-level,
    so it's tricky to make it work with EXPLAIN. However, EXPLAIN ANALYZE
    Is also handled at the top level, before the EXECUTE handling, so that
    should just work.
    
    This change allows the parsing of such statements so we can use
    EXPLAIN ANALYZE EXECUTE. If EXPLAIN EXECUTE is attempted, we get a
    "not supported" error (this is less cryptic than the parsing error
    anyway).
    
    Release note: None
    RaduBerinde committed Apr 20, 2021
    Configuration menu
    Copy the full SHA
    22e4d64 View commit details
    Browse the repository at this point in the history
  2. opt: normalize In with single-element list to Eq

    This commit adds a rule that normalizes `a IN (b)` to `a = b` (and
    similarly `NOT IN` to `!=`).
    
    This query confirms the equivalency holds even when NULLs are
    involved:
    
    ```
    WITH
      vals (v) AS (VALUES (1), (2), (NULL))
    SELECT
      v1.v, v2.v, v1.v NOT IN (v2.v,) AS in, v1.v != v2.v AS eq
    FROM
      vals AS v1, vals AS v2
    
       v   |  v   |  in   |  eq
    -------+------+-------+--------
         1 |    1 | false | false
         1 |    2 | true  | true
         1 | NULL | NULL  | NULL
         2 |    1 | true  | true
         2 |    2 | false | false
         2 | NULL | NULL  | NULL
      NULL |    1 | NULL  | NULL
      NULL |    2 | NULL  | NULL
      NULL | NULL | NULL  | NULL
    ```
    
    Release note: None
    RaduBerinde committed Apr 20, 2021
    Configuration menu
    Copy the full SHA
    4c6e54e View commit details
    Browse the repository at this point in the history
  3. opt: introduce placeholder fast path

    Currently running a query with placeholders happens as follows:
     - at preparation time, we build a memo with the normalized expression
       *with* placeholders.
     - at execution time, we copy the expression into a new memo,
       replacing placeholders with their value (AssignPlaceholders).
     - then we run exploration which performs the actual optimization.
    
    For trivial queries like KV reads, we do too much work during
    execution (profiles show it is 10-20% of the runtime for KV workloads
    with high read rates).
    
    This commit introduces the concept of a "placeholder fast path". The
    idea is that we can make specific checks for simple expressions and
    produce (at preparation time) a fully optimized expression (which
    still depends on placeholders). For this, we introduce a new operator,
    PlaceholderScan which is similar to a Scan except that it always scans
    one span with the same start and end key, and the key values are child
    scalar expressions (either constants or placeholders).
    
    We use this new operator only for simple SELECTs where the filters
    constrain a prefix of an index to constant values; in addition, the
    index must be covering and there must be only one such index. With
    these conditions, we know the optimal plan upfront.
    
    For now, the placeholder fast path transformation is Go code; if it
    gets more complicated, it should be switched to use optgen rules.
    
    A benchmark on my machine shows a kv95 workload going from 34kiops to
    38kiops with this change.
    
    Release note: None
    RaduBerinde committed Apr 20, 2021
    Configuration menu
    Copy the full SHA
    df67aa9 View commit details
    Browse the repository at this point in the history