MariaDB can we create a functional index (index based on a function)

MariaDB can we create a functional index ( index based on a function )

我可以像在 Oracle 中一样在 MariaDB 中创建功能索引吗?

create index fnc_idx on table_name (to_char(date_col, 'MM-YYYY'));

或任何替代解决方案...? 问候

您可以使用计算列作为方法:

CREATE TABLE `orders` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `order_date` datetime NOT NULL,
  -- add calculated column
  `order_month` VARCHAR(6) AS (CONCAT(MONTH(order_date), YEAR(order_date))) PERSISTENT,
  `client_id` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  -- add calculated column index
  INDEX (`order_month`)
);

MariaDB fiddle