当前位置: 移动技术网 > IT编程>数据库>Mysql > MySQL中索引优化distinct语句及distinct的多字段操作

MySQL中索引优化distinct语句及distinct的多字段操作

2017年12月12日  | 移动技术网IT编程  | 我要评论

公务员考试报名网址,普法栏目剧借命下,杨丽坤照片

mysql通常使用groupby(本质上是排序动作)完成distinct操作,如果distinct操作和orderby操作组合使用,通常会用到临时表.这样会影响性能. 在一些情况下,mysql可以使用索引优化distinct操作,但需要活学活用.本文涉及一个不能利用索引完成distinct操作的实例.

 

实例1 使用索引优化distinct操作

create table m11 (a int, b int, c int, d int, primary key(a)) engine=innodb;

insert into m11 values (1,1,1,1),(2,2,2,2),(3,3,3,3),(4,4,4,4),(5,5,5,5),(6,6,6,6),(7,7,7,7),(8,8,8,8);

explain select distinct(a) from m11;

mysql> explain select distinct(a) from m11;

复制代码 代码如下:

+----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+-------------+| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | extra |+----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+-------------+| 1 | simple | m11 | null | index | primary | primary | 4 | null | 1 | 100.00 | using index |+----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+-------------+


说明:
1 'a'列上存在主键索引,mysql可以利用索引(key列值表明使用了主键索引)完成了distinct操作.

2 这是使用索引优化distinct操作的典型实例.

 

实例2 使用索引不能优化distinct操作

create table m31 (a int, b int, c int, d int, primary key(a)) engine=memory;

insert into m31 values (1,1,1,1),(2,2,2,2),(3,3,3,3),(4,4,4,4),(5,5,5,5),(6,6,6,6),(7,7,7,7),(8,8,8,8);

explain select distinct(a) from m31;

 mysql> explain select distinct(a) from m31;

复制代码 代码如下:

+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | extra |+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+| 1 | simple | m31 | null | all | null | null | null | null | 8 | 100.00 | null |+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+



说明:
1 从查询执行计划看,索引没有被使用.

2 对比实例1的建表语句,只是存储引擎不同.

3 为什么主键索引没有起作用? 难道memory存储引擎上的索引不可使用?

 

实例3 使用索引可以优化distinct操作的memory表

create table m33 (a int, b int, c int, d int, index using btree (a)) engine=memory;

insert into m33 values (1,1,1,1),(2,2,2,2),(3,3,3,3),(4,4,4,4),(5,5,5,5),(6,6,6,6),(7,7,7,7),(8,8,8,8);

explain select distinct(a) from m33;

 

 mysql> explain select distinct(a) from m33;

+----+-------------+-------+------------+-------+---------------+------+---------+------+------+----------+-------+| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | extra |+----+-------------+-------+------------+-------+---------------+------+---------+------+------+----------+-------+| 1 | simple | m33 | null | index | null | a | 5 | null | 8 | 100.00 | null |+----+-------------+-------+------------+-------+---------------+------+---------+------+------+----------+-------+
说明:
1 'a'列上存在主键索引,mysql可以利用索引(key列值表明使用了主键索引)完成了distinct操作.

2 对比实例2,可以发现,二者都使用了memory引擎. 但实例3指名使用btree类型的索引.

3 实例2没有指定使用什么类型的索引,mysql将采用默认值. mysql手册上说:

as indicated by the engine name, memory tables are stored in memory. they use hash indexes by default, which makes them very fast for single-value lookups, and very useful for creating temporary tables.

 

结论:

1 看索引对查询的影响,要注意索引的类型.

2 hash索引适合等值查找,但不适合需要有序的场景,而btree却适合有序的场景.

3 看查询执行计划,发现索引没有被使用,需要进一步考察索引的类型.

 

distinct不能选择多个字段的解决方法
在实际应用中,我们经常要选择数据库某表中重复数据,通常我们是使用distinct函数。

但distinct只能对一个字段有效,比如:

sql="select distinct title from table where id>0"

当我们需要列出数据中的另一列,比如:

sql="select distinct title,posttime from table where id>0" 

得出的结果就不是我们想要的了,所以我们需要用另外的方法来解决这个问题。

下面的是我写的sql语句,我不知道是不是很好,但愿有更好的人拿出来分享一下:

写法一:

sql = "select distinct(title),posttime from table1 where id>0"

写法二:

sql = "select title,posttime from table1 where id>0 group by title,posttime"

写法三:

sql="select title,posttime from table where id in (select min(id) from table group by title)"

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网