[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存在冲突的.

(0)
上一篇 2022年3月22日
下一篇 2022年3月22日

相关推荐