SyntaxStudy
Sign Up
MySQL Indexing JSON with Generated Columns
MySQL Advanced 5 min read

Indexing JSON with Generated Columns

Generated Columns for JSON Indexes

You cannot index a JSON column directly, but you can create a generated column from a JSON path and index that.

Example
ALTER TABLE products
  ADD COLUMN ram INT
    GENERATED ALWAYS AS (attributes->'$.ram') STORED,
  ADD INDEX idx_ram (ram);

-- Now this query uses the index
SELECT name FROM products WHERE ram = 16;
Pro Tip

STORED generated columns use disk space; VIRTUAL are recomputed on read.