访问原文提升阅读体验: https://www.modb.pro/db/22805?cyn
和其它数据库一样,MySQL索引对表中指定列进行排序后另外保存,用于快速查找具有特定值的行。如果没有索引,必须从第一行开始,读取整个表查找,表越大,成本就越高。如果表中有相关列的索引,就可以快速确定要在数据文件中间查找的位置,而不必查看所有数据,比按顺序读取每一行快得多。
MySQL的PRIMARY KEY索引、UNIQUE索引、普通索引、FULLTEXT索引都使用B-trees存储,Spatial索引使用R-trees存储。
MySQL 5.7中的创建索引语法:
CREATE [UNIQUE | FULLTEXT | SPATIAL] INDEX index_name
[index_type]
ON tbl_name (key_part,…)
[index_option]
[algorithm_option | lock_option] …key_part:
col_name [(length)] [ASC | DESC]index_option:
KEY_BLOCK_SIZE [=] value
| index_type
| WITH PARSER parser_name
| COMMENT ‘string’index_type:
USING {BTREE | HASH}algorithm_option:
ALGORITHM [=] {DEFAULT | INPLACE | COPY}lock_option:
LOCK [=] {DEFAULT | NONE | SHARED | EXCLUSIVE}
也可以使用alter table语句来创建索引。
MySQL支持前缀索引,即对索引字段的前N个字符创建索引。
访问原文提升阅读体验: https://www.modb.pro/db/22805?cyn
root@database-one 21:56: [gftest]> select * from emp;+--------+------+---------+------------+--------+| ename | age | sal | hiredate | deptno |+--------+------+---------+------------+--------+| 郭军 | 27 | 8400.00 | 2019-12-08 | 10 || 刘杰 | 30 | 9100.00 | 2018-04-09 | 10 || 王艳 | 24 | 6000.00 | 2020-01-05 | 20 || 马丽 | 26 | 7200.00 | 2018-07-06 | 30 || 肖伟 | 29 | 8700.00 | 2017-05-28 | 30 |+--------+------+---------+------------+--------+5 rows in set (0.02 sec)root@database-one 21:57: [gftest]> show index from emp \GEmpty set (0.01 sec)root@database-one 21:57: [gftest]> create index idx_emp_ename on emp(ename(2));Query OK, 0 rows affected (0.09 sec)Records: 0 Duplicates: 0 Warnings: 0root@database-one 21:57: [gftest]> show index from emp \G*************************** 1. row ***************************Table: empNon_unique: 1Key_name: idx_emp_enameSeq_in_index: 1Column_name: enameCollation: ACardinality: 5Sub_part: 2Packed: NULLNull: YESIndex_type: BTREEComment:Index_comment:1 row in set (0.00 sec)root@database-one 21:57: [gftest]> explain select * from emp where ename like '王%';+----+-------------+-------+------------+-------+---------------+---------------+---------+------+------+----------+-------------+| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |+----+-------------+-------+------------+-------+---------------+---------------+---------+------+------+----------+-------------+| 1 | SIMPLE | emp | NULL | range | idx_emp_ename | idx_emp_ename | 9 | NULL | 1 | 100.00 | Using where |+----+-------------+-------+------------+-------+---------------+---------------+---------+------+------+----------+-------------+1 row in set, 1 warning (0.07 sec)root@database-one 21:58: [gftest]> explain select * from emp where sal>6000;+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+| 1 | SIMPLE | emp | NULL | ALL | NULL | NULL | NULL | NULL | 5 | 33.33 | Using where |+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+1 row in set, 1 warning (0.03 sec)
可以看到,创建的索引是BTREE类型,并且在按照ename查询时被使用。
索引使用总有一些原则:
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29715045/viewspace-2681643/ ,如需转载,请注明出处,否则将追究法律责任。