|
|
我的机器运行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 编辑 ] |
|