Understanding MySQL Functional (Expression) Indexes
The article explains MySQL functional (expression) indexes, how they are implemented via generated columns, demonstrates their creation and performance benefits for datetime and JSON fields, compares them with prefix indexes, and offers guidance for older MySQL versions.
Reading font
This article introduces MySQL functional (expression) indexes, which are indexes built on the result of functions, operators, or expressions rather than raw column values.
Functional indexes are implemented using generated (virtual) columns that are computed on‑the‑fly and stored only as index entries. MySQL 8.0.13 added native support; earlier versions require work‑arounds.
Typical use case: indexing parts of a datetime column. By creating an index on date(log_time), a query filtering by date can use the index and reduce scanned rows from the whole table to a single row.
Example DDL and queries demonstrate the performance improvement, showing EXPLAIN output before and after adding the functional index.
<localhost|mysql>show create table t1\G<br/>*************************** 1. row ***************************<br/>Table: t1<br/>Create Table: CREATE TABLE `t1` (<br/> `id` bigint unsigned NOT NULL AUTO_INCREMENT,<br/> `log_time` datetime(6) DEFAULT NULL,<br/> PRIMARY KEY (`id`),<br/> KEY `idx_log_time` (`log_time`)<br/>) ENGINE=InnoDB AUTO_INCREMENT=524268 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci<localhost|mysql>alter table t1 add key idx_func_index_1((date(log_time)));<br/>Query OK, 0 rows affected (2.76 sec)<localhost|mysql>explain select * from t1 where date(log_time) = '2100-02-02'\G<br/>*************************** 1. row ***************************<br/>id: 1<br/>select_type: SIMPLE<br/>table: t1<br/>type: ref<br/>possible_keys: idx_func_index_1<br/>key: idx_func_index_1<br/>key_len: 4<br/>ref: const<br/>rows: 1<br/>filtered: 100.00<br/>Extra: NULLWhen indexing JSON values, the extraction operator ->> returns a longtext, so it must be cast to a suitable type and a collation specified, e.g., cast(r1->>'$.x' as char(1) collate utf8mb4_bin). The article shows creating such an index and the resulting execution plan.
<localhost|mysql>alter table t2 add key idx_func_index_2((cast(r1->>'$.x' as char(1)) collate utf8mb4_bin));<br/>Query OK, 0 rows affected (0.07 sec)<localhost|mysql>explain select * from t2 where r1->>'$.x'='a'\G<br/>*************************** 1. row ***************************<br/>id: 1<br/>select_type: SIMPLE<br/>table: t2<br/>type: ref<br/>possible_keys: idx_func_index_2<br/>key: idx_func_index_2<br/>key_len: 7<br/>ref: const<br/>rows: 1<br/>filtered: 100.00<br/>Extra: NULLThe article also compares functional indexes with prefix indexes, illustrating that they are not interchangeable; functional indexes require the query to use the exact expression defined.
For older MySQL versions, functional indexes can be emulated with virtual columns (5.7) or redundant columns updated manually (5.6/5.5), with recommendations on how to maintain them.
For questions about attribution, corrections or content rights, please contactand we will review it promptly.