|
怪哉,拆开分为3个sql,非常快
SQL> var n number;
SQL> exec :n:=7700000
PL/SQL 过程已成功完成。
已用时间: 00: 00: 00.01
SQL> var n number;
SQL> exec :n:=7700000
PL/SQL 过程已成功完成。
已用时间: 00: 00: 00.00
SQL> create table t_b as
2 with t as (select level l from dual connect by level<=7)
3 ,tb as
4 (
5 select to_number(t1.l||t2.l||t3.l||t4.l||t5.l||t6.l||t7.l) s
6 from t t1,t t2,t t3,t t4,t t5,t t6,t t7
7 where t7.l in ('1','3','5','7')
8 and t1.l not in( t2.l,t3.l,t4.l,t5.l,t6.l,t7.l)
9 and t2.l not in(t1.l, t3.l,t4.l,t5.l,t6.l,t7.l)
10 and t3.l not in(t1.l,t2.l, t4.l,t5.l,t6.l,t7.l)
11 and t4.l not in(t1.l,t2.l,t3.l, t5.l,t6.l,t7.l)
12 and t5.l not in(t1.l,t2.l,t3.l,t4.l, t6.l,t7.l)
13 and t6.l not in(t1.l,t2.l,t3.l,t4.l,t5.l, t7.l)
14 and t7.l not in(t1.l,t2.l,t3.l,t4.l,t5.l,t6.l )
15 )
16 select * from tb;
表已创建。
已用时间: 00: 00: 00.16
SQL> create table p_1k(rn number(8,0));
表已创建。
已用时间: 00: 00: 00.01
SQL> insert into p_1k
2 with ta as
3 (select level*2+1 l from dual connect by level<=sqrt(:N)/2),
4 t1 as (select level*2+1 l from dual connect by level<=sqrt(:N)/3)
5 ,p1k as(select l rn from ta --1000以内的质数
6 minus
7 select /*+ USE_MERGE (t1 t2) */ t1.l*t2.l from t1,t1 t2 where t1.l<=sqrt(sqrt(:N))
8 and t1.l<=t2.l and t1.l*t2.l<=:N
9 )
10 select * from p1k;
已创建402行。
已用时间: 00: 00: 00.08
SQL> select max(s) from t_b
2 where not exists(select 1 from p_1k where mod(s,rn)=0 and rn<=sqrt(s) and s is not null);
MAX(S)
----------
7652413
已用时间: 00: 00: 00.20
SQL> explain plan for select max(s) from t_b
2 where not exists(select 1 from p_1k where mod(s,rn)=0 and rn<=sqrt(s) and s is not null);
已解释。
已用时间: 00: 00: 00.00
SQL> select * from table(dbms_xplan.display);
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------------------------------------------------------------------
Plan hash value: 3757790328
-----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 13 | 275 (1)| 00:00:04 |
| 1 | SORT AGGREGATE | | 1 | 13 | | |
|* 2 | FILTER | | | | | |
| 3 | TABLE ACCESS FULL | T_B | 2880 | 37440 | 4 (0)| 00:00:01 |
|* 4 | FILTER | | | | | |
|* 5 | TABLE ACCESS FULL| P_1K | 1 | 13 | 3 (0)| 00:00:01 |
-----------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - filter( NOT EXISTS (SELECT 0 FROM "P_1K" "P_1K" WHERE :B1 IS NOT
NULL AND MOD(:B2,"RN")=0 AND "RN"<=SQRT(:B3)))
4 - filter(:B1 IS NOT NULL)
5 - filter(MOD(:B1,"RN")=0 AND "RN"<=SQRT(:B2))
Note
-----
- dynamic sampling used for this statement (level=2)
已选择24行。
已用时间: 00: 00: 00.03
SQL>
|
|
|