Generated Columns
A generated column is calculated from other columns. You never write to it. Two kinds:
| Kind | Stored on disk? | Cost |
|---|---|---|
VIRTUAL (default) | No, computed when read | No extra space, small CPU cost |
STORED | Yes, computed on write | Uses space, read is a plain lookup |
Result:
| id | qty | unit_price | total | label |
|---|---|---|---|---|
| 1 | 4 | 9.99 | 39.96 | Qty 4 |
| 2 | 10 | 2.50 | 25.00 | Qty 10 |
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:
Result:
| rows_loaded |
|---|
| 20000 |
Without an index, a filter on payload->>'$.user' reads everything:
MySQL plan:
| table | type | key | rows | Extra |
|---|---|---|---|---|
| events | ALL | NULL | 19782 | Using where |
Add a virtual generated column and an index on it:
MySQL plan:
| table | type | key | rows | Extra |
|---|---|---|---|---|
| events | ref | idx_events_user | 10 | Using 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:
MySQL plan:
| table | type | key | rows | Extra |
|---|---|---|---|---|
| events | ref | idx_events_user_fn | 10 | NULL |
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:
Result:
| id | title |
|---|---|
| 1 | MySQL tips |
| 3 | SQL joins |
Result:
| id | title |
|---|---|
| 2 | Go intro |
| 3 | SQL joins |
Prove the index is used on a larger table:
MySQL plan:
| table | type | key | rows | Extra |
|---|---|---|---|---|
| docs | ref | idx_doc_tags | 80 | Using where |
When to Use Which
| Need | Choose |
|---|---|
| Fast filter on one JSON field | Generated column + index, or functional index |
| Search inside a JSON array | Multi-valued index with MEMBER OF |
| Frequently read computed value | STORED generated column |
| Rarely read, save space | VIRTUAL generated column |
Common Beginner Mistakes
| Mistake | What happens | Fix |
|---|---|---|
| Writing a value to a generated column | Error 3105 | Change the source columns |
| Query expression differs from the index expression | Index ignored | Use exactly the same expression, including CAST |
LIKE on JSON text expecting an index | Full scan | Use MEMBER OF with a multi-valued index |
| Using multi-valued index on MySQL 8.0.16 or older | Syntax error | Upgrade to 8.0.17+ |
Practice
- Add a stored generated column
full_namefrom first and last names. - Index a JSON field of your choice and confirm with EXPLAIN.
- Create a multi-valued index on a
tagsarray and query withMEMBER OF. - Compare
VIRTUALandSTOREDfor 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.

