楼主: 〇〇

[转载] 趣味题

[复制链接]
论坛徽章:
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
11#
 楼主| 发表于 2010-6-10 14:39 | 只看该作者
我的机器运行8#的也很慢,2分钟了
执行计划还不错

SQL> set timi on
SQL> set lines 132 pages 50000
SQL> explain plan for
  2  with tmp as(
  3  select a.num from (select rownum as num from dual connect by rownum <= 999) a
  4  where substr(a.num,1,1) != substr(a.num,2,1)
  5  and substr(a.num,3,1) != substr(a.num,2,1)
  6  and substr(a.num,1,1) != substr(a.num,3,1) and instr(a.num,0)=0
  7  and a.num >= 123 and a.num<=987)
  8  --
  9  select count(*) from(
10  select
11  a.num || '+' || b.num || '=' || c.num as str
12  from tmp a, tmp b, tmp c
13  where replace(replace(replace(
14        replace(replace(replace(
15        replace(replace(replace('123456789',substr(a.num,1,1)),substr(a.num,2,1)),substr(a.num,3,1)),
16                                            substr(b.num,1,1)),substr(b.num,2,1)),substr(b.num,3,1)),
17                                            substr(c.num,1,1)),substr(c.num,2,1)),substr(c.num,3,1)) is null
18        and a.num + b.num = c.num
19        and a.num < b.num
20        and b.num < c.num
21  );

已解释。

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

PLAN_TABLE_OUTPUT
------------------------------------------------------------------------------------------------------------------------------------
Plan hash value: 1767511922

---------------------------------------------------------------------------------------------------------------
| Id  | Operation                        | Name                       | Rows  | Bytes | Cost (%CPU)| Time     |
---------------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT                 |                            |     1 |    39 |     8   (0)| 00:00:01 |
|   1 |  TEMP TABLE TRANSFORMATION       |                            |       |       |            |       |
|   2 |   LOAD AS SELECT                 |                            |       |       |            |       |
|*  3 |    VIEW                          |                            |     1 |    13 |     2   (0)| 00:00:01 |
|   4 |     COUNT                        |                            |       |       |            |       |
|*  5 |      CONNECT BY WITHOUT FILTERING|                            |       |       |            |       |
|   6 |       FAST DUAL                  |                            |     1 |       |     2   (0)| 00:00:01 |
|   7 |   SORT AGGREGATE                 |                            |     1 |    39 |            |       |
|   8 |    NESTED LOOPS                  |                            |     1 |    39 |     6   (0)| 00:00:01 |
|   9 |     MERGE JOIN CARTESIAN         |                            |     1 |    26 |     4   (0)| 00:00:01 |
|  10 |      VIEW                        |                            |     1 |    13 |     2   (0)| 00:00:01 |
|  11 |       TABLE ACCESS FULL          | SYS_TEMP_0FD9D6610_16B6486 |     1 |    13 |     2   (0)| 00:00:01 |
|  12 |      BUFFER SORT                 |                            |     1 |    13 |     4   (0)| 00:00:01 |
|  13 |       VIEW                       |                            |     1 |    13 |     2   (0)| 00:00:01 |
|  14 |        TABLE ACCESS FULL         | SYS_TEMP_0FD9D6610_16B6486 |     1 |    13 |     2   (0)| 00:00:01 |
|* 15 |     VIEW                         |                            |     1 |    13 |     2   (0)| 00:00:01 |
|  16 |      TABLE ACCESS FULL           | SYS_TEMP_0FD9D6610_16B6486 |     1 |    13 |     2   (0)| 00:00:01 |
---------------------------------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   3 - filter(SUBSTR(TO_CHAR("A"."NUM"),1,1)<>SUBSTR(TO_CHAR("A"."NUM"),2,1) AND
              SUBSTR(TO_CHAR("A"."NUM"),3,1)<>SUBSTR(TO_CHAR("A"."NUM"),2,1) AND
              SUBSTR(TO_CHAR("A"."NUM"),1,1)<>SUBSTR(TO_CHAR("A"."NUM"),3,1) AND INSTR(TO_CHAR("A"."NUM"),'0')=0 AND
              "A"."NUM">=123 AND "A"."NUM"<=987)
   5 - filter(ROWNUM<=999)
  15 - filter(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE('123456789',SUBST
              R(TO_CHAR("A"."NUM"),1,1)),SUBSTR(TO_CHAR("A"."NUM"),2,1)),SUBSTR(TO_CHAR("A"."NUM"),3,1)),SUBSTR(TO_CH
              AR("B"."NUM"),1,1)),SUBSTR(TO_CHAR("B"."NUM"),2,1)),SUBSTR(TO_CHAR("B"."NUM"),3,1)),SUBSTR(TO_CHAR("C".
              "NUM"),1,1)),SUBSTR(TO_CHAR("C"."NUM"),2,1)),SUBSTR(TO_CHAR("C"."NUM"),3,1)) IS NULL AND
              "C"."NUM"="A"."NUM"+"B"."NUM" AND "A"."NUM"<"B"."NUM" AND "B"."NUM"<"C"."NUM")

