如何在 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)

更新於: 30-Jul-2019

202 次瀏覽

開啟你的職業生涯

完成課程,獲得認證

開始學習
廣告
© . All rights reserved.