For indexing values stored in JSON
, use a stored generated column.
For example, to index title
and category
of
{"title": "Some Title", "category": "Some Category", "url": "...", ...}
use something like:
CREATE TABLE listings (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) AS
(data->>'$.title') STORED,
category VARCHAR(255) AS
(data->>'$.category') STORED,
data JSON NOT NULL,
KEY (title), -- index title
KEY (category), -- index category
KEY (title, category) -- composite index of title & category
);
Read more about it in my article MySQL as smart JSON storage :-)
CAST( JSON_UNQUOTE( JSON_EXTRACT(json_doc, path) ) AS type)
but I don't think I can do aINDEX i1 ( CAST( JSON_UNQUOTE( JSON_EXTRACT(json_doc, path) ) AS type) )
. Idk. All these questions because I am too lazy to spin up a test environment. – Yates