I am using JSON
columns a lot in MySQL (version 8.0.35). Usually, I create functional indexes on JSON properties to speed up queries. According to the MySQL docs, Functional indexes are implemented as hidden virtual generated columns. However, they seem to behave differently from indexes on generated columns in joins. I will use a somehow constructed example to explain my point.
Example: Given are two tables product
and purchase
. purchase
has a JSON property $.productUuid
that references a product
.
CREATE TABLE IF NOT EXISTS product (
id BINARY(16) NOT NULL,
payload JSON NOT NULL,
CONSTRAINT pk_product PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
CREATE TABLE IF NOT EXISTS purchase (
id BINARY(16) NOT NULL,
payload JSON NOT NULL,
CONSTRAINT pk_product PRIMARY KEY (id),
INDEX `i_purchase_product` (
( CAST(payload->>'$.productUuid' AS CHAR(36)) COLLATE utf8mb4_bin )
)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
INSERT INTO product (id, payload)
VALUES
( UUID_TO_BIN(UUID(), TRUE), '{ "name": "random drink" }' ),
( UUID_TO_BIN(UUID(), TRUE), '{ "name": "random dish" }' ),
( UUID_TO_BIN(UUID(), TRUE), '{ "name": "random tool" }' )
;
INSERT INTO purchase (id, payload)
SELECT
UUID_TO_BIN(UUID(), TRUE), JSON_SET(payload, '$.productUuid', BIN_TO_UUID(id))
FROM product
;
For the following query
SELECT * FROM product a INNER JOIN purchase b ON BIN_TO_UUID(a.id) = b.payload->>'$.productUuid';
MySQL generates the following plan:
+----+-------------+-------+---------------+------+--------------------------------------------+
| id | select_type | table | possible_keys | key | Extra |
+----+-------------+-------+---------------+------+--------------------------------------------+
| 1 | SIMPLE | a | NULL | NULL | NULL |
| 1 | SIMPLE | b | NULL | NULL | Using where; Using join buffer (hash join) |
+----+-------------+-------+---------------+------+--------------------------------------------+
The plan shows that the functional index is not even considered. The case is different if I create the table with a generated column and a regular index on it.
CREATE TABLE IF NOT EXISTS purchase (
id BINARY(16) NOT NULL,
payload JSON NOT NULL,
product_uuid VARCHAR(36) GENERATED ALWAYS AS (payload->>'$.productUuid') STORED NOT NULL,
CONSTRAINT pk_purchase PRIMARY KEY (id),
INDEX `i_purchase_product` (product_uuid)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
Now, the plan generated by MySQL is showing that the index is considered:
+----+-------------+-------+--------------------+--------------------+-----------------------+
| id | select_type | table | possible_keys | key | Extra |
+----+-------------+-------+--------------------+--------------------+-----------------------+
| 1 | SIMPLE | a | NULL | NULL | NULL |
| 1 | SIMPLE | b | i_purchase_product | i_purchase_product | Using index condition |
+----+-------------+-------+--------------------+--------------------+-----------------------+
Is there an explanation, preferrably documented, for this behavior?
CAST()
in the functional index? That's not the same as the generated column.CAST()
is required asJSON_UNQUOTE()
(->>
is shorthand forJSON_UNQUOTE(JSON_EXTRACT(...))
) returns data typeLONGTEXT
and MySQL cannot index those without specifying a prefix length (Functional Key Parts). It raises error code 3757.