如何在 MySQL 中使用基于函数的索引?
在 MySQL 5.6 之前的版本中,无法实现基于函数的索引。首先,要在 MySQL 中创建基于函数的索引,我们需要创建一个表。
mysql> create table FunctionIndexDemo - > ( - > FirstName varchar(100) - > ); Query OK, 0 rows affected (0.70 sec)
让我们看看创建基于函数索引的语法。
create index index_name on yourTableName (column_name(IntegerSize));
以下是查询。
mysql> create index indFirstName on FunctionIndexDemo (FirstName(6)); Query OK, 0 rows affected (0.56 sec) Records: 0 Duplicates: 0 Warnings: 0
要检查索引是否存在。
mysql> SHOW INDEX FROM FunctionIndexDemo;
以下是输出。
+-------------------+------------+--------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+ | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible | +-------------------+------------+--------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+ | functionindexdemo | 1 | indFirstName | 1 | FirstName | A | 0 | 6 | NULL | YES | BTREE | | | YES | +-------------------+------------+--------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+ 1 row in set (0.24 sec)
广告