数据库索引有什么好处?请给出具体例子
以mysql为例子。 表结构:
mysql> desc demo;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | | |
| name | varchar(10) | YES | MUL | NULL | |
+-------+-------------+------+-----+---------+-------+
索引状况:
show index in demo;
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+- --------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+- --------+
| demo | 0 | PRIMARY | 1 | id | A | 4 | NULL | NULL | | BTREE | |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+- --------+
查询计划:
explain select * from demo where name like '李%';
+----+-------------+-------+------+---------------+------+---------+------+- -----+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+------+- -----+-------------+
| 1 | SIMPLE | demo | ALL | NULL | NULL | NULL | NULL | 4 | Using where |
+----+-------------+-------+------+---------------+------+---------+------+- -----+-------------+
[color=violet]注意到没有使用任何索引.[/color]
添加索引:
create index name_index_demo on demo;
现在的索引状况:
show index in demo;
+-------+------------+-----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+-------+------------+-----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| demo | 0 | PRIMARY | 1 | id | A | 4 | NULL | NULL | | BTREE | |
| demo | 1 | name_index_demo | 1 | name | A | NULL | NULL | NULL | YES | BTREE | |
+-------+------------+-----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
新的查询的查询计划:
explain select * from demo where name like '李%';
+----+-------------+-------+-------+-----------------+-----------------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | [color=red]key[/color] | [color=red]key_len[/color] | ref | rows | Extra |
+----+-------------+-------+-------+-----------------+-----------------+---------+------+------+-------------+
| 1 | SIMPLE | demo | range | name_index_demo | [color=red]name_index_demo[/color] | [color=red]13[/color] | NULL | 1 | Using where |
+----+-------------+-------+-------+-----------------+-----------------+---------+------+------+-------------+
1 row in set (0.03 sec)
[color=violet]注意到查询已经使用了我们创建的索引.[/color]
可以提高查询速度 不过相应的会使存储变大
经常修改的列不要用索引
每次更新数据(Insert or Update),因为有索引存在,所以就会去更性索引,如果原来表的数据量很大,更新索引的系统开销,可能会大到无法接受的程度