当我们在Oracle中执行任何sql语句时,会为该sql语句分配一个哈希值并将其存储到库缓存中。因此,稍后,如果另一个用户请求相同的查询,则Oracle会找到哈希值并执行相同的执行计划。但是,我对哈希值有一个疑问。我的意思是,如何生成哈希值?,我的意思是,Oracle服务器是使用某些算法还是只是将sql字符串转换为某个数值。
因为,我正在阅读 Pro Oracle SQL 一书,其中写有,
select * from employees where department_id = 60;
SELECT * FROM EMPLOYEES WHERE DEPARTMENT_ID = 60;
select /* a_comment */ * from employees where department_id = 60;
将返回不同的哈希值,因为当执行sql语句时,Oracle首先将字符串转换为哈希值。但是,当我尝试这个时,它会返回相同的哈希值。
SQL> select * from boats where bid=10;
no rows selected
Execution Plan
----------------------------------------------------------
Plan hash value: 2799518614
-------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 16 | 1 (0)| 00:00:01 |
| 1 | TABLE ACCESS BY INDEX ROWID| BOATS | 1 | 16 | 1 (0)| 00:00:01 |
|* 2 | INDEX UNIQUE SCAN | B_PK | 1 | | 0 (0)| 00:00:01 |
-------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("BID"=10)
SQL> SELECT * FROM BOATS WHERE BID=10;
no rows selected
Execution Plan
----------------------------------------------------------
Plan hash value: 2799518614
-------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 16 | 1 (0)| 00:00:01 |
| 1 | TABLE ACCESS BY INDEX ROWID| BOATS | 1 | 16 | 1 (0)| 00:00:01 |
|* 2 | INDEX UNIQUE SCAN | B_PK | 1 | | 0 (0)| 00:00:01 |
-------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("BID"=10)
答案 0 :(得分:9)
在您的问题文本中,您似乎在描述sql_id
和/或hash_value
。这是SQL语句文本的哈希值,也是Oracle用于确定共享池中是否已存在特定SQL语句的哈希值。但是,您在示例中显示的是plan_hash_value
,它是为SQL语句生成的计划的哈希值。两者之间可能存在多对多的关系。单个SQL语句(sql_id
/ hash_value
)可以有多个不同的计划(plan_hash_value
),并且多个不同的SQL语句可以共享相同的计划。
因此,例如,如果我编写两个不同的SQL语句来查询EMP
表中的特定行,我将得到相同的plan_hash_value
。
SQL> set autotrace traceonly;
SQL> select * from emp where ename = 'BOB';
no rows selected
Execution Plan
----------------------------------------------------------
Plan hash value: 3956160932
--------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 39 | 3 (0)| 00:00:01 |
|* 1 | TABLE ACCESS FULL| EMP | 1 | 39 | 3 (0)| 00:00:01 |
--------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("ENAME"='BOB')
SQL> ed
Wrote file afiedt.buf
1* select * FROM emp WHERE ename = 'BOB'
SQL> /
no rows selected
Execution Plan
----------------------------------------------------------
Plan hash value: 3956160932
--------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 39 | 3 (0)| 00:00:01 |
|* 1 | TABLE ACCESS FULL| EMP | 1 | 39 | 3 (0)| 00:00:01 |
--------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("ENAME"='BOB')
但是,如果我查看v$sql
,我会看到生成了两个不同的sql_id
和hash_value
值
SQL> set autotrace off;
SQL> ed
Wrote file afiedt.buf
1 select sql_id, sql_text, hash_value, plan_hash_value
2 from v$sql
3 where sql_text like 'select%BOB%'
4* and length(sql_text) < 50
SQL> /
SQL_ID SQL_TEXT HASH_VALUE PLAN_HASH_VALUE
------------- ---------------------------------------- ---------- ---------------
161v96c0v9c0n select * FROM emp WHERE ename = 'BOB' 28618772 3956160932
cvs1krtgzfr78 select * from emp where ename = 'BOB' 1610046696 3956160932
Oracle认识到这两个语句是具有不同sql_id
和hash_value
哈希值的不同查询。但他们都碰巧生成了相同的计划,所以他们最终得到了相同的plan_hash_value
。
答案 1 :(得分:0)
我会说你在这种情况下证明了这本书是错的。理论上,让哈希识别概念性SQL语句而不是随机大写的字符串似乎更好......我希望在生成哈希时也会忽略注释。 ; - )
答案 2 :(得分:0)
设置行300 col BEGIN_INTERVAL_TIME为a30 从dba_hist_snapshot a,dba_hist_sqlstat b中选择a.snap_id,a.begin_interval_time,b.plan_hash_value,其中a.snap_id = b.snap_id和b.sql_id ='&amp; sql_id'顺序为1;