当前位置: 代码网 > it编程>数据库>Mysql > 详解MySQL中DISTINCT去重的核心注意事项

详解MySQL中DISTINCT去重的核心注意事项

2025年06月18日 Mysql 我要评论
distinct 六大注意事项1. 作用范围:所有 select 字段select distinct a, b from table; -- 对(a,b)组合整体去重误以为只作用于第一个字段:--

distinct 六大注意事项

1. 作用范围:所有 select 字段

select distinct a, b from table;  -- 对(a,b)组合整体去重

误以为只作用于第一个字段:

-- 错误理解:以为只对name去重
select distinct name, class from students;  

实际效果:对 (name, class) 组合去重(如 ('张三','一班') 和 ('张三','二班') 算不同记录)

2. null 值的特殊处理

insert into students (name, class, score) values (null, '三班', 90);

​​​​​​​select distinct name from students;

结果:

+--------+
| name   |
+--------+
| 张三   |
| 李四   |
| 王五   |
| null   |  -- null被视为独立值保留
+--------+

3. 性能陷阱(大数据量)

-- 当表有百万行时慎用
select distinct text_column from huge_table; 

优化方案:

-- 先通过where缩小范围再去重
select distinct text_column 
from huge_table 
where create_time > '2023-01-01';

-- 或添加索引(对text类型有限制)
alter table huge_table add index idx_text(text_column(20)); -- 前缀索引

4. 与 order by 的优先级

select distinct class 
from students 
order by score desc; -- 错误!score不在select中

正确写法:

-- 方案1:排序字段必须在select中
select distinct class, max(score) as max_score 
from students 
group by class 
order by max_score desc;

-- 方案2:子查询
select distinct class from (
  select class, score from students order by score desc
) as tmp;

5. 聚合函数中的 distinct

-- 统计不重复的班级数量
select count(distinct class) from students; 

-- 错误用法(语法无效):
select distinct count(class) from students; 

6. 不可用于部分字段计算

-- 尝试计算不同班级的平均分(错误!)
select distinct class, avg(score) from students; 

正确做法:必须配合 group by

select class, avg(score) 
from students 
group by class;  -- 这才是标准解法

高级注意点

7. 与 limit 的配合问题

select distinct class from students limit 2;

结果不确定性:

返回的 2 条记录是随机的(除非指定 order by),不同执行可能结果不同。

8. 临时表空间占用

distinct 操作会在内存/磁盘创建临时表存储唯一值

当去重字段总数据量超过 tmp_table_size 时,性能急剧下降

查看阈值:

show variables like 'tmp_table_size'; -- 默认16mb

对比 group by 去重

特性distinctgroup by
是否可搭配聚合函数✅ (如sum/avg)
结果排序无序可按分组键排序
执行效率简单场景更快复杂聚合时更优
索引利用可使用索引必须用分组字段索引

最佳实践总结

小数据量:直接 distinct 简洁高效

需要聚合计算:用 group by 替代

精确去重计数:优先 count(distinct column)

排序需求:必须显式写 order by

超大数据:先过滤再去重 + 合理索引

实战检验

订单表 orders 结构:

create table orders (
    id int primary key,
    product_id int,
    user_id int,
    amount decimal(10,2),
    coupon_code varchar(20) -- 允许为null
);

问题:

如何高效获取使用过不同优惠券的用户id列表(含null)?

写出你的解决方案:

select _______________________________
from orders;

答案(折叠):

-- 方案1:基础写法
select distinct user_id, coupon_code 
from orders 
where coupon_code is not null; -- 若需包含null则去掉where

​​​​​​​-- 方案2:大数据量优化(添加联合索引)
alter table orders add index idx_user_coupon(user_id, coupon_code);
select distinct user_id, coupon_code from orders;

到此这篇关于详解mysql中distinct去重的核心注意事项的文章就介绍到这了,更多相关mysql distinct去重内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com