当前位置: 首页 > news >正文

单表查询-having和where区别

having和where的区别

差异之处:
Having:用于分组后过滤数据,可以使用聚合函数
Where:用于分组前过滤数据,不可使用聚合函数
相同之处:
都是用于过滤数据的。

案例分析

语句执行顺序:from->where->group by ->having->select->order
从上面的执行顺序可以知道where优先级比having高。

例子1

SQL> select count(*),fid from t1 group by fid having fid=1;

LINEID     COUNT(*)             FID        
---------- -------------------- -----------
1          166                  1


1   #NSET2: [510, 250->1, 4] 
2     #PRJT2: [510, 250->1, 4]; exp_num(2), is_atom(FALSE) 
3       #SLCT2: [510, 250->1, 4]; T1.FID = 1
4         #SAGR2: [509, 10000->10000, 4]; grp_num(1), sfun_num(1), opt_num(0), distinct_flag[0]; slave_empty(0) keys(T1.FID)
5           #PARALLEL: [317, 3000000->3000000, 4]; scan_type(FULL)
6             #SSCN: [317, 3000000->3000000, 4]; IDX_DM_T1_FID(T1); btr_scan(1)

Statistics
-----------------------------------------------------------------
0        data pages changed
0        undo pages changed
2440        logical reads
0        physical reads
0        redo size
197        bytes sent to client
119        bytes received from client
1        roundtrips to/from client
0        sorts (memory)
0        sorts (disk)
1        rows processed
0        io wait time(ms)
38        exec time(ms)


used time: 38.918(ms). Execute id is 703.

例子2

SQL> select count(*),fid from t1 where fid=1 group by fid;

LINEID     COUNT(*)             FID        
---------- -------------------- -----------
1          166                  1


1   #NSET2: [1, 1->1, 4] 
2     #PRJT2: [1, 1->1, 4]; exp_num(2), is_atom(FALSE) 
3       #SLCT2: [1, 1->1, 4]; exp_sfun9 > 0
4         #AAGR2: [1, 1->1, 4]; grp_num(0), sfun_num(1), distinct_flag[0]; slave_empty(0)
5           #PARALLEL: [1, 166->166, 4]; scan_type(FULL)
6             #SSEK2: [1, 166->166, 4]; scan_type(ASC), IDX_DM_T1_FID(T1), scan_range[1,1]

Statistics
-----------------------------------------------------------------
0        data pages changed
0        undo pages changed
136        logical reads
0        physical reads
0        redo size
190        bytes sent to client
118        bytes received from client
1        roundtrips to/from client
0        sorts (memory)
0        sorts (disk)
1        rows processed
0        io wait time(ms)
1        exec time(ms)


used time: 1.606(ms). Execute id is 704.

上面例子的结果都是一样的,但是执行效率是where更快。
过滤数据上,前面也提到where优先级高于having,也就是说where先过滤数据再分组,having是先分组再过滤数据。

总结

在实际项目中,有时遇到上面的情况。像这类问题的优化处理原则:先过滤后分组。不用聚合函数过滤数据则用where。


文章转载自:

http://6FdC9XUJ.hrmqx.cn
http://n5uQ2wjd.hrmqx.cn
http://wWDOBDpp.hrmqx.cn
http://dokeg92K.hrmqx.cn
http://ZJhgZqjk.hrmqx.cn
http://cVJxYYjx.hrmqx.cn
http://BAhk2UEw.hrmqx.cn
http://PdOMi36A.hrmqx.cn
http://7iGaFJrH.hrmqx.cn
http://Jzwhu55I.hrmqx.cn
http://a6dWmSxG.hrmqx.cn
http://2SXIuCgC.hrmqx.cn
http://iM7OpeH0.hrmqx.cn
http://qlP9JpwX.hrmqx.cn
http://RKn7kmvY.hrmqx.cn
http://Az1gU0k3.hrmqx.cn
http://rjWzGA5E.hrmqx.cn
http://Z0nGXc1b.hrmqx.cn
http://mTVqhMw9.hrmqx.cn
http://cSy4zxZP.hrmqx.cn
http://OGAPWa9M.hrmqx.cn
http://Z0hfss2z.hrmqx.cn
http://fXO57FUC.hrmqx.cn
http://fm7mXvkf.hrmqx.cn
http://GOFZKdpT.hrmqx.cn
http://opGUb4ku.hrmqx.cn
http://XLEWsDIC.hrmqx.cn
http://4PlvQ4MM.hrmqx.cn
http://3Qtrl3Xl.hrmqx.cn
http://GJ1ed0qc.hrmqx.cn
http://www.dtcms.com/a/378164.html

相关文章:

  • LVGL:基础对象
  • 【LeetCode - 每日1题】将字符串中的元音字母排序
  • 签名、杂凑、MAC、HMAC
  • C++与QT高频面试问题(不定时更新)
  • 数据结构之跳表
  • 记录豆包的系统提示词
  • Docker 从入门到实践:容器化技术核心指南
  • 【Python-Day 43】告别依赖混乱:Python虚拟环境venv入门与实战
  • CF702E Analysis of Pathes in Functional Graph 题解
  • 元宇宙与智慧城市:数字孪生赋能的城市治理新范式
  • es通过分片迁移迁移解决磁盘不均匀问题
  • 深入浅出CRC校验:从数学原理到单周期硬件实现 (2)CRC数学多项式基础
  • 无人设备遥控器之控制指令发送技术篇
  • LinuxC++项目开发日志——高并发内存池(4-central cache框架开发)
  • 解决蓝牙耳机连win11电脑画质依托答辩问题
  • 农业养殖为何离不开温湿度传感器?
  • Android开发 AlarmManager set() 方法与WiFi忘记连接问题分析
  • CKA02-Ingress
  • JavaEE 初阶第二十一期:网络原理,底层框架的“通关密码”(一)
  • TOL-API 基于Token验证文件传输API安全工具
  • 构建一个优雅的待办事项应用:现代JavaScript实践
  • 计算机视觉进阶教学之图像投影(透视)变换
  • 计算机视觉与深度学习 | 基于MATLAB的AI图片识别系统研究
  • 计算机视觉----图像投影(透视)变换(小案例)
  • Docker 学习笔记(七):Docker Swarm 服务管理与 Containerd 实践
  • 3-10〔OSCP ◈ 研记〕❘ WEB应用攻击▸XSS攻击理论基础
  • 微信小程序开发笔记(01_小程序基础与配置文件)
  • ArcGIS JSAPI 高级教程 - ArcGIS Maps SDK for JavaScript - 自定义(GLSL)修改高亮图层样式
  • idea npm install 很慢(nodejs)
  • Elasticsearch 创建索引别名的正确姿势