[20181130]hash冲突导致查询缓慢.txt
[20181130]hash冲突导致查询缓慢.txt
--//昨天看了链接https://jonathanlewis.wordpress.com/2018/11/26/shrink-space-2/,演示了Shrink Space导致
--//执行语句缓慢的情况,我自己重复测试,实际上这样发生的概率还是很低的,我个人认为,至于Shrink Space是否好坏,
--//我个人还是根据实际的情况来确定.
1.环境:
SCOTT@book> @ ver1
PORT_STRING VERSION BANNER
------------------------------ -------------- --------------------------------------------------------------------------------
x86_64/Linux 2.4.xx 11.2.0.4.0 Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
SCOTT@book> rename emp to empxxx;
Table renamed.
--//作者测试的表名与scoot用户下冲突,我先修改原系统的表名.
2.建立测试脚本:
create table emp(
dept_no not null,
sal,
emp_no not null,
padding,
constraint e_pk primary key(emp_no)
)
as
with generator as (
select null
from dual
connect by
level <= 1e4 -- > comment to avoid wordpress format issue
)
select
mod(rownum,6),
rownum,
rownum,
rpad('x',60)
from
generator v1,
generator v2
where
rownum <= 2e4 -- > comment to avoid wordpress format issue
;
insert into emp values(432, 20001, 20001, rpad('x',60));
delete /*+ full(emp) */ from emp where emp_no <= 1000; -- > comment to avoid wordpress format issue
--//注:执行时要删除后面的注解.不然报错.作者应该把分号放在最后才能正常执行ok.
commit;
begin
dbms_stats.gather_table_stats(
ownname => user,
tabname => 'EMP',
method_opt => 'for all columns size 1'
);
end;
/
3.测试:
SCOTT@book> alter session set statistics_level = all;
Session altered.
select
/*+ gather_plan_statistics pre-shrink */
count(*)
from (
select /*+ no_merge */
outer.*
from
emp outer
where
outer.sal > (
select /*+ no_unnest */
avg(inner.sal)
from
emp inner
where
inner.dept_no = outer.dept_no
)
)
;
COUNT(*)
----------
9998
SCOTT@book> @ dpc '' ''
PLAN_TABLE_OUTPUT
-------------------------------------
SQL_ID 9bkx1f5cpcv14, child number 1
-------------------------------------
select /*+ gather_plan_statistics pre-shrink */
count(*) from ( select /*+ no_merge */
outer.* from emp outer where
outer.sal > ( select /*+ no_unnest */
avg(inner.sal) from
emp inner where
inner.dept_no = outer.dept_no
) )
Plan hash value: 322796046
------------------------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Starts | E-Rows |E-Bytes| Cost (%CPU)| E-Time | A-Rows | A-Time | Buffers |
------------------------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | | | 569 (100)| | 1 |00:00:00.04 | 1912 |
| 1 | SORT AGGREGATE | | 1 | 1 | | | | 1 |00:00:00.04 | 1912 |
| 2 | VIEW | | 1 | 143 | | 569 (1)| 00:00:07 | 9998 |00:00:00.04 | 1912 |
|* 3 | FILTER | | 1 | | | | | 9998 |00:00:00.03 | 1912 |
| 4 | TABLE ACCESS FULL | EMP | 1 | 20001 | 156K| 71 (0)| 00:00:01 | 20001 |00:00:00.01 | 239 |
| 5 | SORT AGGREGATE | | 7 | 1 | 8 | | | 7 |00:00:00.02 | 1673 |
|* 6 | TABLE ACCESS FULL| EMP | 7 | 2857 | 22856 | 71 (0)| 00:00:01 | 20001 |00:00:00.01 | 1673 |
------------------------------------------------------------------------------------------------------------------------
Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------
1 - SEL$1
2 - SEL$2 / from$_subquery$_001@SEL$1
3 - SEL$2
4 - SEL$2 / OUTER@SEL$2
5 - SEL$3
6 - SEL$3 / INNER@SEL$3
Predicate Information (identified by operation id):
---------------------------------------------------
3 - filter("OUTER"."SAL">)
6 - filter("INNER"."DEPT_NO"=:B1)
--//你可以发现内层循环扫描emp表7次.因为有7个部门.也就是oracle缓存了执行过.虽然最后1个记录是dept_no=432存在冲突,仅仅1条,
--//影响不大.
4.测试Shrink Space后:
SCOTT@book> alter table emp enable row movement;
Table altered.
SCOTT@book> alter table emp shrink space compact;
Table altered.
SCOTT@book> select * from emp where rownum<=4;
DEPT_NO SAL EMP_NO PADDING
---------- ---------- ---------- ---------
432 20001 20001 x
4 19978 19978 x
5 19979 19979 x
0 19980 19980 x
--//这样dept_no=432被移动到前面.
select
/*+ gather_plan_statistics post-shrink */
count(*)
from (
select /*+ no_merge */
outer.*
from emp outer
where outer.sal >
(
select /*+ no_unnest */ avg(inner.sal)
from emp inner
where inner.dept_no = outer.dept_no
)
)
;
COUNT(*)
----------
9498
SCOTT@book> @ dpc '' ''
PLAN_TABLE_OUTPUT
-------------------------------------
SQL_ID gx7xb7rhfd2zf, child number 0
-------------------------------------
select /*+ gather_plan_statistics post-shrink */
count(*) from ( select /*+ no_merge */
outer.* from emp outer where outer.sal >
( select /*+ no_unnest */ avg(inner.sal)
from emp inner where
inner.dept_no = outer.dept_no ) )
Plan hash value: 322796046
------------------------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Starts | E-Rows |E-Bytes| Cost (%CPU)| E-Time | A-Rows | A-Time | Buffers |
------------------------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | | | 569 (100)| | 1 |00:00:03.43 | 783K|
| 1 | SORT AGGREGATE | | 1 | 1 | | | | 1 |00:00:03.43 | 783K|
| 2 | VIEW | | 1 | 143 | | 569 (1)| 00:00:07 | 9498 |00:00:03.43 | 783K|
|* 3 | FILTER | | 1 | | | | | 9498 |00:00:03.43 | 783K|
| 4 | TABLE ACCESS FULL | EMP | 1 | 20001 | 156K| 71 (0)| 00:00:01 | 19001 |00:00:00.01 | 247 |
| 5 | SORT AGGREGATE | | 3172 | 1 | 8 | | | 3172 |00:00:03.42 | 783K|
|* 6 | TABLE ACCESS FULL| EMP | 3172 | 2857 | 22856 | 71 (0)| 00:00:01 | 10M|00:00:02.71 | 783K|
------------------------------------------------------------------------------------------------------------------------
Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------
1 - SEL$1
2 - SEL$2 / from$_subquery$_001@SEL$1
3 - SEL$2
4 - SEL$2 / OUTER@SEL$2
5 - SEL$3
6 - SEL$3 / INNER@SEL$3
Predicate Information (identified by operation id):
---------------------------------------------------
3 - filter("OUTER"."SAL">)
6 - filter("INNER"."DEPT_NO"=:B1)
--//注:我的测试机器比较快,没有作者测试的9秒,仅仅接近4秒完成,不过还是看出比原来执行慢.注意看id=6,循环执行次数是3172.
--//也就是dept_no=432与dept_no=0,1,2,3,4,5存在hash冲突,这样每次执行内层循环dept_no=:B1是都要重复调用.
SCOTT@book> select dept_no,count(*) from emp group by dept_no order by 1;
DEPT_NO COUNT(*)
---------- ----------
0 3167
1 3167
2 3167
3 3166
4 3166
5 3167
432 1
7 rows selected.
--//假设与dept_no=1出现hash冲突.
--//dept_no=432 循环1次
--//dept_no=0 循环1次
--//dept_no=1 循环3167次
--//dept_no=2 循环1次
--//dept_no=3 循环1次
--//dept_no=4 循环1次
--//dept_no=5 循环1次
--//这样累加: 1+1+3167+1+1+1+1 = 3173 ,不对相差1.我做了一些细节,证明hash冲突是dept_no=4.
5.其它有趣的测试:
--//执行如下,只要dept_no in 里面包括4,432查询就很慢(至少查询3个部门).就会有点慢.
--//也就是证明hash冲突的是dept_no=4.
select
/*+ gather_plan_statistics post-shrink */
count(*)
from (
select /*+ no_merge */
outer.*
from emp outer
where outer.sal >
(
select /*+ no_unnest */ avg(inner.sal)
from emp inner
where inner.dept_no = outer.dept_no
)
) where dept_no in (432,4,5)
;
--//如果你执行如下,你会发现执行很快:
select
/*+ gather_plan_statistics post-shrink */
count(*)
from (
select /*+ no_merge */
outer.*
from emp outer
where outer.sal >
(
select /*+ no_unnest */ avg(inner.sal)
from emp inner
where inner.dept_no = outer.dept_no
)
) where dept_no in (432,4)
;
SCOTT@book> @ dpc '' ''
PLAN_TABLE_OUTPUT
-------------------------------------
SQL_ID 9v9984wd6k9t5, child number 0
-------------------------------------
select /*+ gather_plan_statistics post-shrink */
count(*) from ( select /*+ no_merge */
outer.* from emp outer where outer.sal >
( select /*+ no_unnest */ avg(inner.sal)
from emp inner where
inner.dept_no = outer.dept_no ) ) where dept_no
in (432,4)
Plan hash value: 322796046
------------------------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Starts | E-Rows |E-Bytes| Cost (%CPU)| E-Time | A-Rows | A-Time | Buffers |
------------------------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | | | 214 (100)| | 1 |00:00:00.01 | 741 |
| 1 | SORT AGGREGATE | | 1 | 1 | | | | 1 |00:00:00.01 | 741 |
| 2 | VIEW | | 1 | 143 | | 214 (1)| 00:00:03 | 1583 |00:00:00.01 | 741 |
|* 3 | FILTER | | 1 | | | | | 1583 |00:00:00.01 | 741 |
|* 4 | TABLE ACCESS FULL | EMP | 1 | 5715 | 45720 | 71 (0)| 00:00:01 | 3167 |00:00:00.01 | 247 |
| 5 | SORT AGGREGATE | | 2 | 1 | 8 | | | 2 |00:00:00.01 | 494 |
|* 6 | TABLE ACCESS FULL| EMP | 2 | 2857 | 22856 | 71 (0)| 00:00:01 | 3167 |00:00:00.01 | 494 |
------------------------------------------------------------------------------------------------------------------------
--//循环仅仅2次.这是因为参考链接:
http://blog.itpub.net/267265/viewspace-2155927/
https://blogs.oracle.com/oraclemagazine/on-caching-and-evangelizing-sql
-//摘要如下:
When you're using a scalar subquery, Oracle Database will set up a small in-memory hash table for the subquery and its
results each time it runs the query. So, when you run the previous query, Oracle Database sets up in memory a hash table
that looks like this:
Oracle Database will use this hash table to remember the scalar subquery and the inputs to it—just :DEPTNO in this case
—and the output from it. At the beginning of every query execution, this cache is empty, but suppose you run the query
and the first PROJECTS row you retrieve has a DEPTNO value of 10. Oracle Database will assign the number 10 to a hash
value between 1 and 255 (the size of the hash table cache in Oracle Database 10g and Oracle Database 11g currently) and
will look in that hash table slot to see if the answer exists. In this case, it will not, so Oracle Database must run
the scalar subquery with the input of 10 to get the answer. If that answer (count) is 42, the hash table may look
something like this:
//注:补充说明我测试10.2.0.5,buckets=512而不是255.有机会测试11.2.0.4的情况.
Select count(*) from emp where emp.deptno = :deptno
:deptno Count(*)
You'll have saved the DEPTNO value of 10 and the answer (count) of 42 in some slot—probably not the first or last slot,
but whatever slot the hash value 10 is assigned to. Now suppose the second row you get back from the PROJECTS table
includes a DEPTNO value of 20. Oracle Database will again look in the hash table after assigning the value 20, and it
will discover "no result in the cache yet." So it will run the scalar subquery, get the result, and put it into the hash
table cache. Now the cache may look like this:
Select count(*) from emp where emp.deptno = :deptno
:deptno Count(*)
Select count(*) from emp where emp.deptno = :deptno
:deptno Count(*)
… …
10 42
Now suppose the query returns a third row and it again includes a DEPTNO value of 10. This time, Oracle Database will
see DEPTNO = 10, find that it already has that value in the hash table cache, and will simply return 42 from the cache
instead of executing the scalar subquery. In fact, it will never have to run that scalar subquery for the DEPTNO values
of 10 or 20 again for that query—it will already have the answer.
What happens if the number of unique DEPTNO values exceeds the size of the hash table? What if there are more than 255
values? Or, more generally, if more than one DEPTNO value is assigned to the same slot in the hash table, what happens
in a hash collision?
The answer is the same for all these questions and is rather simple: Oracle Database will not be able to cache the
second or nth value to that slot in the hash table. For example, what if the third row returned by the query contains
the DEPTNO = 30 value? Further, suppose that DEPTNO = 30 is to be assigned to exactly the same hash table slot as DEPTNO
= 10. The database won't be able to effectively cache DEPTNO = 30 in this case—the value will never make it into the
hash table. It will, however, be "partially cached." Oracle Database still has the hash table with all the previous
executions, but it also keeps the last scalar subquery result it had "next to" the hash table. That is, if the fourth
row also includes a DEPTNO = 30 value, Oracle Database will discover that the result is not in the hash table but is
"next to" the hash table, because the last time it ran the scalar subquery, it was run with an input of 30. On the other
hand, if the fourth row includes a DEPTNO = 40 value, Oracle Database will run the scalar subquery with the DEPTNO = 40
value (because it hasn't seen that value yet during this query execution) and overwrite the DEPTNO = 30 result. The next
time Oracle Database sees DEPTNO = 30 in the result set, it'll have to run that scalar subquery again.
--//答案在这一段落中,如果查询结果临近它会从前面的查询获得结果,而不用进入循环.我仅仅查询dept_no in (432,4),这样后面全部是
--//dept_no=4返回,这样可以从临近的查询获得结果.实际上你看作者的表设计就知道答案:
SCOTT@book> select * from emp where rownum<=10;
DEPT_NO SAL EMP_NO PADDING
---------- ---------- ---------- --------
432 20001 20001 x
4 19978 19978 x
5 19979 19979 x
0 19980 19980 x
1 19981 19981 x
2 19982 19982 x
3 19983 19983 x
4 19984 19984 x
5 19985 19985 x
0 19986 19986 x
10 rows selected.
--//dept_no记录不是聚集在一起的.
总结:
--//实际上这个例子我记忆在作者<基于成本的优化>的书中提到过.当时想作者如何知道那个数存在冲突.感觉作者很厉害.
--//另外写一篇blog猜测那些hash存在冲突的.
[20181130]hash冲突导致查询缓慢.txt的更多相关文章
- [20181130]如何猜测那些值存在hash冲突.txt
[20181130]如何猜测那些值存在hash冲突.txt --//今年6月份开始kerrycode的1个帖子提到子查询结果缓存在哈希表中情况:--//链接:http://www.cnblogs.co ...
- [20180626]函数与标量子查询14.txt
[20180626]函数与标量子查询14.txt --//前面看http://www.cnblogs.com/kerrycode/p/9099507.html链接,里面提到: 通俗来将,当使用标量子查 ...
- Hash冲突的四种解决办法
一.哈希表简介 非哈希表的特点:关键字在表中的位置和它自检不存在一个确定的关系,查找的过程为给定值一次和各个关系自进行比较,查找的效率取决于给定值进行比较的次数. 哈希表的特点:关键字在表中位置和它自 ...
- Java集合--Hash、Hash冲突
一.Hash 散列表(Hash table,也叫哈希表),是根据键(Key)而直接访问在内存存储位置的数据结构.也就是说,它通过计算一个关于键值的函数,将所需查询的数据映射到表中一个位置来访问记录,这 ...
- 没想到 Hash 冲突还能这么玩,你的服务中招了吗?
背景 其实这个问题我之前也看到过,刚好在前几天,洪教授在某个群里分享的一个<一些有意思的攻击手段.pdf>,我觉得这个话题还是有不少人不清楚的,今天我就准备来“实战”一把,还请各位看官轻拍 ...
- hash冲突随笔
一:hash表 也叫散列表,以key-value的形式存储数据,就是将需要存储的关键码值通过hash函数映射到表中的位置,可加快访问速度. 二:hash冲突 如果两个相同的关键码值通过hash函数映射 ...
- [20190306]奇怪的查询结果.txt
[20190306]奇怪的查询结果.txt--//链接http://www.itpub.net/thread-2108588-1-1.html提到一个非常古怪的问题,我自己重复测试看看:1.环境:SC ...
- 解决hash冲突之分离链接法
解决hash冲突之分离链接法 分离链接法:其做法就是将散列到同一个值的所有元素保存到一个表中. 这样讲可能比较抽象,下面看一个图就会很清楚,图如下 相应的实现可以用分离链接散列表来实现(其实就是一个l ...
- Map之HashMap的get与put流程,及hash冲突解决方式
在java中HashMap作为一种Map的实现,在程序中我们经常会用到,在此记录下其中get与put的执行过程,以及其hash冲突的解决方式: HashMap在存储数据的时候是key-value的键值 ...
随机推荐
- 【Android基础】Fragment 详解之Fragment生命周期
上一篇文章简单介绍了一下Fragment,这一篇文章会详细的说一下Fragment的生命周期和创建一个用户界面. Fragment的主要功能就是创建一个View,并且有一个生命周期来管理这个View的 ...
- 正则表达式的一些探索(偏JavaScript)
简单的探索下正则表达式的相关知识,首先先了解下正则表达式的引擎和匹配过程区别,再试着掌握如何在场景中编写正则表达式,再然后探索下根据上文已知的原理和编写过程怎么去优化正则表达式,最后给出一些js里正则 ...
- leetcode — combinations
import java.util.ArrayList; import java.util.Arrays; import java.util.List; /** * Source : https://o ...
- 如何做活动页面的滚动动画?让用户体验MAX的demo在这里!
本文由云+社区发表 最近的一个活动页面需要做一个可以左右滑动的抽签效果,故通过用css的transform属性和js结合来模拟可以无限滚动的效果. 先上效果: demo地址:https://kiror ...
- 编码(1)学点编码知识又不会死:Unicode的流言终结者和编码大揭秘
学点编码知识又不会死:Unicode的流言终结者和编码大揭秘 http://www.freebuf.com/articles/web/25623.html 如果你是一个生活在2003年的程序员,却不了 ...
- Resource Agent:LSB和OCF
1.简介 heartbeat和pacemaker都支持三种资源代理:传统的haresources脚本(/etc/ha.d/resource.d).符合LSB规范的脚本(/etc/init.d)以及OC ...
- 华为路由器 IPSec 与 GRE 结合实验
二者结合的目的 GRE 支持单播.组播.广播,IPSec 仅支持单播.GRE 不支持对于数据完整性以及身份认证的验证功能,并且也不具备数据加密保护.而 IPSec 恰恰拥有强大的安全机制.达到了互补的 ...
- 写一个ORM框架的第一步(Apache Commons DbUtils)
新一次的内部提升开始了,如果您想写一个框架从Apache Commons DbUtils开始学习是一种不错的选择,我们先学习应用这个小“框架”再把源代码理解,然后写一个属于自己的ORM框架不是梦. 一 ...
- ___简单的MVC单个图片上传预览
js: $("#btnImg").click(function () { $("#form0").ajaxSubmit({ url: "/Studen ...
- springbooot2 thymeleaf 配置以及加载资源文件。Cannot find template location: classpath:/templates/ (please add some templates or check your Thymeleaf configuration)
最近在学习springbooot2 和 thymeleaf 程序文件 application.properties文件配置: #thymeleaf spring.thymeleaf.prefix=cl ...