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: include != and IS DISTINCT FROM and IS NOT NULL columns in index recommendations #102206

Closed
michae2 opened this issue Apr 25, 2023 · 1 comment · Fixed by #104208
Closed
Labels
A-sql-explain Issues related to EXPLAIN and EXPLAIN ANALYZE improvements C-enhancement Solution expected to add code/behavior + preserve backward-compat (pg compat issues are exception) E-quick-win Likely to be a quick win for someone experienced. E-starter Might be suitable for a starter project for new employees or team members. T-sql-queries SQL Queries Team

Comments

@michae2
Copy link
Collaborator

michae2 commented Apr 25, 2023

Index recommendations currently do not consider hypothetical indexes on columns used with the following operators:

  • !=
  • IS DISTINCT FROM
  • IS NOT NULL

I think columns used with these operators could go into the R (range) set of columns during hypothetical index creation, as we still generate constrained index scans for them when an index exists.

Jira issue: CRDB-27339

@michae2 michae2 added C-enhancement Solution expected to add code/behavior + preserve backward-compat (pg compat issues are exception) A-sql-explain Issues related to EXPLAIN and EXPLAIN ANALYZE improvements E-quick-win Likely to be a quick win for someone experienced. T-sql-queries SQL Queries Team labels Apr 25, 2023
@michae2
Copy link
Collaborator Author

michae2 commented Apr 25, 2023

Here are some examples:

CREATE TABLE a (a INT);
INSERT INTO a SELECT IF(i % 10 = 0, NULL, i) FROM generate_series(0, 99) s(i);
ANALYZE a;

-- these all recommend CREATE INDEX ON a (a)
EXPLAIN SELECT * FROM a WHERE a = 3;
EXPLAIN SELECT * FROM a WHERE a > 3;
EXPLAIN SELECT * FROM a WHERE a IS NOT DISTINCT FROM 3;
EXPLAIN SELECT * FROM a WHERE a IS NULL;
EXPLAIN SELECT * FROM a WHERE a IS NOT DISTINCT FROM NULL;

-- these do not, even though they do use the index when created
EXPLAIN SELECT * FROM a WHERE a IS DISTINCT FROM 3;
EXPLAIN SELECT * FROM a WHERE a != 3;
EXPLAIN SELECT * FROM a WHERE a IS NOT NULL;
EXPLAIN SELECT * FROM a WHERE a IS DISTINCT FROM NULL;

@michae2 michae2 added the E-starter Might be suitable for a starter project for new employees or team members. label Apr 25, 2023
qiyanghe1998 added a commit to qiyanghe1998/cockroach that referenced this issue Jun 1, 2023
…index recommendations

Fixes cockroachdb#102206

As for the tests, since !=, IS DISTINT FROM and IS NOT are categorized as range
index, so I changed all tests with "R": R, EQ + R, J + R, EQ + J + R.

Release note: None
craig bot pushed a commit that referenced this issue Jun 1, 2023
104206: sql: unskip TestTxnContentionEventsTable r=gtr a=gtr

Informs: #102660.

Release note: None

104208: indexrec: improve recommendations for queries with inequalities r=qiyanghe1998 a=qiyanghe1998

Fixes #102206

As for the tests, since !=, IS DISTINT FROM and IS NOT are categorized as range
index, so I copied similar tests with "R": R, EQ + R, J + R, EQ + J + R.

Release note: None

Epic: None

Co-authored-by: gtr <[email protected]>
Co-authored-by: qiyanghe1998 <[email protected]>
@craig craig bot closed this as completed in 5f50b7a Jun 1, 2023
@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
A-sql-explain Issues related to EXPLAIN and EXPLAIN ANALYZE improvements C-enhancement Solution expected to add code/behavior + preserve backward-compat (pg compat issues are exception) E-quick-win Likely to be a quick win for someone experienced. E-starter Might be suitable for a starter project for new employees or team members. T-sql-queries SQL Queries Team
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

1 participant