在实际使用中,经常会有带in的子查询,如where id in (1,2,3)这样的情况,但是如果很多这样的语句在数据库中出现,将引起数据库的大量硬解析与共享池SQL碎片。所以,在实际应用中,可以采用其他方法,将这些in list给绑定起来。
如果需要绑定in list,首先,需要创建两个类型(type):
针对数据类型的:
CREATE OR REPLACE TYPE NUMTABLETYPE as table of number;
针对字符串类型的(每个list的单元大小不要超过1000字节)
create or replace type vartabletype as table of varchar2(1000);
然后创建两个相关的函数
数字列表函数
create or replace function str2numList( p_string in varchar2 ) return numTableType
as
v_str long default p_string || ',';
v_n number;
v_data numTableType := numTableType();
begin
loop
v_n := to_number(instr( v_str, ',' ));
exit when (nvl(v_n,0) = 0);
v_data.extend;
v_data( v_data.count ) := ltrim(rtrim(substr(v_str,1,v_n-1)));
v_str := substr( v_str, v_n+1 );
end loop;
return v_data;
end;
字符列表函数
create or replace function str2varList( p_string in varchar2 ) return VarTableType
as
v_str long default p_string || ',';
v_n varchar2(2000);
v_data VarTableType := VarTableType();
begin
loop
v_n :=instr( v_str, ',' );
exit when (nvl(v_n,0) = 0);
v_data.extend;
v_data( v_data.count ) := ltrim(rtrim(substr(v_str,1,v_n-1)));
v_str := substr( v_str, v_n+1 );
end loop;
return v_data;
end;
创建之后,我们就可以采用如下的方式来使用in list的绑定了。如可以采用如下的三种方案
SELECT /*+ ordered use_nl(a,u) */ id, user_id, BITAND(promoted_type,4) busauth
from table(STR2NUMLIST(:bind0)) a,
bmw_users u
where u.user_id = a.column_value;
SELECT /*+ leading(a) */ id, user_id, BITAND(promoted_type,4) busauth
from bmw_users u where user_id in
(select * from table(STR2NUMLIST(:bind0)) a);
SELECT /*+ index(bmw_users UK_BMW_USERS_USERID) */ id, user_id
from bmw_users where user_id in
(SELECT * FROM THE (SELECT CAST(STR2NUMLIST(:bind0) AS NUMTABLETYPE)
FROM dual) WHERE rownum<1000);
在如上的方案中,以上语句中的hint提示,是为了稳定执行计划,防止Oracle对in list的错误估计而导致走hash连接。一般建议采用第一种方法,比较简单可靠并且可以指定稳定的计划。但是要求数据库的版本比较高,在老版本中(8i),可能只能采用第三种方法。总的来说,1、2两种方法比3要少6个逻辑读左右。如:
SQL> SELECT /*+ ordered use_nl(a,u) */ id, user_id from table(STR2NUMLIST('1,2,3')) a,
bmw_users u where u.user_id = a.column_value
Execution Plan---------------------------------- 0 SELECT STATEMENT Optimizer=CHOOSE (Cost=3279 Card=8168 Bytes =334888) 1 0 NESTED LOOPS (Cost=3279 Card=8168 Bytes=334888) 2 1 COLLECTION ITERATOR (PICKLER FETCH) OF 'STR2NUMLIST' 3 1 TABLE ACCESS (BY INDEX ROWID) OF 'BMW_USERS' (Cost=1 Card=1 Bytes=39) 4 3 INDEX (UNIQUE SCAN) OF 'UK_BMW_USERS_USERID' (UNIQUE)Statistics---------------------------------- 0 recursive calls 0 db block gets 10 consistent gets 0 physical reads 0 redo size…… /*###################*/SQL> SELECT /*+ index(bmw_users UK_BMW_USERS_USERID) */ id, user_id
from bmw_users where user_id in
(SELECT * FROM THE (SELECT CAST(STR2NUMLIST('1,2,3') AS NUMTABLETYPE)
FROM dual) WHERE rownum<1000)
Execution Plan---------------------------------- 0 SELECT STATEMENT Optimizer=CHOOSE (Cost=430 Card=999 Bytes=51948) 1 0 NESTED LOOPS (Cost=430 Card=999 Bytes=51948) 2 1 VIEW OF 'VW_NSO_1' (Cost=11 Card=999 Bytes=12987) 3 2 SORT (UNIQUE) 4 3 COUNT (STOPKEY) 5 4 COLLECTION ITERATOR (PICKLER FETCH) OF 'STR2NUMLIST' 6 5 TABLE ACCESS (FULL) OF 'DUAL' (Cost=2 Card=82) 7 1 TABLE ACCESS (BY INDEX ROWID) OF 'BMW_USERS' (Cost=1 Card=1 Bytes=39) 8 7 INDEX (UNIQUE SCAN) OF 'UK_BMW_USERS_USERID' (UNIQUE)Statistics---------------------------------- 0 recursive calls 0 db block gets 16 consistent gets 0 physical reads 0 redo size……
分享到:
相关推荐
用法 numlist=[1,2,3] AddLinear(numList,99,1) NumList为[99,1,2,3] numList:=[1,2,3] Addlinear(numList,99,6) NumList为[1,2,3,0,0,99] 相关函数 参见系统函数SortByProperty和SortByValue。
这里实现方式事先说明下,采用json+文本流方式,即读取数据库信息至文本流中,格式采用json,之后读取文本中json数据至数据库中,实现度还不完全,不过也提供了...protected List numList=new ArrayList(); protected
theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,...
Magento的更好的所见即所得2编辑TinyMCE4通过 概述 通过此扩展程序,您可以扩展Magento 2中的TinyMCE 4工具,例如:添加文本颜色,图像,html代码,撤消重做,样式选择,fontsizeselect,前景色背景色,粗体,斜体...
NULL 博文链接:https://ssh-2009-126-com.iteye.com/blog/649592
Const NumList = "零壹贰叁肆伍陆柒捌玖" Const UnitList = "仟佰拾亿仟佰拾万仟佰拾元角分" Public Shared Sub StringList() Dim tmepList As New List(Of String) Console.WriteLine(vbLf & "Capacity: {0...
toolbar : 'bold italic bullist numlist link mediauploader' , // Path to CSS file to customize plugin widget appearance, see // `lib/styles.css` for available selectors and example styling // // ...
紧凑的 tiny_mce ...justifycenter,justifyright,justifyfull,|,formatselect,fontselect,|,undo,redo,|,preview,cleanup',theme_advanced_buttons2:'pasteword,|,bullist,numlist,|,outdent,indent,|,link,unlink
本文为大家分享了Python实现全排列的打印的代码,供大家参考,具体如下 问题:输入一个数字:3,打印它的全排列组合:123 132...def permutationCove(startIndex, n, numList): '''递归实现交换其中的两个。一直循环下
##文档结构 ... dataGrabber["list"]是返回信息中使用的数据 numlist是这个循环的元素 如果 XHR 请求失败,则 true 设置一些默认的“未找到数据”信息 populateItem()循环遍历最后一个参数中定义的元
#Tinymce 组插件 tinymce 的插件,用于显示组合菜单。 入门 Tinymce 配置: ... buttons: 'bullist,numlist', title: 'advanced.bullist_desc' }, { icon: 'indent', buttons: 'indent,outde
forest_full.css增加了对前3个独立的功能custom_slim.css 通过添加内联标签#numlist添加数字列表。 通过添加内嵌标签添加看板功能#kanban , #kanban-small , #kanban-w100 , #kanban-w200 , #kanban-w300 ,或#...
是的matlab代码用于REDAXO 5的TinyMCE4编辑器 默认配置文件 { selector : ' textarea.tinyMCEEditor ...numlist outdent indent | link unlink | table image pastetext removeformat | fullscreen visualblocks ' , c