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

optimizer not take into account expression index (even with index hinting) #83390

Closed
kocoten1992 opened this issue Jun 26, 2022 · 2 comments · Fixed by #83619
Closed

optimizer not take into account expression index (even with index hinting) #83390

kocoten1992 opened this issue Jun 26, 2022 · 2 comments · Fixed by #83619
Assignees
Labels
C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. O-community Originated from the community T-sql-queries SQL Queries Team X-blathers-triaged blathers was able to find an owner

Comments

@kocoten1992
Copy link

kocoten1992 commented Jun 26, 2022

cockroachdb version 22.1

create table why_u_no_use_index (id uuid, long_text text);
insert into why_u_no_use_index (select gen_random_uuid(), random() * 100 from generate_series(1, 100000));
create index null_check on why_u_no_use_index((long_text is null));
explain analyze select * from why_u_no_use_index where long_text is null limit 100;
# postgres
postgres=# explain analyze select * from why_u_no_use_index where long_text is null limit 100;
                                                              QUERY PLAN                                                              
--------------------------------------------------------------------------------------------------------------------------------------
 Limit  (cost=0.29..4.31 rows=1 width=32) (actual time=0.075..0.075 rows=0 loops=1)
   ->  Index Scan using null_check on why_u_no_use_index  (cost=0.29..4.31 rows=1 width=32) (actual time=0.074..0.074 rows=0 loops=1)
         Index Cond: ((long_text IS NULL) = true)
         Filter: (long_text IS NULL)
 Planning Time: 0.168 ms
 Execution Time: 0.093 ms
(6 rows)
# cockroachdb
root@:26257/defaultdb> explain analyze select * from why_u_no_use_index@null_check where long_text is null limit 100;
                             info
---------------------------------------------------------------
  planning time: 579µs
  execution time: 12.5s
  distribution: full
  vectorized: true
  rows read from KV: 200,000 (9.0 MiB)
  cumulative time spent in KV: 12.5s
  maximum memory usage: 10 MiB
  network usage: 0 B (0 messages)

  • limit
  │ nodes: n1
  │ actual row count: 0
  │ count: 100
  │
  └── • filter
      │ nodes: n1
      │ actual row count: 0
      │ filter: long_text IS NULL
      │
      └── • index join
          │ nodes: n1
          │ actual row count: 100,000
          │ KV time: 12.1s
          │ KV contention time: 0µs
          │ KV rows read: 100,000
          │ KV bytes read: 5.3 MiB
          │ estimated max memory allocated: 6.4 MiB
          │ estimated max sql temp disk usage: 0 B
          │ table: why_u_no_use_index@why_u_no_use_index_pkey
          │
          └── • scan
                nodes: n1
                actual row count: 100,000
                KV time: 477ms
                KV contention time: 0µs
                KV rows read: 100,000
                KV bytes read: 3.6 MiB
                estimated max memory allocated: 3.7 MiB
                missing stats
                table: why_u_no_use_index@null_check
                spans: FULL SCAN
(41 rows)


Time: 12.541s total (execution 12.540s / network 0.001s)

Jira issue: CRDB-17042

@kocoten1992 kocoten1992 added the C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. label Jun 26, 2022
@blathers-crl
Copy link

blathers-crl bot commented Jun 26, 2022

Hello, I am Blathers. I am here to help you get the issue triaged.

It looks like you have not filled out the issue in the format of any of our templates. To best assist you, we advise you to use one of these templates.

I have CC'd a few people who may be able to assist you:

  • @cockroachdb/sql-queries (found keywords: vectorized,PLAN)
  • @cockroachdb/kv (found keywords: KV)

If we have not gotten back to your issue within a few business days, you can try the following:

  • Join our community slack channel and ask on #cockroachdb.
  • Try find someone from here if you know they worked closely on the area and CC them.

🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is otan.

@blathers-crl blathers-crl bot added O-community Originated from the community X-blathers-triaged blathers was able to find an owner labels Jun 26, 2022
@blathers-crl blathers-crl bot added the T-sql-queries SQL Queries Team label Jun 26, 2022
@mgartner mgartner self-assigned this Jun 27, 2022
@mgartner
Copy link
Collaborator

Thank you for the report! We'll aim to address this soon.

craig bot pushed a commit that referenced this issue Jul 11, 2022
83619: opt: constrain expression indexes with IS NULL expressions r=mgartner a=mgartner

The optimizer can generate constrained scans over indexes on computed
columns when columns referenced in the computed column expression are
held constant. Consider this example:

    CREATE TABLE t (a INT, v INT AS (a + 1) STORED, INDEX v_idx (v))
    SELECT * FROM t WHERE a = 1

A constrained scan can be generated over `v_idx` because `v` depends on
`a` and the query filter holds `a` constant.

This commit lifts a restriction that prevented this optimization when
columns referenced in the computed column expression were held constant
to the `NULL` value. As far as I can tell, this restriction is not
necessary. In fact, `@rytaft` had questioned its purpose originally, but
the question was never answered:

#43450 (review)

By lifting this restriction, the optimizer can explore constrained scans
over both indexed computed columns with `IS NULL` expressions and
expression indexes with `IS NULL` expressions.

Fixes #83390

Release note (performance improvement): The optimizer now explores more
efficient query plans when index computed columns and expressions have
`IS NULL` expressions.

84084: bazel: new versions of prebuilt `c-deps` r=srosenberg a=rickystewart

Rebuild these archives to pull in
`52a3a0aa8a707f9bb03802186da0c60b715ed9ce` (change to `jemalloc` to
build without `MADV_FREE`).

Release note: None

84088: ui: fix alignment on custom scale r=maryliag a=maryliag

The check for valid options with the
removal of some options on #83229 didn't took
the custom values into consideration.
This commit add the option back, allowing the alignment
on custom values.

Release note (bug fix): Custom time period selection is now aligning
between Metrics and SQL Activity page.

84155: sql/schemachanger/scbuild: minor cleanup r=ajwerner a=ajwerner

Improves the error handling a tad to make runtime errors and assertion failure.
Fixes a typo.

Release note: None

Co-authored-by: Marcus Gartner <[email protected]>
Co-authored-by: Ricky Stewart <[email protected]>
Co-authored-by: Marylia Gutierrez <[email protected]>
Co-authored-by: Andrew Werner <[email protected]>
@craig craig bot closed this as completed in 35e9737 Jul 11, 2022
@mgartner mgartner moved this to Done in SQL Queries Jul 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. O-community Originated from the community T-sql-queries SQL Queries Team X-blathers-triaged blathers was able to find an owner
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

2 participants