当前位置:网站首页>面试突击63:MySQL 中如何去重?
面试突击63:MySQL 中如何去重?
2022-07-06 11:32:00 【王磊】
在 MySQL 中,最常见的去重方法有两个:使用 distinct 或使用 group by,那它们有什么区别呢?接下来我们一起来看。
1.创建测试数据
-- 创建测试表
drop table if exists pageview;
create table pageview(
id bigint primary key auto_increment comment '自增主键',
aid bigint not null comment '文章ID',
uid bigint not null comment '(访问)用户ID',
createtime datetime default now() comment '创建时间'
) default charset='utf8mb4';
-- 添加测试数据
insert into pageview(aid,uid) values(1,1);
insert into pageview(aid,uid) values(1,1);
insert into pageview(aid,uid) values(2,1);
insert into pageview(aid,uid) values(2,2);
最终展现效果如下:
2.distinct 使用
distinct 基本语法如下:
SELECT DISTINCT column_name,column_name FROM table_name;
2.1 单列去重
我们先用 distinct 实现单列去重,根据 aid(文章 ID)去重,具体实现如下:
2.2 多列去重
除了单列去重之外,distinct 还支持多列(两列及以上)去重,我们根据 aid(文章 ID)和 uid(用户 ID)联合去重,具体实现如下:
2.3 聚合函数+去重
使用 distinct + 聚合函数去重,计算 aid 去重之后的总条数,具体实现如下:
3.group by 使用
group by 基础语法如下:
SELECT column_name,column_name FROM table_name
WHERE column_name operator value
GROUP BY column_name
3.1 单列去重
根据 aid(文章 ID)去重,具体实现如下: 与 distinct 相比 group by 可以显示更多的列,而 distinct 只能展示去重的列。
3.2 多列去重
根据 aid(文章 ID)和 uid(用户 ID)联合去重,具体实现如下:
3.3 聚合函数 + group by
统计每个 aid 的总数量,SQL 实现如下: 从上述结果可以看出,使用 group by 和 distinct 加 count 的查询语义是完全不同的,distinct + count 统计的是去重之后的总数量,而 group by + count 统计的是分组之后的每组数据的总数。
4.distinct 和 group by 的区别
官方文档在描述 distinct 时提到:在大多数情况下 distinct 是特殊的 group by,如下图所示: 官方文档地址:https://dev.mysql.com/doc/refman/8.0/en/distinct-optimization.html 但二者还是有一些细微的不同的,比如以下几个。
区别1:查询结果集不同
当使用 distinct 去重时,查询结果集中只有去重列信息,如下图所示: 当你试图添加非去重字段(查询)时,SQL 会报错如下图所示:
而使用 group by 排序可以查询一个或多个字段,如下图所示:
区别2:使用业务场景不同
统计去重之后的总数量需要使用 distinct,而统计分组明细,或在分组明细的基础上添加查询条件时,就得使用 group by 了。 使用 distinct 统计某列去重之后的总数量: 统计分组之后数量大于 2 的文章,就要使用 group by 了,如下图所示:
区别3:性能不同
如果去重的字段有索引,那么 group by 和 distinct 都可以使用索引,此情况它们的性能是相同的;而当去重的字段没有索引时,distinct 的性能就会高于 group by,因为在 MySQL 8.0 之前,group by 有一个隐藏的功能会进行默认的排序,这样就会触发 filesort 从而导致查询性能降低。
总结
大部分场景下 distinct 是特殊的 group by,但二者也有细微的区别,比如它们在查询结果集上、使用的具体业务场景上,以及性能上都是不同的。
参考 & 鸣谢
zhuanlan.zhihu.com/p/384840662
是非审之于己,毁誉听之于人,得失安之于数。
公众号:Java面试真题解析
边栏推荐
- Mysql Information Schema 学习(一)--通用表
- 三面蚂蚁金服成功拿到offer,Android开发社招面试经验
- Actf 2022 came to a successful conclusion, and 0ops team won the second consecutive championship!!
- 思维导图+源代码+笔记+项目,字节跳动+京东+360+网易面试题整理
- English topic assignment (25)
- Computer network: sorting out common network interview questions (I)
- 时钟轮在 RPC 中的应用
- Documents to be used in IC design process
- 第五期个人能力认证考核通过名单公布
- CPU负载很低,loadavg很高处理方法
猜你喜欢
MRO工业品企业采购系统:如何精细化采购协同管理?想要升级的工业品企业必看!
五金机电行业智能供应链管理系统解决方案:数智化供应链为传统产业“造新血”
包装行业商业供应链管理平台解决方案:布局智慧供应体系,数字化整合包装行业供应链
ROS自定义消息发布订阅示例
Mysql Information Schema 学习(一)--通用表
C language daily practice - day 22: Zero foundation learning dynamic planning
Synchronous development of business and application: strategic suggestions for application modernization
Zero foundation entry polardb-x: build a highly available system and link the big data screen
MRO industrial products enterprise procurement system: how to refine procurement collaborative management? Industrial products enterprises that want to upgrade must see!
A full set of teaching materials, real questions of Android interview of 7 major manufacturers including Alibaba Kwai pinduoduo
随机推荐
Meilu biological IPO was terminated: the annual revenue was 385million, and Chen Lin was the actual controller
R language uses rchisq function to generate random numbers that conform to Chi square distribution, and uses plot function to visualize random numbers that conform to Chi square distribution
三面蚂蚁金服成功拿到offer,Android开发社招面试经验
Based on butterfly species recognition
GCC【7】- 编译检查的是函数的声明,链接检查的是函数的定义bug
Translation D28 (with AC code POJ 26:the nearest number)
PMP practice once a day | don't get lost in the exam -7.6
LeetCode-1279. Traffic light intersection
主从搭建报错:The slave I/O thread stops because master and slave have equal MySQL serv
Airiot IOT platform enables the container industry to build [welding station information monitoring system]
Synchronous development of business and application: strategic suggestions for application modernization
学习探索-使用伪元素清除浮动元素造成的高度坍塌
JDBC details
冒烟测试怎么做
Wx applet learning notes day01
应用使用Druid连接池经常性断链问题分析
R语言dplyr包进行数据分组聚合统计变换(Aggregating transforms)、计算dataframe数据的分组均值(mean)
Don't miss this underestimated movie because of controversy!
Looting iii[post sequence traversal and backtracking + dynamic planning]
Black Horse - - Redis Chapter