Mysql数据库(十)创建索引
表order_info
1、show indexes from order_info;查看表的索引,从表中可以看出只有一个索引,这个索引是主键(primary key) id
2、从表中找到状态是 no_completed 的两条数据
3、explain select * from order_info where status='no_completed';在语句前,加上explain 查看寻找过程,这里可以看到rows=8,说明这条语句查询了数据库中的全部8条记录。
4、创建索引
create index index_status on order_info(status);
Query OK, 0 rows affected (0.27 sec)
Records: 0 Duplicates: 0 Warnings: 0
5、再次查看查询过程,rows=2 ,只需要查询两条记录。
6、查看表的索引,已经有两条索引了。可以看到新建的索引index_status
7、 drop index index_status on order_info;删除索引
drop index index_status on order_info;
Query OK, 0 rows affected (0.13 sec)
Records: 0 Duplicates: 0 Warnings: 0