oracle数据库中,我们会使用一些sql语句找出存在隐式转换的问题sql,其中网上流传的一个sql语句如下,查询v$sql_plan的字段filter_predicates中是否存在internal_function:
select
sql_id,
plan_hash_value
from
v$sql_plan x
where
x.filter_predicates like '%internal_function%'
group by
sql_id,
plan_hash_value;
但是笔者测试验证发现,有时候,执行计划中出现internal_function,并不一定代表出现了隐式数据类型转换,下面我们结合这篇博客“what the heck is the internal_function in execution plan predicate section?”来讲述一下执行计划谓词部分中的internal_function到底是什么?这篇博客没有打算直接翻译这篇文章,而是想结合自己的理解,来简单讲述一下internal_function。其实官方文档对internal_function的介绍非常少,最常见的理解,internal_function这种特殊函数用于执行隐式数据类型转换(implicit datatype conversion),可能来自官方文档https://docs.oracle.com/cd/e11882_01/server.112/e25523/part_avail.htm#sthref141 。但是这个说法,事实上仅仅部分正确,而不是全部的事实。事实上,oracle中找不到internal_function这个函数,通过v$sqlfn_metadata视图根本找不到internal_function这个对象。
col sqlfn_descr head description for a100 word_wrap
col sqlfn_name head name for a30
select
func_id
, name sqlfn_name
, offloadable
-- , usage
, minargs
, maxargs
-- this is just to avoid clutter on screen
, case when name != descr then descr else null end sqlfn_descr
from
v$sqlfn_metadata
where
upper(name) like upper('%&1%')
/
一般而言,我们在执行计划的的谓词部分发现出现“internal_function”,那么可能意味着出现了隐式类型转换(implicit data type conversion),下面我先简单构造一个例子,
sql> create table t(a varchar2(20), b date);
table created.
sql> insert into t values( to_char(sysdate), sysdate) ;
1 row created.
sql> commit;
commit complete.
如下所示,这个sql会出现隐式数据类型转换(implicit datatype conversion)
sql> select * from t where a = b;
no rows selected
sql> select * from table(dbms_xplan.display_cursor);
plan_table_output
--------------------------------------------------------------------------------
sql_id 4ptcbny27y9b0, child number 0
-------------------------------------
select * from t where a = b
plan hash value: 1601196873
--------------------------------------------------------------------------
| id | operation | name | rows | bytes | cost (%cpu)| time |
--------------------------------------------------------------------------
| 0 | select statement | | | | 2 (100)| |
|* 1 | table access full| t | 1 | 21 | 2 (0)| 00:00:01 |
plan_table_output
--------------------------------------------------------------------------------
--------------------------------------------------------------------------
predicate information (identified by operation id):
---------------------------------------------------
1 - filter("b"=internal_function("a"))
note
-----
- dynamic sampling used for this statement
22 rows selected.
通过执行计划,我们看到oracle为了能够比较两个不同数据类型(字段a与b之间的比较),强制在字段a上加了一个数据类型转换函数,在oracle内部,运算从where a=b 转换为where to_date(a)=b, 这也是为什么执行计划中出现internal_function的原因–从实际的“二进制”执行计划生成可读性的执行计划的代码无法将内部操作码转换为相应的适合人们容易理解的函数名称,因此默认使用“internal_function”字符串取而代之显示。 英文原文如下,可以对比理解(如果觉得翻译的不好的话)
what happens here is that oracle is forced to (implicitly) add a datatype conversion function around column a, to be able to physically compare two different datatypes. internally oracle is not running a comparison <strong>”where a = b”</strong> anymore, but rather something like <strong>”where to_date(a) = b”</strong>. this is one of the reasons why the internal_function shows up – the code generating the human-readable execution plan from the actual “binary” execution plan is not able to convert the internal opcode to a corresponding human-readable function name, thus shows a default “internal_function” string there instead.
un-unparseable complex expressions
执行计划中出现“internal_function”,还有一种情况是因为不可分割的复杂表达式(un-unparseable complex expressions),下面通过一个例子来说明一下
sql> drop table t purge;
table dropped.
sql> create table t as select * from dba_objects;
table created.
sql> select count(*) from t where owner = 'sys' or owner = 'system';
count(*)
----------
23851
sql> select * from table(dbms_xplan.display_cursor);
plan_table_output
--------------------------------------------------------------------------------
sql_id 77xzyugx5q3kf, child number 0
-------------------------------------
select count(*) from t where owner = 'sys' or owner = 'system'
plan hash value: 2966233522
---------------------------------------------------------------------------
| id | operation | name | rows | bytes | cost (%cpu)| time |
---------------------------------------------------------------------------
| 0 | select statement | | | | 108 (100)| |
| 1 | sort aggregate | | 1 | 17 | | |
plan_table_output
--------------------------------------------------------------------------------
|* 2 | table access full| t | 22494 | 373k| 108 (7)| 00:00:01 |
---------------------------------------------------------------------------
predicate information (identified by operation id):
---------------------------------------------------
2 - filter(("owner"='sys' or "owner"='system'))
note
-----
- dynamic sampling used for this statement
plan_table_output
--------------------------------------------------------------------------------
现在,我们让谓词稍微复杂一点,在查询条件中添加另一个or,但这是针对另一列object_id的查询条件,如下所示:
sql> select count(*) from t where owner = 'sys' or owner = 'system' or object_id = 123;
count(*)
----------
23851
sql> select * from table(dbms_xplan.display_cursor);
plan_table_output
--------------------------------------------------------------------------------
sql_id 9vh8b6ku8sd1t, child number 0
-------------------------------------
select count(*) from t where owner = 'sys' or owner = 'system' or
object_id = 123
plan hash value: 2966233522
---------------------------------------------------------------------------
| id | operation | name | rows | bytes | cost (%cpu)| time |
---------------------------------------------------------------------------
| 0 | select statement | | | | 111 (100)| |
plan_table_output
--------------------------------------------------------------------------------
| 1 | sort aggregate | | 1 | 30 | | |
|* 2 | table access full| t | 22494 | 659k| 111 (10)| 00:00:01 |
---------------------------------------------------------------------------
predicate information (identified by operation id):
---------------------------------------------------
2 - filter((internal_function("owner") or "object_id"=123))
note
-----
plan_table_output
--------------------------------------------------------------------------------
- dynamic sampling used for this statement
24 rows selected.
修改where查询条件后,owner表上的两个查询条件消失了,由internal_function替换了,接下来,让我们用in运算符,而不是or,但是上面sql是不同字段之间的or,我们需要修改一下sql语句
sql> select count(*) from t where owner in ('sys','system','scott') and object_type = 'table';
count(*)
----------
896
sql> select * from table(dbms_xplan.display_cursor);
plan_table_output
--------------------------------------------------------------------------------
sql_id gcqgrmtna9g1u, child number 0
-------------------------------------
select count(*) from t where owner in ('sys','system','scott') and
object_type = 'table'
plan hash value: 2966233522
---------------------------------------------------------------------------
| id | operation | name | rows | bytes | cost (%cpu)| time |
---------------------------------------------------------------------------
| 0 | select statement | | | | 111 (100)| |
plan_table_output
--------------------------------------------------------------------------------
| 1 | sort aggregate | | 1 | 16 | | |
|* 2 | table access full| t | 894 | 14304 | 111 (10)| 00:00:01 |
---------------------------------------------------------------------------
predicate information (identified by operation id):
---------------------------------------------------
2 - filter(("object_type"='table' and internal_function("owner")))
20 rows selected.
很不幸,上面执行计划中谓词部分依然出现了internal_function,我们在逻辑上简化一下,只搜寻同一个字段上的三个值:
sql> select count(*) from t where owner in ('sys','system','scott');
count(*)
----------
23857
sql> select * from table(dbms_xplan.display_cursor);
plan_table_output
--------------------------------------------------------------------------------
sql_id 2qazbqj67y17s, child number 0
-------------------------------------
select count(*) from t where owner in ('sys','system','scott')
plan hash value: 2966233522
---------------------------------------------------------------------------
| id | operation | name | rows | bytes | cost (%cpu)| time |
---------------------------------------------------------------------------
| 0 | select statement | | | | 111 (100)| |
| 1 | sort aggregate | | 1 | 7 | | |
plan_table_output
--------------------------------------------------------------------------------
|* 2 | table access full| t | 24133 | 164k| 111 (10)| 00:00:01 |
---------------------------------------------------------------------------
predicate information (identified by operation id):
---------------------------------------------------
2 - filter(("owner"='scott' or "owner"='sys' or "owner"='system'))
19 rows selected.
如上所示,它确实生效了,oracle已将in谓词转换为(或至少在执行计划中显示了)了一堆or-ed条件(针对同一列)
你可能已经看到了前面的例子的执行计划输出内容– dbms_xplan.display_cursor无法解释在单个执行计划步骤中应用的“复杂”的复合谓词,其中包括多个不同的列,并且至少其中一个列具有多个要检查的值(例如列表中或or-ed谓词)
display_cursor从何处获取数据并进行解释呢?
dbms_xplan.display_cursor从v$sql_plan获取其执行计划的相关数据,谓词部分来自access_predicates和filter_predicates列。但是当我直接查询v$sql_plan时,我仍然看到相同的问题:
sql> select id, filter_predicates from v$sql_plan where sql_id = ‘gcqgrmtna9g1u’;
id filter_predicates
———- ————————————————————
0
1
2 (internal_function(“owner”) and “object_type”=’table’)
你可能已经注意到,上面的原始ored条件周围也有括号(),这在9i中,意味着谓词周围的“二进制”执行计划中存在“无法解释的”内部函数,但是在这种情况下(如10g +支持internal_function命名),不应出现空白的函数名称……不确定为什么会出现这种情况,但这对本篇文章来说太深入了。
v$sql_plan视图本身访问库高速缓存(library cache)中的实际“二进制”子游标(在使用了适当的latches/pins/mutexe之后)并对其进行解析。为什么用这样的术语–其实并不是根据人类容易理解的输入并将其转换为计算机可理解的“二进制”格式。悄悄相反– v$sql_plan访问游标中的“二进制”执行计划的内存结构,并将其转换为人类可读的执行计划输出。甚至还有一个参数控制此v$sql_plan的行为,如果将其设置为false,则access_predicates和filter_predicates列将为空:
这段真不好翻译(有可能翻译不当),参考英文原文如下:
the v$sql_plan view itself accesses the actual “binary” child cursor in library cache (after taking appropriate latches/pins/mutexes) and unparses it. why such term – well isn’t parsing something that takes a human readable input and translates it into computer-understandable “binary” format. thus unparsing is the opposite – v$sql_plan accesses the cursor’s “binary” execution plan memory structure and translates it to human-readable execution plan output. there’s even a parameter controlling this v$sql_plan behavior, if it’s set to false, the access_predicates and filter_predicates columns will be empty there:
sql> @pd unparse
show all parameters and session values from x$ksppi/x$ksppcv...
name value description
----------------------------- --------- -----------------------------------------------
_cursor_plan_unparse_enabled true enables/disables using unparse to build
projection/predicates
顺便说一句,为什么我总是说“二进制”执行计划并用双引号括起来? 这是因为我想强调,oracle的实际执行计划并不像我们在屏幕上看到的输出的文本那样,这些输出的“执行计划”只是为了在troubleshooting的时候,更好的适应人类的阅读习惯而生成的文本(这里其实就是说转换成了符合人类阅读系统的文本),执行计划也不是真正的可执行二进制文件(如oracle.exe中一样),也没有直接反馈给cpu执行。 库缓存子游标中的物理执行计划(physical execution plan)是一堆操作码(a bunch of opcodes),object_id和指针,用于定义行源执行的层次结构和顺序。 sql执行引擎去循环遍历这些操作码,对其进行解码,然后知道下一步该做什么(要调用哪个rowsource函数)。
因此,如上所述,某些具有复杂and / or条件的谓词被dbms_xplan显示为internal_function()。display_cursor和v$sql_plan因为它们也无法完全解码(解析)执行计划信息。
using the good old explain plan
不过有个好消息! 旧的explain plan命令能够正确的解析这些复杂谓词(当然仅仅是其中一部分),当explain plan以一种特殊、更加仪器化的方式(more instrumented way)解析给定的sql语句时,它显然手头有更多信息(并且它还使用了更多的内存)。或者可能只是谁写了v$sql_plan,没有编写一段代码来解析更复杂的谓词:),如下所示:
sql> explain plan for
2 select count(*) from t where owner in ('sys','system','scott') and object_type = 'table';
explained.
sql> select * from table(dbms_xplan.display);
plan_table_output
--------------------------------------------------------------------------------
plan hash value: 2966233522
---------------------------------------------------------------------------
| id | operation | name | rows | bytes | cost (%cpu)| time |
---------------------------------------------------------------------------
| 0 | select statement | | 1 | 16 | 111 (10)| 00:00:01 |
| 1 | sort aggregate | | 1 | 16 | | |
|* 2 | table access full| t | 894 | 14304 | 111 (10)| 00:00:01 |
---------------------------------------------------------------------------
predicate information (identified by operation id):
plan_table_output
--------------------------------------------------------------------------------
---------------------------------------------------
2 - filter("object_type"='table' and ("owner"='scott' or
"owner"='sys' or "owner"='system'))
15 rows selected.
sql>
这真是一个奇迹,internal_function消失不见了,所有的谓词都正确的显示了,explain plan命令在这里非常有用。
因此,尽管我通常不使用explain plan命令,因为explain plan输出的执行计划可能会骗你,但是,每当我在display_cursor/v$sql_plan/sql monitor输出中看到internal_function时,我都会运行explain plan命令执行同一个sql,希望快速找出其中的谓词internal_function代表的真正意义。
参考资料:
https://blog.tanelpoder.com/2013/01/16/what-the-heck-is-the-internal_function-in-execution-plan-predicate-section/
https://docs.oracle.com/cd/e11882_01/server.112/e25523/part_avail.htm#sthref141