Repository navigation
Description
Environment
- MatrixOne main@HEAD (c6683bd), 3-CN local cluster
- Table: 100k rows with a JSON column
Reproduction
CREATE TABLE jbig (id INT PRIMARY KEY, j JSON);
-- insert 100k rows, each like {"user": {"id": N, ...}}
-- The only way to index a JSON path (functional index is rejected, see below):
ALTER TABLE jbig ADD COLUMN uid INT AS (JSON_EXTRACT(j, '$.user.id'));
CREATE INDEX jx ON jbig(uid);
ANALYZE TABLE jbig;Query A — filter on the generated column:
SELECT COUNT(*) FROM jbig WHERE uid = 42;
-- 2.3 ms, index scanQuery B — semantically identical filter, expressed on the JSON path:
SELECT COUNT(*) FROM jbig WHERE JSON_EXTRACT(j, '$.user.id') = 42;
-- 96 ms, **41x slower**Evidence
EXPLAIN for query B shows a plain Table Scan with outcnt=100000, selectivity=1.0 — the planner did not even attempt to bind the predicate to the existing jx index, let alone compute a selectivity.
The two queries are exactly equivalent (the generated column uid is defined as that very expression), yet only one syntax gets the index.
This is a documented MySQL 8.0 optimization: JSON-path predicates are matched against generated-columns indexes created over the same expression (https://dev.mysql.com/doc/refman/8.0/en/create-table-secondary-indexes.html#json-table-index).
Compounding factor
Functional indexes that would make this automatic are also unavailable:
CREATE INDEX jx2 ON jbig((CAST(JSON_EXTRACT(j, '$.user.id') AS INT)));
-- err 20105, session-independent (pkg/sql/plan/function/functional_index.go:246)So users are forced into the manual generated-column route — and then the planner refuses to match the predicate written the natural JSON way. Any query rewrite by ORMs / BI tools (which almost always emit the JSON_EXTRACT form) silently degrades to a full scan on an indexed column.
Expected behavior
Match JSON_EXTRACT(col, path) (and the col->>path family) equality/range predicates against generated columns whose defining expression is identical, and use their indexes. MySQL 8.0 semantics.
Suggested fix direction
Planner-side expression normalization: when a filter references JSON_EXTRACT(col, path), search the table's generated columns for an identical expression and substitute the column reference so normal index selection applies.
Activity