|
udfrog 发表于 2012-8-16 08:35 ![]()
可以尽早的把重复路线去掉,这样当棋盘变宽时,性能优势就出来了.
确实
SQL> with t as (
2 select c, r
3 from (select rownum c from dual connect by rownum<=18),
4 (select rownum r from dual connect by rownum<=18)
5 )
6 select count(*)
7 from t a, t b, t c
8 where (a.c+1<b.c or (a.c+1=b.c and abs(a.r-b.r)>1) or (a.c=b.c and a.r+1<b.r))
9 and (b.c+1<c.c or (b.c+1=c.c and abs(b.r-c.r)>1) or (b.c=c.c and b.r+1<c.r))
10 and (a.c+1<c.c or (a.c+1=c.c and abs(a.r-c.r)>1) or (a.c=c.c and a.r+1<c.r));
COUNT(*)
----------
5239808
已用时间: 00: 00: 04.33
SQL> with t as(select level l,trunc((level-1)/18)r,mod(level-1,18)c from dual connect by level<=18*18)
2 select count(*)from t a, t b, t c
3 where
4 a.l<b.l and b.l<c.l
5 and power(b.r-a.r,2)+power(b.c-a.c,2)>2.5
6 and power(c.r-b.r,2)+power(c.c-b.c,2)>2.5
7 and power(c.r-a.r,2)+power(c.c-a.c,2)>2.5
8 ;
COUNT(*)
----------
5239808
已用时间: 00: 00: 07.06
SQL> with t as(select level l from dual connect by level<=20*20)
2 select count(*)from t a, t b, t c
3 where a.l>=20 and c.l<=19*20 and a.l<b.l and b.l-a.l not in(1,19,20,21)
4 and b.l<c.l and c.l-b.l not in(1,19,20,21)
5 and c.l-a.l not in(1,19,20,21)
6 and mod(a.l,20)not in(0,1)
7 and mod(b.l,20)not in(0,1)
8 and mod(c.l,20)not in(0,1)
9 ;
COUNT(*)
----------
5239808
已用时间: 00: 00: 09.96 |
|