已选择37行。

已用时间:  00: 00: 00.06
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 replace(replace(replace(
13        replace(replace(replace(
14        replace(replace(replace('123456789',substr(a.num,1,1)),substr(a.num,2,1)),substr(a.num,3,1)),
15                                            substr(b.num,1,1)),substr(b.num,2,1)),substr(b.num,3,1)),
16                                            substr(c.num,1,1)),substr(c.num,2,1)),substr(c.num,3,1)) is null
17        and a.num + b.num = c.num
18        and a.num < b.num
19        and b.num < c.num
20  );


我的成本

SQL> explain plan for
  2  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

  3  and  substr(x,2,1)<>substr(x,3,1) and instr(x,0)=0),
  4  a as (select rownum rn,x from t where x<494)
  5  select count(*) from (
  6  select a.x||'+'||b.x||'-'||c.x||'==0' from a a,t b,t c
  7  where a.x+b.x-c.x=0 and a.x<494 and a.x<b.x --and b.x<c.x
  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))
  9  --and rownum<=10;

已解释。

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

PLAN_TABLE_OUTPUT
------------------------------------------------------------------------------------------------------------------------------------
Plan hash value: 2614637613

----------------------------------------------------------------------------------------------------------------
| Id  | Operation                         | Name                       | Rows  | Bytes | Cost (%CPU)| Time     |
----------------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT                  |                            |     1 |    39 |    10   (0)| 00:00:01 |
|   1 |  TEMP TABLE TRANSFORMATION        |                            |       |       |            |          |
|   2 |   LOAD AS SELECT                  |                            |       |       |            |          |
|*  3 |    VIEW                           |                            |     1 |    13 |     2   (0)| 00:00:01 |
|*  4 |     CONNECT BY WITHOUT FILTERING  |                            |       |       |            |          |
|   5 |      FAST DUAL                    |                            |     1 |       |     2   (0)| 00:00:01 |
|   6 |   SORT AGGREGATE                  |                            |     1 |    39 |            |          |
|*  7 |    FILTER                         |                            |       |       |            |          |
|   8 |     NESTED LOOPS                  |                            |     1 |    39 |     6   (0)| 00:00:01 |
|   9 |      NESTED LOOPS                 |                            |     1 |    26 |     4   (0)| 00:00:01 |
|* 10 |       VIEW                        |                            |     1 |    13 |     2   (0)| 00:00:01 |
|  11 |        COUNT                      |                            |       |       |            |          |
|* 12 |         VIEW                      |                            |     1 |    13 |     2   (0)| 00:00:01 |
|  13 |          TABLE ACCESS FULL        | SYS_TEMP_0FD9D6612_16B6486 |     1 |    13 |     2   (0)| 00:00:01 |
|* 14 |       VIEW                        |                            |     1 |    13 |     2   (0)| 00:00:01 |
|  15 |        TABLE ACCESS FULL          | SYS_TEMP_0FD9D6612_16B6486 |     1 |    13 |     2   (0)| 00:00:01 |
|* 16 |      VIEW                         |                            |     1 |    13 |     2   (0)| 00:00:01 |
|  17 |       TABLE ACCESS FULL           | SYS_TEMP_0FD9D6612_16B6486 |     1 |    13 |     2   (0)| 00:00:01 |
|* 18 |     VIEW                          |                            |     1 |    13 |     2   (0)| 00:00:01 |
|  19 |      COUNT                        |                            |       |       |            |          |
|* 20 |       CONNECT BY WITHOUT FILTERING|                            |       |       |            |          |
|  21 |        FAST DUAL                  |                            |     1 |       |     2   (0)| 00:00:01 |
----------------------------------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   3 - filter(SUBSTR(TO_CHAR("X"),1,1)<>SUBSTR(TO_CHAR("X"),2,1) AND
              SUBSTR(TO_CHAR("X"),1,1)<>SUBSTR(TO_CHAR("X"),3,1) AND
              SUBSTR(TO_CHAR("X"),2,1)<>SUBSTR(TO_CHAR("X"),3,1) AND INSTR(TO_CHAR("X"),'0')=0)
   4 - filter(LEVEL<=865)
   7 - filter( NOT EXISTS (SELECT 0 FROM  (SELECT ROWNUM "L" FROM "SYS"."DUAL" "DUAL" CONNECT BY
              LEVEL<=9) "from$_subquery$_009" WHERE INSTR(TO_CHAR(:B1)||TO_CHAR(:B2)||TO_CHAR(:B3),TO_CHAR("L"),1,2)>0
              ))
  10 - filter("A"."X"<494)
  12 - filter("X"<494)
  14 - filter("A"."X"<"B"."X")
  16 - filter("A"."X"+"B"."X"-"C"."X"=0)
  18 - filter(INSTR(TO_CHAR(:B1)||TO_CHAR(:B2)||TO_CHAR(:B3),TO_CHAR("L"),1,2)>0)
  20 - filter(LEVEL<=9)

已选择45行。

已用时间:  00: 00: 00.06
SQL>


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

使用道具 举报

回复
论坛徽章:
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
12#
 楼主| 发表于 2010-6-10 15:27 | 只看该作者
奇怪啊

SQL> set timi on
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)
  2  and  substr(x,2,1)<>substr(x,3,1) and instr(x,0)=0),
  3  a as (select rownum rn,x from t where x<494)
  4  select count(*) from (
  5  select a.x||'+'||b.x||'-'||c.x||'==0' from a a,t b,t c
  6  where a.x+b.x-c.x=0 and a.x<494 and a.x<b.x --and b.x<c.x
  7  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))
  8  --and rownum<=10;

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

已用时间:  00: 00: 16.57
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 replace(replace(replace(
13        replace(replace(replace(
14        replace(replace(replace('123456789',substr(a.num,1,1)),substr(a.num,2,1)),substr(a.num,3,1)),
15                                            substr(b.num,1,1)),substr(b.num,2,1)),substr(b.num,3,1)),
16                                            substr(c.num,1,1)),substr(c.num,2,1)),substr(c.num,3,1)) is null
17        and a.num + b.num = c.num
18        and a.num < b.num
19        and b.num < c.num
20  );

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

已用时间:  00: 25: 47.70

使用道具 举报

回复
论坛徽章:
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
13#
 楼主| 发表于 2010-6-10 15:28 | 只看该作者
原帖由 szusunny 于 2010-6-10 14:05 发表
--假定第一个三位数最小
with tmp as(
select a.num from (select rownum as num from dual connect by rownum = 100)
--
select
a.num || '+' || b.num || '=' || c.num as str
from tmp a, tmp b, tmp c
where replace(replace(replace(
      replace(replace(replace(
      replace(replace(replace('123456789',substr(a.num,1,1)),substr(a.num,2,1)),substr(a.num,3,1)),
                                          substr(b.num,1,1)),substr(b.num,2,1)),substr(b.num,3,1)),
                                          substr(c.num,1,1)),substr(c.num,2,1)),substr(c.num,3,1)) is null
      and a.num + b.num = c.num
      and a.num < b.num
      and b.num < c.num

--result
           STR
1        186+273=459
2        183+276=459
3        176+283=459
4        173+286=459
5        195+273=468
6        193+275=468
7        175+293=468
8        173+295=468
9        129+357=486
10        127+359=486
11        159+327=486
12        157+329=486
13        128+367=495
14        127+368=495
15        168+327=495
16        167+328=495
17        187+362=549
18        182+367=549
19        162+387=549
20        167+382=549
21        139+428=567
22        138+429=567
23        129+438=567
24        128+439=567
25        219+348=567
26        218+349=567
27        249+318=567
28        248+319=567
29        194+382=576
30        192+384=576
31        184+392=576
32        182+394=576
33        278+316=594
34        276+318=594
35        216+378=594
36        218+376=594
37        187+452=639
38        182+457=639
39        157+482=639
40        152+487=639
41        297+351=648
42        291+357=648
43        257+391=648
44        251+397=648
45        218+439=657
46        239+418=657
47        238+419=657
48        219+438=657
49        183+492=675
50        182+493=675
51        192+483=675
52        193+482=675
53        294+381=675
54        291+384=675
55        284+391=675
56        281+394=675
57        218+475=693
58        215+478=693
59        278+415=693
60        275+418=693
61        186+543=729
62        183+546=729
63        146+583=729
64        143+586=729
65        192+546=738
66        196+542=738
67        146+592=738
68        142+596=738
69        159+624=783
70        154+629=783
71        129+654=783
72        124+659=783
73        219+564=783
74        214+569=783
75        269+514=783
76        264+519=783
77        154+638=792
78        158+634=792
79        138+654=792
80        134+658=792
81        246+573=819
82        243+576=819
83        362+457=819
84        357+462=819
85        367+452=819
86        352+467=819
87        273+546=819
88        276+543=819
89        145+692=837
90        142+695=837
91        246+591=837
92        241+596=837
93        192+645=837
94        195+642=837
95        296+541=837
96        291+546=837
97        319+527=846
98        317+529=846
99        329+517=846
100        327+519=846
101        293+571=864
102        291+573=864
103        273+591=864
104        271+593=864
105        139+725=864
106        135+729=864
107        129+735=864
108        125+739=864
109        254+619=873
110        219+654=873
111        214+659=873
112        259+614=873
113        257+634=891
114        254+637=891
115        237+654=891
116        234+657=891
117        327+564=891
118        367+524=891
119        364+527=891
120        324+567=891
121        275+643=918
122        273+645=918
123        346+572=918
124        342+576=918
125        245+673=918
126        243+675=918
127        376+542=918
128        372+546=918
129        381+546=927
130        386+541=927
131        346+581=927
132        341+586=927
133        154+782=936
134        152+784=936
135        184+752=936
136        182+754=936
137        183+762=945
138        182+763=945
139        163+782=945
140        162+783=945
141        327+618=945
142        318+627=945
143        317+628=945
144        328+617=945
145        283+671=954
146        281+673=954
147        273+681=954
148        271+683=954
149        216+738=954
150        238+716=954
151        236+718=954
152        218+736=954
153        215+748=963
154        248+715=963
155        245+718=963
156        218+745=963
157        358+614=972
158        354+618=972
159        318+654=972
160        314+658=972
161        246+735=981
162        245+736=981
163        236+745=981
164        235+746=981
165        357+624=981
166        354+627=981
167        327+654=981
168        324+657=981
--168 rows selected in 1.219 seconds

机器配置?oracle环境参数?

使用道具 举报

回复
论坛徽章:
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
14#
发表于 2010-6-10 15:41 | 只看该作者
机器配置: Intel Core2 6300 @ 1.86GHz; 1GB of RAM
OS: Windows XP(sp3)
Oracle: Oracle9i Release 9.2.0.5.0

使用道具 举报

回复
论坛徽章:
1088
金色在线徽章
日期:2007-04-25 04:02:08金色在线徽章
日期:2007-06-29 04:02:43金色在线徽章
日期:2007-03-11 04:02:02在线时间
日期:2007-04-11 04:01:02在线时间
日期:2007-04-12 04:01:02在线时间
日期:2007-03-07 04:01:022008版在线时间
日期:2010-05-01 00:01:152008版在线时间
日期:2011-05-01 00:01:342008版在线时间
日期:2008-06-03 11:59:43ITPUB年度最佳技术原创精华奖
日期:2013-03-22 13:18:30
15#
发表于 2010-6-10 15:43 | 只看该作者
原帖由 szusunny 于 2010-6-10 15:41 发表
机器配置: Intel Core2 6300 @ 1.86GHz; 1GB of RAM
OS: Windows XP(sp3)
Oracle: Oracle9i Release 9.2.0.5.0

晕,我在10g上没有反应啊!方便把执行计划贴下不?

[ 本帖最后由 dingjun123 于 2010-6-10 15:56 编辑 ]

使用道具 举报

回复
论坛徽章:
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
16#
 楼主| 发表于 2010-6-10 15:57 | 只看该作者

回复 #15 dingjun123 的帖子

找到窍门了
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 /*+rule */
10  a.num || '+' || b.num || '=' || c.num as str
11  from tmp a, tmp b, tmp c
12  where replace(replace(replace(
13        replace(replace(replace(
14        replace(replace(replace('123456789',substr(a.num,1,1)),substr(a.num,2,1)),substr(a.num,3,1)),
15                                            substr(b.num,1,1)),substr(b.num,2,1)),substr(b.num,3,1)),
16                                            substr(c.num,1,1)),substr(c.num,2,1)),substr(c.num,3,1)) is null
17        and a.num + b.num = c.num
18        and a.num < b.num
19        and b.num < c.num
20  );

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

已用时间:  00: 00: 00.65

使用道具 举报

回复
论坛徽章:
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
17#
 楼主| 发表于 2010-6-10 16:00 | 只看该作者
我的用了/*+rule*/退化了

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)
  2  and  substr(x,2,1)<>substr(x,3,1) and instr(x,0)=0),
  3  a as (select rownum rn,x from t where x<494)
  4  select count(*) from (
  5  select /*+rule*/ a.x||'+'||b.x||'-'||c.x||'==0' from a a,t b,t c
  6  where a.x+b.x-c.x=0 and a.x<494 and a.x<b.x --and b.x<c.x
  7  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))
  8  --and rownum<=10;

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

已用时间:  00: 00: 48.39

使用道具 举报

回复
论坛徽章:
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
18#
 楼主| 发表于 2010-6-10 16:09 | 只看该作者
收集统计信息
SQL> create table 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);

表已创建。

已用时间:  00: 00: 00.62
SQL> exec DBMS_STATS.GATHER_TABLE_STATS('LT','TMP');

PL/SQL 过程已成功完成。

已用时间:  00: 00: 00.73
SQL> select count(*) from(
  2  select
  3  a.num || '+' || b.num || '=' || c.num as str
  4  from tmp a, tmp b, tmp c
  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
10        and a.num + b.num = c.num
11        and a.num < b.num
12        and b.num < c.num
13  );

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

已用时间:  00: 00: 00.24
SQL> explain plan for select count(*) from(
  2  select
  3  a.num || '+' || b.num || '=' || c.num as str
  4  from tmp a, tmp b, tmp c
  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
10        and a.num + b.num = c.num
11        and a.num < b.num
12        and b.num < c.num
13  );

已解释。

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

PLAN_TABLE_OUTPUT
------------------------------------------------------------------------------------------------------------------------------------
Plan hash value: 2905956015

------------------------------------------------------------------------------
| Id  | Operation             | Name | Rows  | Bytes | Cost (%CPU)| Time     |
------------------------------------------------------------------------------
|   0 | SELECT STATEMENT      |      |     1 |    12 |    12  (25)| 00:00:01 |
|   1 |  SORT AGGREGATE       |      |     1 |    12 |            |          |
|*  2 |   HASH JOIN           |      |  3163 | 37956 |    12  (25)| 00:00:01 |
|   3 |    TABLE ACCESS FULL  | TMP  |   504 |  2016 |     3   (0)| 00:00:01 |
|   4 |    MERGE JOIN         |      |   126K|   990K|     8  (25)| 00:00:01 |
|   5 |     SORT JOIN         |      |   504 |  2016 |     4  (25)| 00:00:01 |
|   6 |      TABLE ACCESS FULL| TMP  |   504 |  2016 |     3   (0)| 00:00:01 |
|*  7 |     SORT JOIN         |      |   504 |  2016 |     4  (25)| 00:00:01 |
|   8 |      TABLE ACCESS FULL| TMP  |   504 |  2016 |     3   (0)| 00:00:01 |
------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   2 - access("C"."NUM"="A"."NUM"+"B"."NUM")
       filter(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(RE
              PLACE(REPLACE('123456789',SUBSTR(TO_CHAR("A"."NUM"),1,1)),SUBSTR(TO_CHAR
              ("A"."NUM"),2,1)),SUBSTR(TO_CHAR("A"."NUM"),3,1)),SUBSTR(TO_CHAR("B"."NU
              M"),1,1)),SUBSTR(TO_CHAR("B"."NUM"),2,1)),SUBSTR(TO_CHAR("B"."NUM"),3,1)
              ),SUBSTR(TO_CHAR("C"."NUM"),1,1)),SUBSTR(TO_CHAR("C"."NUM"),2,1)),SUBSTR
              (TO_CHAR("C"."NUM"),3,1)) IS NULL AND "B"."NUM"<"C"."NUM")
   7 - access("A"."NUM"<"B"."NUM")
       filter("A"."NUM"<"B"."NUM")

已选择28行。

已用时间:  00: 00: 00.03


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

使用道具 举报

回复
论坛徽章:
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
19#
 楼主| 发表于 2010-6-10 16:16 | 只看该作者
我的写法在收集统计信息后还是不快

SQL> select count(*) from (
  2  select a.num||'+'||b.num||'-'||c.num||'==0' from tmp a,tmp b,tmp c
  3  where a.num+b.num-c.num=0 and a.num<494 and a.num<b.num --and b.num<c.num
  4  and not exists(select 1 from (select rownum l from dual connect by level<=9)where instr(a.num||b.num||c.num ,l,1,2)>0))
  5  ;

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

已用时间:  00: 00: 11.16

使用道具 举报

回复
论坛徽章:
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
20#
 楼主| 发表于 2010-6-10 16:19 | 只看该作者

增加一个过滤条件好了点

SQL> 3
  3* where a.num+b.num-c.num=0 and a.num<494 and a.num<b.num --and b.num<c.num
SQL> c/--/
  3* where a.num+b.num-c.num=0 and a.num<494 and a.num<b.num and b.num<c.num
SQL> /

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

已用时间:  00: 00: 04.94

使用道具 举报

回复

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

本版积分规则 发表回复

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