Mysql必读MySQL中索引优化distinct语句及distinct的多字段 *** 作

Mysql必读MySQL中索引优化distinct语句及distinct的多字段 *** 作,第1张

概述介绍《Mysql必读MySQL中索引优化distinct语句及distinct的多字段 *** 作》开发教程,希望对您有用。

《MysqL必读MysqL中索引优化distinct语句及distinct的多字段 *** 作》要点:
本文介绍了MysqL必读MysqL中索引优化distinct语句及distinct的多字段 *** 作,希望对您有用。如果有疑问,可以联系我们。

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

 MysqL入门

实例1 使用索引优化disTINCT *** 作MysqL入门

create table m11 (a int,b int,c int,d int,primary key(a)) engine=INNODB;insert into m11 values (1,1,1),(2,2,2),(3,3,3),(4,4,4),(5,5,5),(6,6,6),(7,7,7),(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 |+----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+-------------+MysqL入门


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

2 这是使用索引优化disTINCT *** 作的典型实例.MysqL入门

 MysqL入门

实例2 使用索引不能优化disTINCT *** 作MysqL入门

create table m31 (a int,primary key(a)) engine=MEMORY;insert into m31 values (1,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 |+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+MysqL入门



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

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

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

 MysqL入门

实例3 使用索引可以优化disTINCT *** 作的Memory表MysqL入门

create table m33 (a int,INDEX USING BTREE (a)) engine=MEMORY;insert into m33 values (1,8);explain select distinct(a) from m33;

 MysqL入门

 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 *** 作.MysqL入门

2 对比实例2,可以发现,二者都使用了Memory引擎. 但实例3指名使用Btree类型的索引.MysqL入门

3 实例2没有指定使用什么类型的索引,MysqL将采用默认值. 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.MysqL入门

 MysqL入门

结论:MysqL入门

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

2 HASH索引适合等值查找,但不适合需要有序的场景,而Btree却适合有序的场景.MysqL入门

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

 MysqL入门

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

但disTINCT只能对一个字段有效,比如:MysqL入门

sql="select disTINCT Title from table where ID>0"

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

sql="select disTINCT Title,posttime from table where ID>0" 

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

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

写法一:MysqL入门

sql = "Select disTINCT(Title),posttime From table1 Where ID>0"

写法二:MysqL入门

sql = "Select Title,posttime From table1 Where ID>0 group by Title,posttime"

写法三:MysqL入门

sql="select Title,posttime from table where ID in (select min(ID) from table group by Title)"
总结

以上是内存溢出为你收集整理的Mysql必读MySQL中索引优化distinct语句及distinct的多字段 *** 作全部内容,希望文章能够帮你解决Mysql必读MySQL中索引优化distinct语句及distinct的多字段 *** 作所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/sjk/1164042.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-01
下一篇 2022-06-01

发表评论

登录后才能评论

评论列表(0条)

保存