MySQL generated columns: index values that are computed or inside JSON
A query that filters on LOWER(email), on DATE(created_at) or on a key inside a JSON document cannot use a normal index: the index stores the column, not the result of the expression. A generated column stores (or computes) that expression as a column of its own, and that column can be indexed. The query becomes an index lookup instead of a full table scan.
Problem: A plugin table wp_ha_products_sync keeps the data from a supplier feed as JSON. A report filters by brand with WHERE payload LIKE '%"brand":"Acme"%', which reads all 400,000 rows, matches wrong rows (“Acme Pro”) and gets slower every week.
Solution: Add a VIRTUAL generated column that extracts the brand with JSON_UNQUOTE(JSON_EXTRACT(payload, '$.brand')), index it, and query the column instead of the JSON; in MySQL 8.0.13+ a functional index does the same without a visible column.
A virtual column with an index
ALTER TABLE wp_ha_products_sync
ADD COLUMN brand VARCHAR(100)
AS (JSON_UNQUOTE(JSON_EXTRACT(payload, '$.brand'))) VIRTUAL,
ADD INDEX brand (brand);
SELECT sku, brand
FROM wp_ha_products_sync
WHERE brand = 'Acme';
EXPLAIN now shows key: brand and a handful of rows instead of a full scan. The comparison is exact, so “Acme Pro” no longer matches.
- VIRTUAL columns are computed when a row is read and take no space in the table, but their index is stored and maintained on every insert and update, like any index.
- STORED columns are computed on write and saved in the row. Use them when the expression is expensive and read often, or when the column must be part of the primary key.
The expression must be deterministic: functions like NOW() or RAND() are not allowed. Inserts and updates must not write to a generated column; list the other columns explicitly in INSERT statements.
Case-insensitive and date lookups
ALTER TABLE wp_ha_leads
ADD COLUMN email_lc VARCHAR(191) AS (LOWER(email)) VIRTUAL,
ADD INDEX email_lc (email_lc),
ADD COLUMN created_day DATE AS (DATE(created_at)) VIRTUAL,
ADD INDEX created_day (created_day);
SELECT id FROM wp_ha_leads WHERE email_lc = LOWER('Anna@Example.com');
SELECT COUNT(*) FROM wp_ha_leads WHERE created_day = '2025-05-19';
For the date, a range on the original column (created_at >= '2025-05-19' AND created_at < '2025-05-20') already uses an index on created_at; the generated column helps when the reports are written with DATE() and cannot easily be changed.
Functional indexes in MySQL 8.0.13+
CREATE INDEX brand_fn ON wp_ha_products_sync ((CAST(payload->>'$.brand' AS CHAR(100)) COLLATE utf8mb4_bin)); SELECT sku FROM wp_ha_products_sync WHERE CAST(payload->>'$.brand' AS CHAR(100)) COLLATE utf8mb4_bin = 'Acme';
The double parentheses make it an expression index; MySQL creates a hidden virtual column internally. The query must use exactly the same expression to match the index, which is easy to get wrong, so a named generated column is often easier to maintain. MariaDB has no functional indexes but supports indexed virtual columns since 10.2, so the column approach works on both.
In a plugin
Generated columns confuse dbDelta(), which compares column definitions as text and tries to “fix” them on every run. Add them once with a version check and $wpdb->query(), as in a JSON column for flexible data in a plugin table. For core tables like wp_postmeta, do not add columns: WordPress and other plugins do not expect them, and core updates may alter these tables.
NOTE: Adding a VIRTUAL column is an instant metadata change in MySQL 8, but building its index reads the whole table; on large tables run it outside peak hours. Whether a query uses the new index: EXPLAIN for slow MySQL queries.