opt: Index isn't always used in a SELECT query with a JSONB column
A user ran a simple SELECT in their table with a simple WHERE query and is seeing slow performance. Replicating the issue shows that having a JSONB column in the table is a correlating factor to the issue with performance. An EXPLAIN shows that the Index that should be allocated to be used in the SELECT is not always used. Even if WHERE does not contain the JSONB column, having this JSONB column in the selected fields makes cockroach ignore the other indexes. Please see reproduction steps for further details.
To Reproduce
- Setup cockroach cluster using v19.1.4
- Below we will create a table
foowith an index configured on columntimestamp1
root@:26257/foobar> CREATE TABLE foo (
timestamp1 TIMESTAMPTZ,
INDEX ("timestamp1" ASC)
);
CREATE TABLE
Time: 5.973296ms
root@:26257/foobar> EXPLAIN SELECT * FROM foo WHERE timestamp1 > '2019-09-30 09:09:42.910767+00:00'::timestamptz;
tree | field | description
+------+-------+-------------------------------+
scan | |
| table | foo@foo_timestamp1_idx
| spans | /2019-09-30T09:09:42.910768Z-
(3 rows)
Time: 6.523031ms
The foo_timestamp1_idx index is used in this example. However, if we configure another table bar with a JSONB column, data, the behavior is different.
root@:26257/foobar> CREATE TABLE bar (
timestamp2 TIMESTAMPTZ,
data JSONB,
INDEX ("timestamp2" ASC)
);
CREATE TABLE
Time: 6.167085ms
root@:26257/foobar> EXPLAIN SELECT * FROM bar WHERE timestamp2 > '2019-09-30 09:09:42.910767+00:00'::timestamptz;
tree | field | description
+------+--------+-------------------------------------------------+
scan | |
| table | bar@primary
| spans | ALL
| filter | timestamp2 > '2019-09-30 09:09:42.910767+00:00'
(4 rows)
Time: 6.978583ms
We can see that instead of the expected foo@foo_timestamp1_idx, we are doing a scan on the primary value, and thus the span is ALL. Even if we try to force the index:
root@:26257/foobar> EXPLAIN SELECT * FROM bar@{FORCE_INDEX=bar_timestamp2_idx,DESC} WHERE timestamp2 > '2019-09-30 09:09:42.910767+00:00'::timestamptz;
tree | field | description
+--------------+-------+-------------------------------+
index-join | |
│ | table | bar@primary
└── revscan | |
| table | bar@bar_timestamp2_idx
| spans | /2019-09-30T09:09:42.910768Z-
(5 rows)
Time: 929.051µs
We see that an index-join is performed on the primary keys for our table bar. If we perform a select on the TIMESTAMPZ column:
root@:26257/foobar> EXPLAIN SELECT timestamp2 FROM bar WHERE timestamp2 > '2019-09-30 09:09:42.910767+00:00'::timestamptz;
tree | field | description
+------+-------+-------------------------------+
scan | |
| table | bar@bar_timestamp2_idx
| spans | /2019-09-30T09:09:42.910768Z-
(3 rows)
Time: 1.058583ms```
We see that the index is used, as expected.
**Expected behavior**
The index configured should be used, even when JSONB column is included in the output.
**Environment:**
- CockroachDB version v19.1.4
- Server OS: Ubuntu
- Client app: cockroach sql
**Additional context**
Very slow performance on SELECT queries.
To clarify, SELECT * behavior is changed based on the number of columns being read. If all the data is contained inside an index, then it will simply use the index available. If there is a new column that is not a part of the same index, then the optimizer will perform a full scan on the table to display the correct information.