Skip to content

Bad index plan for lookup on NULL #8885

Open
@nicktobey

Description

create table test(pk int primary key, c0 int, key idx1(c0, pk));
insert into test values (1, 0), (2, 1), (3, 2), (4, 1), (5, 2), (6, 0), (7, 2), (8, 0), (9, 1), (10, NULL);
describe plan select * from test where c0 is NULL and pk > 1;

Expected plan:

+------------------------------------------+
| plan                                     |
+------------------------------------------+
| IndexedTableAccess(test)                 |
|  ├─ index: [test.c0,test.pk]             |
|  ├─ filters: [{[NULL, 1], [NULL, ∞)}] |
|  └─ columns: [pk c0]                     |
+------------------------------------------+

Actual plan:

+------------------------------+
| plan                         |
+------------------------------+
| Filter                       |
|  ├─ test.c0 IS NULL          |
|  └─ IndexedTableAccess(test) |
|      ├─ index: [test.pk]     |
|      ├─ filters: [{(1, ∞)}]  |
|      └─ columns: [pk c0]     |
+------------------------------+

We should be able to use idx1 to implement the query with a single indexed table access, no filter required. And if the query uses a non-NULL value for c0, we do. But whenc0 is being compared to NULL, the engine won't pick the secondary index.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions