MySQL Generated Columns and Multi-Valued Indexes for JSON

MySQL 55 min min read Updated: Nov 27, 2026 Advanced
MySQL Generated Columns and Multi-Valued Indexes for JSON
Advanced Topic 2 of 3

Generated Columns

A generated column is calculated from other columns. You never write to it. Two kinds:

KindStored on disk?Cost
VIRTUAL (default)No, computed when readNo extra space, small CPU cost
STOREDYes, computed on writeUses space, read is a plain lookup
sql CREATE TABLE line_items ( id INT PRIMARY KEY, qty INT NOT NULL, unit_price DECIMAL(10,2) NOT NULL, total DECIMAL(12,2) AS (qty * unit_price) STORED, label VARCHAR(30) AS (CONCAT('Qty ', qty)) VIRTUAL ); INSERT INTO line_items (id, qty, unit_price) VALUES (1, 3, 9.99), (2, 10, 2.50); UPDATE line_items SET qty = 4 WHERE id = 1; SELECT id, qty, unit_price, total, label FROM line_items ORDER BY id;

Result:

idqtyunit_pricetotallabel
149.9939.96Qty 4
2102.5025.00Qty 10
sql CREATE TABLE line_items (id INT PRIMARY KEY, qty INT, unit_price DECIMAL(10,2), total DECIMAL(12,2) AS (qty * unit_price) STORED); INSERT INTO line_items VALUES (1, 3, 9.99, 50);
Error

ERROR 3105: The value specified for generated column 'total' in table 'line_items' is not allowed.

Index a Computed Value

You cannot index a JSON path directly, but you can expose it as a generated column and index that:

sql CREATE TABLE events (id INT PRIMARY KEY, payload JSON); INSERT INTO events WITH RECURSIVE n AS (SELECT 1 AS g UNION ALL SELECT g + 1 FROM n WHERE g < 20000) SELECT g, JSON_OBJECT('user', g % 2000, 'type', ELT(1 + g % 4, 'click', 'view', 'buy', 'share')) FROM n; SELECT COUNT(*) AS rows_loaded FROM events;

Result:

rows_loaded
20000

Without an index, a filter on payload->>'$.user' reads everything:

sql EXPLAIN SELECT id FROM events WHERE payload->>'$.user' = '77';

MySQL plan:

tabletypekeyrowsExtra
eventsALLNULL19782Using where

Add a virtual generated column and an index on it:

sql EXPLAIN SELECT id FROM events WHERE user_id = 77;

MySQL plan:

tabletypekeyrowsExtra
eventsrefidx_events_user10Using index

Functional Indexes (8.0.13+)

A functional index puts an expression in parentheses and creates the hidden generated column for you. The query must use the same expression:

sql EXPLAIN SELECT id FROM events WHERE CAST(payload->>'$.user' AS UNSIGNED) = 77;

MySQL plan:

tabletypekeyrowsExtra
eventsrefidx_events_user_fn10NULL

Multi-Valued Indexes (8.0.17+)

To search inside a JSON array, MySQL can index every element of the array. Use MEMBER OF, JSON_CONTAINS or JSON_OVERLAPS in the query:

sql CREATE TABLE articles (id INT PRIMARY KEY, title VARCHAR(30), tags JSON, INDEX idx_tags ((CAST(tags AS CHAR(20) ARRAY)))); INSERT INTO articles VALUES (1, 'MySQL tips', '["db", "sql"]'), (2, 'Go intro', '["go", "backend"]'), (3, 'SQL joins', '["sql", "joins"]'); SELECT id, title FROM articles WHERE 'sql' MEMBER OF (tags) ORDER BY id;

Result:

idtitle
1MySQL tips
3SQL joins
sql CREATE TABLE articles (id INT PRIMARY KEY, title VARCHAR(30), tags JSON, INDEX idx_tags ((CAST(tags AS CHAR(20) ARRAY)))); INSERT INTO articles VALUES (1, 'MySQL tips', '["db", "sql"]'), (2, 'Go intro', '["go", "backend"]'), (3, 'SQL joins', '["sql", "joins"]'); SELECT id, title FROM articles WHERE JSON_OVERLAPS(tags, '["go", "joins"]') ORDER BY id;

Result:

idtitle
2Go intro
3SQL joins

Prove the index is used on a larger table:

sql EXPLAIN SELECT id FROM docs WHERE 'tag77' MEMBER OF (tags);

MySQL plan:

tabletypekeyrowsExtra
docsrefidx_doc_tags80Using where

When to Use Which

NeedChoose
Fast filter on one JSON fieldGenerated column + index, or functional index
Search inside a JSON arrayMulti-valued index with MEMBER OF
Frequently read computed valueSTORED generated column
Rarely read, save spaceVIRTUAL generated column

Common Beginner Mistakes

MistakeWhat happensFix
Writing a value to a generated columnError 3105Change the source columns
Query expression differs from the index expressionIndex ignoredUse exactly the same expression, including CAST
LIKE on JSON text expecting an indexFull scanUse MEMBER OF with a multi-valued index
Using multi-valued index on MySQL 8.0.16 or olderSyntax errorUpgrade to 8.0.17+

Practice

  1. Add a stored generated column full_name from first and last names.
  2. Index a JSON field of your choice and confirm with EXPLAIN.
  3. Create a multi-valued index on a tags array and query with MEMBER OF.
  4. Compare VIRTUAL and STORED for a frequently read value.

Conclusion

Generated columns expose computed values that you can index. Functional indexes do it in one step, and multi-valued indexes make JSON array searches fast with MEMBER OF.

Quick Summary: generated column, VIRTUAL, STORED, functional index, multi-valued index, MEMBER OF.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators