楼主: 〇〇

[转载] 趣味题

[复制链接]
论坛徽章:
26
2010年世界杯参赛球队:阿根廷
日期:2010-07-15 16:49:17马上加薪
日期:2014-10-30 09:48:58马上有车
日期:2014-11-04 14:03:06马上有钱
日期:2015-01-13 10:14:512015年新春福章
日期:2015-03-04 14:51:122015年新春福章
日期:2015-03-06 11:57:31喜羊羊
日期:2015-03-16 10:05:36慢羊羊
日期:2015-06-02 11:57:03慢羊羊
日期:2015-06-17 16:43:46巨蟹座
日期:2015-10-19 10:12:48
21#
发表于 2010-6-10 16:22 | 只看该作者

回复 #15 dingjun123 的帖子

font=宋体
1         
2        --------------------------------------------------------------------------------------------------
3        | Id  | Operation                          |  Name                       | Rows  | Bytes | Cost  |
4        --------------------------------------------------------------------------------------------------
5        |   0 | SELECT STATEMENT                   |                             |       |       |       |
6        |   2 |  TEMP TABLE TRANSFORMATION         |                             |       |       |       |
7        |   1 |   RECURSIVE EXECUTION              | SYS_LE_2_0                  |       |       |       |
8        |   0 |    INSERT STATEMENT                |                             |       |       |       |
9        |   1 |     LOAD AS SELECT                 |                             |       |       |       |
10        |*  2 |      VIEW                          |                             |       |       |       |
11        |*  3 |       COUNT STOPKEY                |                             |       |       |       |
12        |   4 |        CONNECT BY WITH FILTERING   |                             |       |       |       |
13        |   5 |         NESTED LOOPS               |                             |       |       |       |
14        |   6 |          TABLE ACCESS FULL         | DUAL                        |       |       |       |
15        |   7 |          TABLE ACCESS BY USER ROWID| DUAL                        |       |       |       |
16        |   8 |         NESTED LOOPS               |                             |       |       |       |
17        |   9 |          BUFFER SORT               |                             |       |       |       |
18        |  10 |           CONNECT BY PUMP          |                             |       |       |       |
19        |  11 |          TABLE ACCESS FULL         | DUAL                        |       |       |       |
20        |   3 |   MERGE JOIN                       |                             |       |       |       |
21        |   4 |    SORT JOIN                       |                             |       |       |       |
22        |   5 |     NESTED LOOPS                   |                             |       |       |       |
23        |   6 |      VIEW                          |                             |       |       |       |
24        |   7 |       TABLE ACCESS FULL            | SYS_TEMP_0FD9D66A9_EF30CCB  |       |       |       |
25        |*  8 |      VIEW                          |                             |       |       |       |
26        |   9 |       TABLE ACCESS FULL            | SYS_TEMP_0FD9D66A9_EF30CCB  |       |       |       |
27        |* 10 |    FILTER                          |                             |       |       |       |
28        |* 11 |     SORT JOIN                      |                             |       |       |       |
29        |  12 |      VIEW                          |                             |       |       |       |
30        |  13 |       TABLE ACCESS FULL            | SYS_TEMP_0FD9D66A9_EF30CCB  |       |       |       |
31        --------------------------------------------------------------------------------------------------
32         
33        Predicate Information (identified by operation id):
34        ---------------------------------------------------
35         
36           2 - filter("A"."NUM">=100 AND INSTR(TO_CHAR("A"."NUM"),'0')=0 AND
37                      SUBSTR(TO_CHAR("A"."NUM"),1,1)<>SUBSTR(TO_CHAR("A"."NUM"),3,1) AND
38                      SUBSTR(TO_CHAR("A"."NUM"),3,1)<>SUBSTR(TO_CHAR("A"."NUM"),2,1) AND
39                      SUBSTR(TO_CHAR("A"."NUM"),1,1)<>SUBSTR(TO_CHAR("A"."NUM"),2,1))
40           3 - filter(ROWNUM<=999)
41           8 - filter("A"."NUM"<"B"."NUM")
42          10 - filter("B"."NUM"<"C"."NUM" AND REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPL
43                      ACE(REPLACE('123456789',SUBSTR(TO_CHAR("A"."NUM"),1,1)),SUBSTR(TO_CHAR("A"."NUM"),2,1)),SUBSTR(TO_
44                      CHAR("A"."NUM"),3,1)),SUBSTR(TO_CHAR("B"."NUM"),1,1)),SUBSTR(TO_CHAR("B"."NUM"),2,1)),SUBSTR(TO_CH
45                      AR("B"."NUM"),3,1)),SUBSTR(TO_CHAR("C"."NUM"),1,1)),SUBSTR(TO_CHAR("C"."NUM"),2,1)),SUBSTR(TO_CHAR
46                      ("C"."NUM"),3,1)) IS NULL)
47          11 - access("C"."NUM"="A"."NUM"+"B"."NUM")
48               filter("C"."NUM"="A"."NUM"+"B"."NUM")
49         
50        Note: rule based optimization

果真是rbo

[ 本帖最后由 〇〇 于 2010-6-10 16:32 编辑 ]

使用道具 举报

回复
论坛徽章:
32
祖国60周年纪念徽章
日期:2009-10-09 08:28:002013年新春福章
日期:2013-02-25 14:51:24迷宫蛋
日期:2013-06-28 11:09:23ITPUB季度 技术新星
日期:2013-07-30 16:04:58优秀写手
日期:2013-12-18 09:29:132014年新春福章
日期:2014-02-18 16:43:09马上有钱
日期:2014-02-18 16:43:09红孩儿
日期:2014-03-04 16:40:38美羊羊
日期:2015-02-16 16:36:28懒羊羊
日期:2015-03-04 14:52:11
22#
发表于 2010-6-10 17:29 | 只看该作者
主要时间花在取数字组合上了:
SQL> with tmp as(select level l, replace(sys_connect_by_path(r, ','), ',') s
  2    from (select rownum r from dual connect by level <= 9)
  3   where level = 3
  4  connect by nocycle prior r <> r)
  5  --select * from tmp
  6  select /*+rule*/count(1)--t1.s, t2.s, t3.s
  7    from tmp t1, tmp t2, tmp t3
  8   where t1.s + t2.s = t3.s
  9     and t1.s < t2.s
