Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create functional index on mariadb?

Tags:

mariadb

I'm trying to create functional index on mariadb 5.5 like below...

MariaDB [testdb]> create table foo(c1 datetime);
Query OK, 0 rows affected (0.43 sec)

MariaDB [testdb]> alter table foo add c2 varchar(10) AS (date_format(c1, '%Y-%m-%d')); 
Query OK, 0 rows affected (0.22 sec)               
Records: 0  Duplicates: 0  Warnings: 0

MariaDB [testdb]> desc foo;
+-------+-------------+------+-----+---------+---------+
| Field | Type        | Null | Key | Default | Extra   |
+-------+-------------+------+-----+---------+---------+
| c1    | datetime    | YES  |     | NULL    |         |
| c2    | varchar(10) | YES  |     | NULL    | VIRTUAL |
+-------+-------------+------+-----+---------+---------+
2 rows in set (0.03 sec)

MariaDB [testdb]> create index idx_foo on foo(c1, c2);
ERROR 1904 (HY000): Key/Index cannot be defined on a non-stored computed column
MariaDB [testdb]> 

Is there any how to create compositional column based index with VIRUAL column?

Any advice would be appreciated.

Thanks in advance.

like image 596
KIM Avatar asked Sep 01 '25 10:09

KIM


1 Answers

See documentation:

Generated (Virtual and Persistent/Stored) Columns

...

MariaDB until 10.2.2

In MariaDB 10.2.2 and before, the following statements apply to indexes for generated columns:

  • Defining indexes on VIRTUAL generated columns is not supported.
  • ...

...

like image 124
wchiquito Avatar answered Sep 06 '25 02:09

wchiquito