数据库索引创建

通常在查询的时候数据量大的话会非常慢,因此用到了数据库索引,一般使用用普通索引就已经基本上满足了需要,

数据库创建索引

1、普通索引创建

mysql> create index idx_userinfo on userinfo(JobType);

或者是创建多列的组合索引:

mysql> create index idx_userinfo on userinfo(TelePhone,FullName,UserId,JobType);

 

2、使用索引对比:

例子:查询某个表的数据并且去重,然后存储到临时表中;

创建索引前:

mysql> create table userinfo_tmp as select DISTINCT TelePhone,FullName,UserId,JobType from userinfo where length(JobType)=6;
Query OK, 160265 rows affected (11 min 10.06 sec)
Records: 160265  Duplicates: 0  Warnings: 0

创建组合索引后:

mysql> create table userinfo_tmp as select DISTINCT TelePhone,FullName,UserId,JobType from userinfo where length(JobType)=6;
Query OK, 160265 rows affected (7.76 sec)
Records: 160265  Duplicates: 0  Warnings: 0

从上面可以看到索引的效率,两者相差了差不多100倍,

网上很多人都说分组去重等不会用到索引,实际上是不完全正确的,上面的例子中也有使用distinct去重,完全是可以使用到索引的!

 

 

你可能感兴趣的:(数据库相关)