|
52楼单个SQL的问题在于 test2, 里面有个笛卡尔积:
select t1.p||t2.p as num
from test t1,test t2
where (t1.p<>t2.p)
然后再带上三个EXISTS过滤,当然慢了。
我给你改了一下还好点,但是因为总体思路不佳,再改也好不到哪去,你要是有兴趣就按此思路自己把剩下的做完。
with tm as (select lpad(level,4,'0') lv from dual connect by level<=9999)
,test as (select lv p
,p1
,p1+p2 as p12
,p1+p2+p3 as p123
,p2+p3+p4 as p234
,p3+p4 as p34
,p4
from (select TO_NUMBER(substr(lv,1,1)) as p1, TO_NUMBER(substr(lv,2,1)) as p2
,TO_NUMBER( substr(lv,3,1)) as p3, TO_NUMBER(substr(lv,4,1)) as p4,lv from tm)
where p1+p2+p3+p4 in (1,4,9,16,25,36)
and REGEXP_COUNT(lv,0) in (0,1,2)
and REGEXP_COUNT(lv,1) in (0,1,2)
and REGEXP_COUNT(lv,2) in (0,1,2)
and REGEXP_COUNT(lv,3) in (0,1,2)
and REGEXP_COUNT(lv,4) in (0,1,2)
and REGEXP_COUNT(lv,5) in (0,1,2)
and REGEXP_COUNT(lv,6) in (0,1,2)
and REGEXP_COUNT(lv,7) in (0,1,2)
and REGEXP_COUNT(lv,8) in (0,1,2)
and REGEXP_COUNT(lv,9) in (0,1,2)
)
,test2 as (select num p from (select num
,substr(num,2,4) as num11
,substr(num,3,4) as num12
,substr(num,4,4) as num13
from (select t1.p||t2.p as num
from test t1,test t2
where (t1.p<>t2.p)
AND t1.p234+t2.p1 IN (4,9,16,25,36)
AND t1.p34+t2.p12 IN (4,9,16,25,36)
AND t1.p4+t2.p123 IN (4,9,16,25,36)
)
where REGEXP_COUNT(num,0) in (0,1,2)
and REGEXP_COUNT(num,1) in (0,1,2)
and REGEXP_COUNT(num,2) in (0,1,2)
and REGEXP_COUNT(num,3) in (0,1,2)
and REGEXP_COUNT(num,4) in (0,1,2)
and REGEXP_COUNT(num,5) in (0,1,2)
and REGEXP_COUNT(num,6) in (0,1,2)
and REGEXP_COUNT(num,7) in (0,1,2)
and REGEXP_COUNT(num,8) in (0,1,2)
and REGEXP_COUNT(num,9) in (0,1,2)
) a
--where exists (select 1 from test where p=a.num11)
-- and exists (select 1 from test where p=a.num12)
-- and exists (select 1 from test where p=a.num13)
)
SELECT * FROM TEST2
|
|