10     and t2.s < t3.s
11     and instr(t1.s, substr(t2.s, 1, 1)) = 0
12     and instr(t1.s, substr(t2.s, 2, 1)) = 0
13     and instr(t1.s, substr(t2.s, 3, 1)) = 0
14     and instr(t1.s, substr(t3.s, 1, 1)) = 0
15     and instr(t1.s, substr(t3.s, 2, 1)) = 0
16     and instr(t1.s, substr(t3.s, 3, 1)) = 0
17     and instr(t2.s, substr(t3.s, 1, 1)) = 0
18     and instr(t2.s, substr(t3.s, 2, 1)) = 0
19     and instr(t2.s, substr(t3.s, 3, 1)) = 0
20  /

COUNT(1)--T1.S,T2.S,T3.S
------------------------
                     168

Executed in 9.735 seconds

RBO还是有优势的,呵呵,致前CBO花了46s。

[ 本帖最后由 regonly1 于 2010-6-10 17:31 编辑 ]

使用道具 举报

回复
论坛徽章:
69
生肖徽章2007版:羊
日期:2008-11-14 14:42:19复活蛋
日期:2011-08-06 08:59:05ITPUB十周年纪念徽章
日期:2011-11-01 16:19:412012新春纪念徽章
日期:2012-01-04 11:49:542012新春纪念徽章
日期:2012-02-13 15:13:202012新春纪念徽章
日期:2012-02-13 15:13:202012新春纪念徽章
日期:2012-02-13 15:13:202012新春纪念徽章
日期:2012-02-13 15:13:202012新春纪念徽章
日期:2012-02-13 15:13:20版主4段
日期:2012-05-15 15:24:11
23#
发表于 2010-6-10 17:57 | 只看该作者
〇〇 的
  5  where replace(replace(replace(
  6        replace(replace(replace(
  7        replace(replace(replace('123456789',substr(a.num,1,1)),substr(a.num,2,1)),substr(a.num,3,1)),
  8                                            substr(b.num,1,1)),substr(b.num,2,1)),substr(b.num,3,1)),
  9                                            substr(c.num,1,1)),substr(c.num,2,1)),substr(c.num,3,1)) is null

修改为:
translate('123456789','0' || a.num || b.num || c.num,'0') is null
会快不少.

使用道具 举报

回复
论坛徽章:
407
紫蛋头
日期:2012-05-21 10:19:41迷宫蛋
日期:2012-06-06 16:02:49奥运会纪念徽章:足球
日期:2012-06-29 15:30:06奥运会纪念徽章:排球
日期:2012-07-10 21:24:24鲜花蛋
日期:2012-07-16 15:24:59奥运会纪念徽章:拳击
日期:2012-08-07 10:54:50奥运会纪念徽章:羽毛球
日期:2012-08-21 15:55:33奥运会纪念徽章:蹦床
日期:2012-08-21 21:09:51奥运会纪念徽章:篮球
日期:2012-08-24 10:29:11奥运会纪念徽章:体操
日期:2012-09-07 16:40:00
24#
 楼主| 发表于 2010-6-10 18:40 | 只看该作者
我看了#8的也想到了translate
但是。。。。
SQL> with t as(select x from (select level+122 x from dual connect by level<=(987-122)) where substr(x,1,1)<>substr(x,2,1) and  substr(x,1,1)<>substr(x,3,1)
and instr(x,0)=0)
  2  select count(*) from (
  3  select a.x||'+'||b.x||'-'||c.x||'==0' from t a,t b,t c
  4  where a.x+b.x-c.x=0 and a.x<494 and a.x<b.x and b.x<c.x
  5  and translate('123456789','$'||a.x||b.x||c.x,'$') is null
  6  );

  COUNT(*)
----------
       168

已用时间:  00: 00: 10.90

使用道具 举报

回复
论坛徽章:
407
紫蛋头
日期:2012-05-21 10:19:41迷宫蛋
日期:2012-06-06 16:02:49奥运会纪念徽章:足球
日期:2012-06-29 15:30:06奥运会纪念徽章:排球
日期:2012-07-10 21:24:24鲜花蛋
日期:2012-07-16 15:24:59奥运会纪念徽章:拳击
日期:2012-08-07 10:54:50奥运会纪念徽章:羽毛球
日期:2012-08-21 15:55:33奥运会纪念徽章:蹦床
日期:2012-08-21 21:09:51奥运会纪念徽章:篮球
日期:2012-08-24 10:29:11奥运会纪念徽章:体操
日期:2012-09-07 16:40:00
25#
 楼主| 发表于 2010-6-10 18:41 | 只看该作者
一个translate竟然比一堆replace还慢

使用道具 举报

回复
论坛徽章:
407
紫蛋头
日期:2012-05-21 10:19:41迷宫蛋
日期:2012-06-06 16:02:49奥运会纪念徽章:足球
日期:2012-06-29 15:30:06奥运会纪念徽章:排球
日期:2012-07-10 21:24:24鲜花蛋
日期:2012-07-16 15:24:59奥运会纪念徽章:拳击
日期:2012-08-07 10:54:50奥运会纪念徽章:羽毛球
日期:2012-08-21 15:55:33奥运会纪念徽章:蹦床
日期:2012-08-21 21:09:51奥运会纪念徽章:篮球
日期:2012-08-24 10:29:11奥运会纪念徽章:体操
日期:2012-09-07 16:40:00
26#
 楼主| 发表于 2010-6-10 19:12 | 只看该作者
