mysql查询字段数据不重复distinct及distinct的多字段操作
来源:互联网 发布:mmm互助 asp.net源码 编辑:程序博客网 时间:2024/05/08 18:37
转载自 【http://www.jb51.net/article/77997.htm】
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
- mysql查询字段数据不重复distinct及distinct的多字段操作
- distinct 多字段去重复
- distinct 多字段去重复
- Hibernate 多字段distinct查询
- MySql中distinct的用法 查询出某个字段不重复的记录
- mysql查询不重复的记录总数distinct用法
- mysql查询不重复的记录总数distinct用法
- SQL DISTINCT 多字段查询用法
- MYSQL的group by和多字段的distinct
- mysql distinct 对多字段过滤不正确的解决方法
- 使用distinct查询不重复的行
- 选择不重复的数据SELECT DISTINCT
- mysql查询不重复的行内容,不重复的记录数.count,distinct
- (3万错误,NO.3),mysql中查询数据用distinct去重复并且查多个字段
- Mysql| distinct去除重复的结果(DISTINCT)
- distinct使SQL查询除去重复的字段
- distinct使SQL查询除去重复的字段
- distinct使SQL查询除去重复的字段
- TLS,SSL,HTTPS with Python
- PDF文章下载
- Greetings
- android Wifi连接
- Oracle使用笔记
- mysql查询字段数据不重复distinct及distinct的多字段操作
- Changes Affecting Upgrades to MySQL 5.7
- hdu 5875 2016 ACM/ICPC Asia Regional Dalian Online 1008
- 深度学习调参备忘
- js中select标签和input标签的value为数字时
- FLASH的读写
- js控制过场动画,setTimeout和clearTimeout的用法以及document.onreadystatechange判断页面是否加载结束
- Spring-8 bean的生命周期
- 长篇大论往往没有demo来的实在