源码-数据库调优(tuning)初接触
来源:互联网 发布:cad网络培训班 编辑:程序博客网 时间:2024/05/29 13:23
数据库调优对我而言一直是比较高深的内容,今晚看了慕课网的一节视频课程后,有点领悟了。
问题:使用分组子句的情况下,如果要过滤查询结果,而where/having都能使用(即,过滤条件不含分组函数),请问用where好还是having好?
答案:使用where子句可以获得更好的性能,因为where是“先过滤,再分组”,而having是“先分组,再过滤”。
口说无凭,以下源码为证:
scott_pd@ORCL> ed已写入 file afiedt.buf 1* select deptno, avg(sal) from emp where deptno=10group by deptnoscott_pd@ORCL> / DEPTNO AVG(SAL)---------- ---------- 10 5000执行计划----------------------------------------------------------Plan hash value: 2854459865-----------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |-----------------------------------------------------------------------------| 0 | SELECT STATEMENT | | 1 | 6 | 3 (0)| 00:00:01 || 1 | SORT GROUP BY NOSORT| | 1 | 6 | 3 (0)| 00:00:01 ||* 2 | TABLE ACCESS FULL | EMP | 9 | 54 | 3 (0)| 00:00:01 |-----------------------------------------------------------------------------Predicate Information (identified by operation id):--------------------------------------------------- 2 - filter("DEPTNO"=10)统计信息---------------------------------------------------------- 0 recursive calls 0 db block gets 7 consistent gets 0 physical reads 0 redo size 605 bytes sent via SQL*Net to client 524 bytes received via SQL*Net from client 2 SQL*Net roundtrips to/from client 0 sorts (memory) 0 sorts (disk) 1 rows processedscott_pd@ORCL> ed已写入 file afiedt.buf 1* select deptno, avg(sal) from emp group by deptno having deptno=10scott_pd@ORCL> / DEPTNO AVG(SAL)---------- ---------- 10 5000执行计划----------------------------------------------------------Plan hash value: 2138686577----------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |----------------------------------------------------------------------------| 0 | SELECT STATEMENT | | 1 | 6 | 4 (25)| 00:00:01 ||* 1 | FILTER | | | | | || 2 | HASH GROUP BY | | 1 | 6 | 4 (25)| 00:00:01 || 3 | TABLE ACCESS FULL| EMP | 38 | 228 | 3 (0)| 00:00:01 |----------------------------------------------------------------------------Predicate Information (identified by operation id):--------------------------------------------------- 1 - filter("DEPTNO"=10)统计信息---------------------------------------------------------- 1 recursive calls 0 db block gets 7 consistent gets 0 physical reads 0 redo size 605 bytes sent via SQL*Net to client 524 bytes received via SQL*Net from client 2 SQL*Net roundtrips to/from client 0 sorts (memory) 0 sorts (disk) 1 rows processedscott_pd@ORCL> spool当前正假脱机至 d:/20161010_group_by_tuning.txtscott_pd@ORCL> spool out
在源码(SQL运行记录)中,我们能看到,使用where子句,共占用了9(3+3+3)个CPU单位,而having子句占用了共11(4+4+3)个CPU单位。
在数据量很庞大的情况下,两者的差距想必也很大。
懂得了这个道理,以后,在写SQL语句的时候,就可以优先使用where了。姑且将本次学习经验称之为数据库调优初接触吧。
0 0
- 源码-数据库调优(tuning)初接触
- Java源码-断言(Assertion)初接触
- 2008-数据库初接触
- 数据库初接触
- 接触数据库
- 调优日志切换(Tuning Log Switches)
- MySQL调优脚本tuning-primer.sh
- Mule Tuning Performance Mule性能调优
- Performance Tuning Introduction drill 性能调优
- 初接触Oracle数据库,常见问题与解决
- android接触--下载源码
- oracle sql调优学习笔记(二)RBO Query Tuning 之Row cache order
- JSP连接操作数据库全接触(所有流行数据库)
- DM8127 Image-tuning tool server thread 源码
- mysql数据库初步接触
- 钩子初接触(一)
- 钩子初接触(二)
- 钩子初接触(三)
- 掌握 Linux PC 性能之基准测试
- 匿名问题
- python 时间模块: datetime
- 程序11-先升序后降序返回最大值
- 封装和private关键字
- 源码-数据库调优(tuning)初接触
- 1-1 C++ learning
- 你可能不清楚的Java细节(2)--for及for-each各自循环适用的场景
- 关于今天笔试关于java一些基础题的疑惑
- 初探Docker
- this关键字
- c语言----交换a,b的值(分别用临时变量,加减法,异或实现)
- 解决listview加载网络图片乱跳
- 构造方法