SQL> with t as(select x from (select level+122 x from dual connect by level<=(987-122)) where substr(x,1,1)<>substr(x,2,1) and  substr(x,1,1)<>substr(x,3,1)
and instr(x,0)=0)
  2  select count(*) from (
  3  select /*+rule */a.x||'+'||b.x||'-'||c.x||'==0' from t a,t b,t c
  4  where a.x+b.x-c.x=0 and a.x<494 and a.x<b.x and b.x<c.x
  5  and translate('123456789','$'||a.x||b.x||c.x,'$') is null)
  6  ;

  COUNT(*)
----------
       168

已用时间:  00: 04: 11.05

使用道具 举报

回复
论坛徽章:
69
生肖徽章2007版:羊
日期:2008-11-14 14:42:19复活蛋
日期:2011-08-06 08:59:05ITPUB十周年纪念徽章
日期:2011-11-01 16:19:412012新春纪念徽章
日期:2012-01-04 11:49:542012新春纪念徽章
日期:2012-02-13 15:13:202012新春纪念徽章
日期:2012-02-13 15:13:202012新春纪念徽章
日期:2012-02-13 15:13:202012新春纪念徽章
日期:2012-02-13 15:13:202012新春纪念徽章
日期:2012-02-13 15:13:20版主4段
日期:2012-05-15 15:24:11
27#
发表于 2010-6-10 20:02 | 只看该作者
原帖由 〇〇 于 2010-6-10 18:41 发表
一个translate竟然比一堆replace还慢

我用你18#的方法建立了一个 TMP 表,只不过没有收集统计信息。
然后执行你下面的SQL语句发现用translate 明显变快。

你测试变慢, 我怀疑是否是 oracle 在进行数据 filter 的时候先执行这个 translate 后执行其他筛选条件呢?
家里没环境测试。

使用道具 举报

回复
论坛徽章:
407
紫蛋头
日期:2012-05-21 10:19:41迷宫蛋
日期:2012-06-06 16:02:49奥运会纪念徽章:足球
日期:2012-06-29 15:30:06奥运会纪念徽章:排球
日期:2012-07-10 21:24:24鲜花蛋
日期:2012-07-16 15:24:59奥运会纪念徽章:拳击
日期:2012-08-07 10:54:50奥运会纪念徽章:羽毛球
日期:2012-08-21 15:55:33奥运会纪念徽章:蹦床
日期:2012-08-21 21:09:51奥运会纪念徽章:篮球
日期:2012-08-24 10:29:11奥运会纪念徽章:体操
日期:2012-09-07 16:40:00
28#
 楼主| 发表于 2010-6-10 20:10 | 只看该作者
