[20180611]函数与标量子查询9.txt

简介: [20180611]函数与标量子查询9.txt --//前几天网友给一个链接,https://blogs.oracle.com/oraclemagazine/on-caching-and-evangelizing-sql --//也证明我测试的例子.
[20180611]函数与标量子查询9.txt

--//前几天网友给一个链接,https://blogs.oracle.com/oraclemagazine/on-caching-and-evangelizing-sql
--//也证明我测试的例子.
--//链接:http://blog.itpub.net/267265/viewspace-2155558/=>[20180602]函数与标量子查询3.txt

--//摘要如下:

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.
--//注意理解这段话.这样就很好理解为什么我前面测试
select a ,(select sleep1(a) from dual) s from
(select 48 a from dual
union all
select 75 a from dual
union all
select 75 a from dual
union all
select 48 a from dual
union all
select 75 a from dual);
--//递规测试是3.

So, all this discussion so far was a setup—a prelude, if you will—for what I really wanted to write about: how to
reduce the number of times a PL/SQL function invoked from SQL is called.


目录
相关文章
|
SQL Oracle 关系型数据库
[20151217]12c标量子查询.txt
[20151217]12c标量子查询.txt --我曾经写过blog,提到许多开发没有根据情况滥用子查询。 --而在12c下呢? So starting with Oracle 12c, the CBO transformation engine c...
1072 0
|
5月前
|
数据挖掘 数据处理 索引
python str.extract提取小数+表inner内连接后,行数多于之前
python str.extract提取小数+表inner内连接后,行数多于之前
39 0
python str.extract提取小数+表inner内连接后,行数多于之前
|
SQL 关系型数据库 MySQL
嵌套套娃,MySQL子查询,单行与多行子查询,相关和不相关(关联)子查询,完整详细可收藏
嵌套套娃,MySQL子查询,单行与多行子查询,相关和不相关(关联)子查询,完整详细可收藏
175 0
嵌套套娃,MySQL子查询,单行与多行子查询,相关和不相关(关联)子查询,完整详细可收藏
|
SQL 关系型数据库 MySQL
MySQL基础-标量子查询
SQL语句中嵌套SELECT语句,称为嵌套查询,又称子查询。
293 0
|
Oracle 关系型数据库 测试技术
[20180626]函数与标量子查询14.txt
[20180626]函数与标量子查询14.txt --//前面看http://www.cnblogs.com/kerrycode/p/9099507.html链接,里面提到: 通俗来将,当使用标量子查询的时候,ORACLE会将子查询结果缓存在哈希表中, 如果后续的记录出现同样的值,优化器通过缓存在哈希 表中的值,判断重复值不用重复调用函数,直接使用上次计算结果即可。
1303 0
|
Oracle 关系型数据库 测试技术
[20180612]函数与标量子查询10.txt
[20180612]函数与标量子查询10.txt --//前面看http://www.cnblogs.com/kerrycode/p/9099507.html链接,里面提到: 通俗来将,当使用标量子查询的时候,ORACLE会将子查询结果缓存在哈希表中, 如果后续的记录出现同样的值,优化器通过缓存在哈希 表中的值,判断重复值不用重复调用函数,直接使用上次计算结果即可。
1206 0
|
测试技术 Shell
[20180625]函数与标量子查询13(补充)
[20180625]函数与标量子查询13(补充).txt --//最近一段时间一直在测试标量子查询视buckets的数量,我前面的测试方法纯粹蛮力测试. --//参考链接:http://blog.
1305 0
|
Oracle 关系型数据库 vr&ar
[20180607]函数与标量子查询8.txt
[20180607]函数与标量子查询8.txt --//前面看http://www.cnblogs.com/kerrycode/p/9099507.html链接,里面提到: 通俗来将,当使用标量子查询的时候,ORACLE会将子查询结果缓存在哈希表中, 如果后续的记录出现同样的值,优化器通过缓存在哈希 表中的值,判断重复值不用重复调用函数,直接使用上次计算结果即可。
1054 0
|
SQL 存储 Oracle
[20180602]函数与标量子查询4.txt
[20180602]函数与标量子查询4.txt --//前面看http://www.cnblogs.com/kerrycode/p/9099507.html链接,里面提到: 通俗来将,当使用标量子查询的时候,ORACLE会将子查询结果缓存在哈希表中, 如果后续的记录出现同样的值,优化器通过缓存在哈希 表中的值,判断重复值不用重复调用函数,直接使用上次计算结果即可。
1009 0
|
Oracle 关系型数据库 存储
[20180602]函数与标量子查询3.txt
[20180602]函数与标量子查询3.txt --//前面看http://www.cnblogs.com/kerrycode/p/9099507.html链接,里面提到: 通俗来将,当使用标量子查询的时候,ORACLE会将子查询结果缓存在哈希表中, 如果后续的记录出现同样的值,优化器通过缓存在哈希 表中的值,判断重复值不用重复调用函数,直接使用上次计算结果即可。
1306 0