在家里9201上测试

  1. SQL> select * from v$version;

  2. BANNER
  3. ----------------------------------------------------------------
  4. Oracle9i Enterprise Edition Release 9.2.0.1.0 - Production
  5. PL/SQL Release 9.2.0.1.0 - Production
  6. CORE    9.2.0.1.0       Production
  7. TNS for 32-bit Windows: Version 9.2.0.1.0 - Production
  8. NLSRTL Version 9.2.0.1.0 - Production

  9. 已用时间:  00: 00: 00.00


  10. SQL> with t as(select x from (select level+122 x from dual connect by level<=(987-122))
  11.   2  where substr(x,1,1)<>substr(x,2,1) and  substr(x,1,1)<>substr(x,3,1)
  12.   3  and  substr(x,2,1)<>substr(x,3,1) and instr(x,0)=0),
  13.   4  a as (select rownum rn,x from t where x<494)
  14.   5  select count(*) from (
  15.   6  select a.x||'+'||b.x||'-'||c.x||'==0' from a a,t b,t c
  16.   7  where a.x+b.x-c.x=0 and a.x<494 and a.x<b.x --and b.x<c.x
  17.   8  and not exists(select 1 from (select rownum l from dual connect by level<=9)where instr(a.x||b.x||c.x ,l,1,2)>0))
  18.   9  ;

  19.   COUNT(*)
  20. ----------
  21.        168

  22. 已用时间:  00: 00: 30.07
  23. SQL>  with tmp as(
  24.   2   select a.num from (select rownum as num from dual connect by rownum <= 999) a
  25.   3   where substr(a.num,1,1) != substr(a.num,2,1)
  26.   4   and substr(a.num,3,1) != substr(a.num,2,1)
  27.   5   and substr(a.num,1,1) != substr(a.num,3,1) and instr(a.num,0)=0
  28.   6   and a.num >= 123 and a.num<=987)
  29.   7   --
  30.   8   select count(*) from(
  31.   9   select
  32. 10  a.num || '+' || b.num || '=' || c.num as str
  33. 11  from tmp a, tmp b, tmp c
  34. 12  where replace(replace(replace(
  35. 13        replace(replace(replace(
  36. 14        replace(replace(replace('123456789',substr(a.num,1,1)),substr(a.num,2,1)),substr(a.num,3,1)),
  37. 15                                            substr(b.num,1,1)),substr(b.num,2,1)),substr(b.num,3,1)),
  38. 16                                            substr(c.num,1,1)),substr(c.num,2,1)),substr(c.num,3,1)) is null
  39. 17        and a.num + b.num = c.num
  40. 18        and a.num < b.num
  41. 19        and b.num < c.num
  42. 20  );

  43.   COUNT(*)
  44. ----------
  45.        168

  46. 已用时间:  00: 00: 00.05
  47. SQL>  with tmp as(
  48.   2   select a.num from (select rownum as num from dual connect by rownum <= 999) a
  49.   3   where substr(a.num,1,1) != substr(a.num,2,1)
  50.   4   and substr(a.num,3,1) != substr(a.num,2,1)
  51.   5   and substr(a.num,1,1) != substr(a.num,3,1) and instr(a.num,0)=0
  52.   6   and a.num >= 123 and a.num<=987)
  53.   7   --
  54.   8   select count(*) from(
  55.   9   select
  56. 10  a.num || '+' || b.num || '=' || c.num as str
  57. 11  from tmp a, tmp b, tmp c
  58. 12  where translate('123456789','$'||a.num||b.num||c.num,'$') is null
  59. 13        and a.num + b.num = c.num
  60. 14        and a.num < b.num
  61. 15        and b.num < c.num
  62. 16  );

  63.   COUNT(*)
  64. ----------
  65.        168

  66. 已用时间:  00: 00: 00.05

  67. SQL> with t as(select x from (select level+122 x from dual connect by level<=(987-122))
  68.   2  where substr(x,1,1)<>substr(x,2,1) and  substr(x,1,1)<>substr(x,3,1)
  69.   3  and instr(x,0)=0)
  70.   4  select count(*) from (
  71.   5  select /*+rule */a.x||'+'||b.x||'-'||c.x||'==0' from t a,t b,t c
  72.   6  where a.x+b.x-c.x=0 and a.x<494 and a.x<b.x and b.x<c.x
  73.   7  and translate('123456789','$'||a.x||b.x||c.x,'$') is null)
  74.   8  ;
  75. ^C终止批处理操作吗(Y/N)? y

  76. 已连接。
  77. SQL> with t as(select x from (select level+122 x from dual connect by level<=(987-122))
  78.   2  where substr(x,1,1)<>substr(x,2,1) and  substr(x,1,1)<>substr(x,3,1)
  79.   3  and instr(x,0)=0)
  80.   4  select count(*) from (
  81.   5  select a.x||'+'||b.x||'-'||c.x||'==0' from t a,t b,t c
  82.   6  where
  83.   7  translate('123456789','$'||a.x||b.x||c.x,'$') is null
  84.   8  and a.x+b.x-c.x=0 and a.x<494 and a.x<b.x and b.x<c.x
  85.   9  )
  86. 10  ;

  87.   COUNT(*)
  88. ----------
  89.        168

  90. 已用时间:  00: 00: 16.06
  91. SQL>


  92. SQL> with t as(select x from (select level+122 x from dual connect by level<=(987-122))
  93.   2  where substr(x,1,1)<>substr(x,2,1) and  substr(x,1,1)<>substr(x,3,1) and substr(x,2,1)<>substr(x,3,1)
  94.   3  and instr(x,0)=0)
  95.   4  select count(*) from (
  96.   5  select a.x||'+'||b.x||'-'||c.x||'==0' from t a,t b,t c
  97.   6  where
  98.   7  translate('123456789','$'||a.x||b.x||c.x,'$') is null
  99.   8  and a.x+b.x-c.x=0 and a.x<494 and a.x<b.x and b.x<c.x
  100.   9  )
  101. 10  ;

  102.   COUNT(*)
  103. ----------
  104.        168

  105. 已用时间:  00: 00: 10.05


  106. SQL> explain plan for with t as(select x from (select level+122 x from dual connect by level<=(987-122))
  107.   2  where substr(x,1,1)<>substr(x,2,1) and  substr(x,1,1)<>substr(x,3,1) and substr(x,2,1)<>substr(x,3,1)
  108.   3  and instr(x,0)=0)
  109.   4  select count(*) from (
  110.   5  select a.x||'+'||b.x||'-'||c.x||'==0' from t a,t b,t c
  111.   6  where
  112.   7  translate('123456789','$'||a.x||b.x||c.x,'$') is null
  113.   8  and a.x+b.x-c.x=0 and a.x<494 and a.x<b.x and b.x<c.x
  114.   9  )
  115. 10  ;

  116. 已解释。

  117. 已用时间:  00: 00: 00.01
  118. SQL> select * from table(dbms_xplan.display);

  119. PLAN_TABLE_OUTPUT
  120. ------------------------------------------------------------------------------------------------------------------------------------

  121. --------------------------------------------------------------------------------------------------
  122. | Id  | Operation                          |  Name                       | Rows  | Bytes | Cost  |
  123. --------------------------------------------------------------------------------------------------
  124. |   0 | SELECT STATEMENT                   |                             |       |       |       |
  125. |   2 |  TEMP TABLE TRANSFORMATION         |                             |       |       |       |
  126. |   1 |   RECURSIVE EXECUTION              | SYS_LE_2_0                  |       |       |       |
  127. |   0 |    INSERT STATEMENT                |                             |       |       |       |
  128. |   1 |     LOAD AS SELECT                 |                             |       |       |       |
  129. |   2 |      VIEW                          |                             |       |       |       |
  130. |*  3 |       FILTER                       |                             |       |       |       |
  131. |   4 |        CONNECT BY WITH FILTERING   |                             |       |       |       |
  132. |   5 |         NESTED LOOPS               |                             |       |       |       |
  133. |   6 |          TABLE ACCESS FULL         | DUAL                        |       |       |       |
  134. |   7 |          TABLE ACCESS BY USER ROWID| DUAL                        |       |       |       |
  135. |   8 |         NESTED LOOPS               |                             |       |       |       |
  136. |   9 |          BUFFER SORT               |                             |       |       |       |
  137. |  10 |           CONNECT BY PUMP          |                             |       |       |       |
  138. |* 11 |          FILTER                    |                             |       |       |       |
  139. |  12 |           TABLE ACCESS FULL        | DUAL                        |       |       |       |
  140. |   3 |   SORT AGGREGATE                   |                             |       |       |       |
  141. |   4 |    NESTED LOOPS                    |                             |       |       |       |
  142. |   5 |     NESTED LOOPS                   |                             |       |       |       |
  143. |   6 |      VIEW                          |                             |       |       |       |
  144. |   7 |       TABLE ACCESS FULL            | SYS_TEMP_0FD9D6606_109DBD8  |       |       |       |
  145. |*  8 |      VIEW                          |                             |       |       |       |
  146. |   9 |       TABLE ACCESS FULL            | SYS_TEMP_0FD9D6606_109DBD8  |       |       |       |
  147. |* 10 |     VIEW                           |                             |       |       |       |
  148. |  11 |      TABLE ACCESS FULL             | SYS_TEMP_0FD9D6606_109DBD8  |       |       |       |
  149. --------------------------------------------------------------------------------------------------

  150. Predicate Information (identified by operation id):
  151. ---------------------------------------------------

  152.    3 - filter(INSTR(TO_CHAR(LEVEL+122),'0')=0 AND SUBSTR(TO_CHAR(LEVEL+122),2,1)<>SUBSTR(TO_CHA
  153.               R(LEVEL+122),3,1) AND SUBSTR(TO_CHAR(LEVEL+122),1,1)<>SUBSTR(TO_CHAR(LEVEL+122),3
  154.               ,1) AND SUBSTR(TO_CHAR(LEVEL+122),1,1)<>SUBSTR(TO_CHAR(LEVEL+122),2,1))
  155.   11 - filter(LEVEL<=865)
  156.    8 - filter("B"."X"<"C"."X")
  157.   10 - filter("A"."X"<"B"."X" AND "A"."X"<494 AND "A"."X"+"B"."X"-"C"."X"=0 AND TRANSLATE('1234
  158.               56789','$'||TO_CHAR("A"."X")||TO_CHAR("B"."X")||TO_CHAR("C"."X"),'$') IS NULL)

  159. Note: rule based optimization

  160. 已选择43行。

  161. 已用时间:  00: 00: 00.07
  162. SQL> explain plan for with t as(select x from (select level+122 x from dual connect by level<=(987-122))
  163.   2  where substr(x,1,1)<>substr(x,2,1) and  substr(x,1,1)<>substr(x,3,1)
  164.   3  and instr(x,0)=0)
  165.   4  select count(*) from (
  166.   5  select /*+rule */a.x||'+'||b.x||'-'||c.x||'==0' from t a,t b,t c
  167.   6  where a.x+b.x-c.x=0 and a.x<494 and a.x<b.x and b.x<c.x
  168.   7  and translate('123456789','$'||a.x||b.x||c.x,'$') is null)
  169.   8  ;

  170. 已解释。

  171. 已用时间:  00: 00: 00.00
  172. SQL> select * from table(dbms_xplan.display);

  173. PLAN_TABLE_OUTPUT
  174. ------------------------------------------------------------------------------------------------------------------------------------

  175. --------------------------------------------------------------------------------------------------
  176. | Id  | Operation                          |  Name                       | Rows  | Bytes | Cost  |
  177. --------------------------------------------------------------------------------------------------
  178. |   0 | SELECT STATEMENT                   |                             |       |       |       |
  179. |   2 |  TEMP TABLE TRANSFORMATION         |                             |       |       |       |
  180. |   1 |   RECURSIVE EXECUTION              | SYS_LE_2_0                  |       |       |       |
  181. |   0 |    INSERT STATEMENT                |                             |       |       |       |
  182. |   1 |     LOAD AS SELECT                 |                             |       |       |       |
  183. |   2 |      VIEW                          |                             |       |       |       |
  184. |*  3 |       FILTER                       |                             |       |       |       |
  185. |   4 |        CONNECT BY WITH FILTERING   |                             |       |       |       |
  186. |   5 |         NESTED LOOPS               |                             |       |       |       |
  187. |   6 |          TABLE ACCESS FULL         | DUAL                        |       |       |       |
  188. |   7 |          TABLE ACCESS BY USER ROWID| DUAL                        |       |       |       |
  189. |   8 |         NESTED LOOPS               |                             |       |       |       |
  190. |   9 |          BUFFER SORT               |                             |       |       |       |
  191. |  10 |           CONNECT BY PUMP          |                             |       |       |       |
  192. |* 11 |          FILTER                    |                             |       |       |       |
  193. |  12 |           TABLE ACCESS FULL        | DUAL                        |       |       |       |
  194. |   3 |   SORT AGGREGATE                   |                             |       |       |       |
  195. |   4 |    NESTED LOOPS                    |                             |       |       |       |
  196. |   5 |     NESTED LOOPS                   |                             |       |       |       |
  197. |   6 |      VIEW                          |                             |       |       |       |
  198. |   7 |       TABLE ACCESS FULL            | SYS_TEMP_0FD9D6607_109DBD8  |       |       |       |
  199. |*  8 |      VIEW                          |                             |       |       |       |
  200. |   9 |       TABLE ACCESS FULL            | SYS_TEMP_0FD9D6607_109DBD8  |       |       |       |
  201. |* 10 |     VIEW                           |                             |       |       |       |
  202. |  11 |      TABLE ACCESS FULL             | SYS_TEMP_0FD9D6607_109DBD8  |       |       |       |
  203. --------------------------------------------------------------------------------------------------

  204. Predicate Information (identified by operation id):
  205. ---------------------------------------------------

  206.    3 - filter(INSTR(TO_CHAR(LEVEL+122),'0')=0 AND SUBSTR(TO_CHAR(LEVEL+122),1,1)<>SUBSTR(TO_CHA
  207.               R(LEVEL+122),3,1) AND SUBSTR(TO_CHAR(LEVEL+122),1,1)<>SUBSTR(TO_CHAR(LEVEL+122),2
  208.               ,1))
  209.   11 - filter(LEVEL<=865)
  210.    8 - filter("B"."X"<"C"."X")
  211.   10 - filter(TRANSLATE('123456789','$'||TO_CHAR("A"."X")||TO_CHAR("B"."X")||TO_CHAR("C"."X"),'
  212.               $') IS NULL AND "A"."X"<"B"."X" AND "A"."X"<494 AND "A"."X"+"B"."X"-"C"."X"=0)

  213. Note: rule based optimization

  214. 已选择43行。

  215. 已用时间:  00: 00: 00.01
  216. SQL> explain plan for  with tmp as(
  217.   2   select a.num from (select rownum as num from dual connect by rownum <= 999) a
  218.   3   where substr(a.num,1,1) != substr(a.num,2,1)
  219.   4   and substr(a.num,3,1) != substr(a.num,2,1)
  220.   5   and substr(a.num,1,1) != substr(a.num,3,1) and instr(a.num,0)=0
  221.   6   and a.num >= 123 and a.num<=987)
  222.   7   --
  223.   8   select count(*) from(
  224.   9   select
  225. 10  a.num || '+' || b.num || '=' || c.num as str
  226. 11  from tmp a, tmp b, tmp c
  227. 12  where translate('123456789','$'||a.num||b.num||c.num,'$') is null
  228. 13        and a.num + b.num = c.num
  229. 14        and a.num < b.num
  230. 15        and b.num < c.num
  231. 16  );

  232. 已解释。

  233. 已用时间:  00: 00: 00.00
  234. SQL> select * from table(dbms_xplan.display);

  235. PLAN_TABLE_OUTPUT
  236. ------------------------------------------------------------------------------------------------------------------------------------

  237. --------------------------------------------------------------------------------------------------
  238. | Id  | Operation                          |  Name                       | Rows  | Bytes | Cost  |
  239. --------------------------------------------------------------------------------------------------
  240. |   0 | SELECT STATEMENT                   |                             |       |       |       |
  241. |   2 |  TEMP TABLE TRANSFORMATION         |                             |       |       |       |
  242. |   1 |   RECURSIVE EXECUTION              | SYS_LE_2_0                  |       |       |       |
  243. |   0 |    INSERT STATEMENT                |                             |       |       |       |
  244. |   1 |     LOAD AS SELECT                 |                             |       |       |       |
  245. |*  2 |      VIEW                          |                             |       |       |       |
  246. |*  3 |       COUNT STOPKEY                |                             |       |       |       |
  247. |   4 |        CONNECT BY WITH FILTERING   |                             |       |       |       |
  248. |   5 |         NESTED LOOPS               |                             |       |       |       |
  249. |   6 |          TABLE ACCESS FULL         | DUAL                        |       |       |       |
  250. |   7 |          TABLE ACCESS BY USER ROWID| DUAL                        |       |       |       |
  251. |   8 |         NESTED LOOPS               |                             |       |       |       |
  252. |   9 |          BUFFER SORT               |                             |       |       |       |
  253. |  10 |           CONNECT BY PUMP          |                             |       |       |       |
  254. |  11 |          TABLE ACCESS FULL         | DUAL                        |       |       |       |
  255. |   3 |   SORT AGGREGATE                   |                             |       |       |       |
  256. |   4 |    MERGE JOIN                      |                             |       |       |       |
  257. |   5 |     SORT JOIN                      |                             |       |       |       |
  258. |   6 |      NESTED LOOPS                  |                             |       |       |       |
  259. |   7 |       VIEW                         |                             |       |       |       |
  260. |   8 |        TABLE ACCESS FULL           | SYS_TEMP_0FD9D6608_109DBD8  |       |       |       |
  261. |*  9 |       VIEW                         |                             |       |       |       |
  262. |  10 |        TABLE ACCESS FULL           | SYS_TEMP_0FD9D6608_109DBD8  |       |       |       |
  263. |* 11 |     FILTER                         |                             |       |       |       |
  264. |* 12 |      SORT JOIN                     |                             |       |       |       |
  265. |  13 |       VIEW                         |                             |       |       |       |
  266. |  14 |        TABLE ACCESS FULL           | SYS_TEMP_0FD9D6608_109DBD8  |       |       |       |
  267. --------------------------------------------------------------------------------------------------

  268. Predicate Information (identified by operation id):
  269. ---------------------------------------------------

  270.    2 - filter("A"."NUM"<=987 AND "A"."NUM">=123 AND INSTR(TO_CHAR("A"."NUM"),'0')=0 AND SUBSTR(
  271.               TO_CHAR("A"."NUM"),1,1)<>SUBSTR(TO_CHAR("A"."NUM"),3,1) AND SUBSTR(TO_CHAR("A"."N
  272.               UM"),3,1)<>SUBSTR(TO_CHAR("A"."NUM"),2,1) AND SUBSTR(TO_CHAR("A"."NUM"),1,1)<>SUB
  273.               STR(TO_CHAR("A"."NUM"),2,1))
  274.    3 - filter(ROWNUM<=999)
  275.    9 - filter("A"."NUM"<"B"."NUM")
  276.   11 - filter("B"."NUM"<"C"."NUM" AND TRANSLATE('123456789','$'||TO_CHAR("A"."NUM")||TO_CHAR("B
  277.               "."NUM")||TO_CHAR("C"."NUM"),'$') IS NULL)
  278.   12 - access("C"."NUM"="A"."NUM"+"B"."NUM")
  279.        filter("C"."NUM"="A"."NUM"+"B"."NUM")

  280. Note: rule based optimization

  281. 已选择48行。

  282. 已用时间:  00: 00: 00.01
  283. SQL>

  284. SQL> explain plan for with t as(select x from (select level+122 x from dual connect by level<=(987-122))
  285.   2  where substr(x,1,1)<>substr(x,2,1) and  substr(x,1,1)<>substr(x,3,1) and substr(x,2,1)<>substr(x,3,1)
  286.   3  and instr(x,0)=0)
  287.   4  select count(*) from (
  288.   5  select /*+rule */a.x||'+'||b.x||'-'||c.x||'==0' from t a,t b,t c
  289.   6  where a.x+b.x-c.x=0 and a.x<494 and a.x<b.x and b.x<c.x
  290.   7  and translate('123456789','$'||a.x||b.x||c.x,'$') is null)
  291.   8  ;

  292. 已解释。

  293. 已用时间:  00: 00: 00.00
  294. SQL> select * from table(dbms_xplan.display);

  295. PLAN_TABLE_OUTPUT
  296. --------------------------------------------------------------------------------------------------------------------

  297. --------------------------------------------------------------------------------------------------
  298. | Id  | Operation                          |  Name                       | Rows  | Bytes | Cost  |
  299. --------------------------------------------------------------------------------------------------
  300. |   0 | SELECT STATEMENT                   |                             |       |       |       |
  301. |   2 |  TEMP TABLE TRANSFORMATION         |                             |       |       |       |
  302. |   1 |   RECURSIVE EXECUTION              | SYS_LE_2_0                  |       |       |       |
  303. |   0 |    INSERT STATEMENT                |                             |       |       |       |
  304. |   1 |     LOAD AS SELECT                 |                             |       |       |       |
  305. |   2 |      VIEW                          |                             |       |       |       |
  306. |*  3 |       FILTER                       |                             |       |       |       |
  307. |   4 |        CONNECT BY WITH FILTERING   |                             |       |       |       |
  308. |   5 |         NESTED LOOPS               |                             |       |       |       |
  309. |   6 |          TABLE ACCESS FULL         | DUAL                        |       |       |       |
  310. |   7 |          TABLE ACCESS BY USER ROWID| DUAL                        |       |       |       |
  311. |   8 |         NESTED LOOPS               |                             |       |       |       |
  312. |   9 |          BUFFER SORT               |                             |       |       |       |
  313. |  10 |           CONNECT BY PUMP          |                             |       |       |       |
  314. |* 11 |          FILTER                    |                             |       |       |       |
  315. |  12 |           TABLE ACCESS FULL        | DUAL                        |       |       |       |
  316. |   3 |   SORT AGGREGATE                   |                             |       |       |       |
  317. |   4 |    NESTED LOOPS                    |                             |       |       |       |
  318. |   5 |     NESTED LOOPS                   |                             |       |       |       |
  319. |   6 |      VIEW                          |                             |       |       |       |
  320. |   7 |       TABLE ACCESS FULL            | SYS_TEMP_0FD9D6609_109DBD8  |       |       |       |
  321. |*  8 |      VIEW                          |                             |       |       |       |
  322. |   9 |       TABLE ACCESS FULL            | SYS_TEMP_0FD9D6609_109DBD8  |       |       |       |
  323. |* 10 |     VIEW                           |                             |       |       |       |
  324. |  11 |      TABLE ACCESS FULL             | SYS_TEMP_0FD9D6609_109DBD8  |       |       |       |
  325. --------------------------------------------------------------------------------------------------

  326. Predicate Information (identified by operation id):
  327. ---------------------------------------------------

  328.    3 - filter(INSTR(TO_CHAR(LEVEL+122),'0')=0 AND SUBSTR(TO_CHAR(LEVEL+122),2,1)<>SUBSTR(TO_CHA
  329.               R(LEVEL+122),3,1) AND SUBSTR(TO_CHAR(LEVEL+122),1,1)<>SUBSTR(TO_CHAR(LEVEL+122),3
  330.               ,1) AND SUBSTR(TO_CHAR(LEVEL+122),1,1)<>SUBSTR(TO_CHAR(LEVEL+122),2,1))
  331.   11 - filter(LEVEL<=865)
  332.    8 - filter("B"."X"<"C"."X")
  333.   10 - filter(TRANSLATE('123456789','$'||TO_CHAR("A"."X")||TO_CHAR("B"."X")||TO_CHAR("C"."X"),'
  334.               $') IS NULL AND "A"."X"<"B"."X" AND "A"."X"<494 AND "A"."X"+"B"."X"-"C"."X"=0)

  335. Note: rule based optimization

  336. 已选择43行。

  337. 已用时间:  00: 00: 00.01

  338. SQL> with t as(select x from (select level+122 x from dual connect by level<=(987-122))
  339.   2  where substr(x,1,1)<>substr(x,2,1) and  substr(x,1,1)<>substr(x,3,1) and substr(x,2,1)<>substr(x,3,1)
  340.   3  and instr(x,0)=0)
  341.   4  select count(*) from (
  342.   5  select /*+rule */a.x||'+'||b.x||'-'||c.x||'==0' from t a,t b,t c
  343.   6  where a.x+b.x-c.x=0 and a.x<494 and a.x<b.x and b.x<c.x
  344.   7  and translate('123456789','$'||a.x||b.x||c.x,'$') is null)
  345.   8  ;
  346. ^C终止批处理操作吗(Y/N)? y
复制代码

使用道具 举报

回复
论坛徽章:
407
紫蛋头
日期:2012-05-21 10:19:41迷宫蛋
日期:2012-06-06 16:02:49奥运会纪念徽章:足球
日期:2012-06-29 15:30:06奥运会纪念徽章:排球
日期:2012-07-10 21:24:24鲜花蛋
日期:2012-07-16 15:24:59奥运会纪念徽章:拳击
日期:2012-08-07 10:54:50奥运会纪念徽章:羽毛球
日期:2012-08-21 15:55:33奥运会纪念徽章:蹦床
日期:2012-08-21 21:09:51奥运会纪念徽章:篮球
日期:2012-08-24 10:29:11奥运会纪念徽章:体操
日期:2012-09-07 16:40:00
29#
 楼主| 发表于 2010-6-10 20:11 | 只看该作者
用了rule提示反而不出结果

使用道具 举报

回复
论坛徽章:
407
紫蛋头
日期:2012-05-21 10:19:41迷宫蛋
日期:2012-06-06 16:02:49奥运会纪念徽章:足球
日期:2012-06-29 15:30:06奥运会纪念徽章:排球
日期:2012-07-10 21:24:24鲜花蛋
日期:2012-07-16 15:24:59奥运会纪念徽章:拳击
日期:2012-08-07 10:54:50奥运会纪念徽章:羽毛球
日期:2012-08-21 15:55:33奥运会纪念徽章:蹦床
日期:2012-08-21 21:09:51奥运会纪念徽章:篮球
日期:2012-08-24 10:29:11奥运会纪念徽章:体操
日期:2012-09-07 16:40:00
30#
 楼主| 发表于 2010-6-10 20:19 | 只看该作者
迄今为止最快的,sort join+merge join

已连接。
SQL>  with tmp as(
  2   select a.num from (select rownum as num from dual connect by rownum <= 999) a
  3   where substr(a.num,1,1) != substr(a.num,2,1)
  4   and substr(a.num,3,1) != substr(a.num,2,1)
  5   and substr(a.num,1,1) != substr(a.num,3,1) and instr(a.num,0)=0
  6   and a.num >= 123 and a.num<=987)
  7   --
  8   --select count(*) from(
  9   select
10  a.num || '+' || b.num || '=' || c.num as str
11  from tmp a, tmp b, tmp c
12  where translate('123456789','$'||a.num||b.num||c.num,'$') is null
13        and a.num + b.num = c.num
14        and a.num < b.num
15        and b.num < c.num
16  --);

STR
--------------------------------------------------------------------------------------------------------------------------
176+283=459
173+286=459
186+273=459
183+276=459
195+273=468
193+275=468
175+293=468
173+295=468
129+357=486
159+327=486
157+329=486
127+359=486
168+327=495
167+328=495
128+367=495
127+368=495
162+387=549
187+362=549
182+367=549
167+382=549
128+439=567
138+429=567
249+318=567
248+319=567
219+348=567
218+349=567
139+428=567
129+438=567
194+382=576
192+384=576
184+392=576
182+394=576
278+316=594
276+318=594
218+376=594
216+378=594
187+452=639
182+457=639
157+482=639
152+487=639
297+351=648
291+357=648
257+391=648
251+397=648
239+418=657
238+419=657
219+438=657
218+439=657
294+381=675
291+384=675
284+391=675
281+394=675
193+482=675
192+483=675
183+492=675
182+493=675
278+415=693
275+418=693
215+478=693
218+475=693
146+583=729
186+543=729
183+546=729
143+586=729
196+542=738
192+546=738
146+592=738
142+596=738
124+659=783
159+624=783
154+629=783
269+514=783
264+519=783
219+564=783
214+569=783
129+654=783
134+658=792
158+634=792
154+638=792
138+654=792
367+452=819
362+457=819
357+462=819
352+467=819
276+543=819
273+546=819
246+573=819
243+576=819
296+541=837
291+546=837
246+591=837
241+596=837
195+642=837
192+645=837
145+692=837
142+695=837
329+517=846
327+519=846
319+527=846
317+529=846
125+739=864
129+735=864
293+571=864
291+573=864
271+593=864
273+591=864
139+725=864
135+729=864
259+614=873
254+619=873
219+654=873
214+659=873
237+654=891
234+657=891
367+524=891
364+527=891
327+564=891
324+567=891
257+634=891
254+637=891
376+542=918
372+546=918
346+572=918
342+576=918
273+645=918
245+673=918
275+643=918
243+675=918
386+541=927
381+546=927
346+581=927
341+586=927
184+752=936
182+754=936
154+782=936
152+784=936
328+617=945
327+618=945
318+627=945
317+628=945
183+762=945
182+763=945
163+782=945
162+783=945
283+671=954
281+673=954
273+681=954
271+683=954
238+716=954
236+718=954
218+736=954
216+738=954
248+715=963
245+718=963
218+745=963
215+748=963
358+614=972
354+618=972
318+654=972
314+658=972
357+624=981
354+627=981
327+654=981
324+657=981
236+745=981
235+746=981
246+735=981
245+736=981

已选择168行。

已用时间:  00: 00: 00.08

使用道具 举报

回复

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

TOP技术积分榜 社区积分榜 徽章 团队 统计 知识索引树 积分竞拍 文本模式 帮助
  ITPUB首页 | ITPUB论坛 | 数据库技术 | 企业信息化 | 开发技术 | 微软技术 | 软件工程与项目管理 | IBM技术园地 | 行业纵向讨论 | IT招聘 | IT文档
  ChinaUnix | ChinaUnix博客 | ChinaUnix论坛
CopyRight 1999-2011 itpub.net All Right Reserved. 北京盛拓优讯信息技术有限公司版权所有 联系我们 未成年人举报专区 
京ICP备16024965号-8  北京市公安局海淀分局网监中心备案编号:11010802021510 广播电视节目制作经营许可证:编号(京)字第1149号
  
快速回复 返回顶部 返回列表