|
优化关键在于12/34,87/65做join.
- WITH
- p12 (srcblock,destblock,tst,pathstr) AS (
- SELECT /*+ */ a.n,b.n,a.st+b.st,cast(chr(a.n)||chr(b.n) as varchar2(100))
- from grid a , grid b
- where
- a.n=1 -- Start with first block
- and mod(b.n,8) in (1,2)
- and b.n in (
- a.n + 1,
- a.n - 7,
- a.n + 9,
- a.n + 8,
- a.n - 8)
- UNION ALL
- SELECT
- a.srcblock,b.n,
- a.tst + b.st,--st
- pathstr||chr(b.n)
- FROM p12 a , (select * from grid where mod(n,8) in (1,2)) b
- WHERE b.n in (
- a.destblock + 1,
- a.destblock - 7,
- a.destblock + 9,
- a.destblock + 8,
- a.destblock - 8
- ) and b.n!=1
- AND a.tst + b.st <=:M
- and instr(pathstr,chr(n))=0
- ),
- pp12 as (
- select /*+ */ distinct destblock,tst from p12
- where mod(destblock,8)=2
- ),
- p34 (srcblock,destblock,tst,pathstr) AS (
- SELECT a.n,b.n,a.st+b.st,cast(chr(a.n)||chr(b.n) as varchar2(100))
- from grid a , grid b
- where mod(a.n,8) =3 -- Start with first block
- and b.n in (
- a.n + 1,
- a.n - 7,
- a.n + 9,
- a.n + 8,
- a.n - 8
- )
- and mod(a.n,8) in (3,4)
- UNION ALL
- SELECT
- a.srcblock,b.n,--出发、到达block
- a.tst + b.st,--st
- pathstr||chr(b.n)
- FROM p34 a , (select * from grid where mod(n,8) in (3,4)) b
- WHERE b.n in (
- a.destblock + 1,
- a.destblock - 7,
- a.destblock + 9,
- a.destblock + 8,
- a.destblock - 8
- )
- AND a.tst + b.st <:M
- and instr(pathstr,chr(n))=0
- ) ,
- pp34 as (
- select /*+ */ distinct srcblock,destblock,tst from p34
- where mod(destblock,8)=4
- ) ,
- p14 as(
- select /*+ use_concat ordered use_hash(a b) */ distinct b.destblock,a.tst+b.tst tst
- from pp12 a,pp34 b
- where
- b.srcblock in (
- a.destblock + 1 ,
- a.destblock - 7 ,
- a.destblock + 9
- )
- AND a.tst + b.tst <:M
- ),
- p87 (srcblock,destblock,tst,pathstr) AS (
- SELECT /*+ */a.n,b.n,a.st+b.st,cast(chr(a.n)||chr(b.n) as varchar2(100))
- from grid a , grid b
- where a.n=64 -- Start with last block
- and b.n in (
- a.n - 1,
- a.n - 9,
- a.n + 7,
- a.n + 8,
- a.n - 8
- )
- and mod(b.n,8) in (0,7)
- UNION ALL
- SELECT
- a.srcblock,b.n,--出发、到达block
- a.tst + b.st,--st
- pathstr||chr(b.n)
- FROM p87 a , (select * from grid where mod(n,8) in (0,7)) b
- WHERE b.n in (
- a.destblock - 1,
- a.destblock - 9,
- a.destblock + 7,
- a.destblock + 8,
- a.destblock - 8
- ) and b.n!=64
- AND a.tst + b.st <:M
- and instr(pathstr,chr(n))=0
- ),
- pp87 as (
- select /*+ */ distinct destblock,tst from p87
- where mod(destblock,8)=7
- ),
- p65 (srcblock,destblock,tst,pathstr) AS ( --先探测3级内的路径
- SELECT a.n,b.n,a.st+b.st,cast(chr(a.n)||chr(b.n) as varchar2(100))
- from grid a , grid b
- where mod(a.n,8) =6 -- Start with first block
- and b.n in (
- a.n - 1,
- a.n - 9,
- a.n + 7,
- a.n + 8,
- a.n - 8
- )
- and mod(a.n,8) in (5,6)
- UNION ALL
- SELECT
- a.srcblock,b.n,--出发、到达block
- a.tst + b.st,--st
- pathstr||chr(b.n)
- FROM p65 a , (select * from grid where mod(n,8) in (5,6)) b
- WHERE b.n in (
- a.destblock - 1,
- a.destblock - 9,
- a.destblock + 7,
- a.destblock + 8,
- a.destblock - 8
- )
- AND a.tst + b.st <=:M
- and instr(pathstr,chr(n))=0
- )
- ,
- pp65 as (
- select /*+ */ distinct srcblock,destblock,tst from p65
- where mod(destblock,8)=5
- ) ,
- p85 as(
- select /*+ use_concat ordered use_hash(a b) */ distinct b.destblock,b.tst+a.tst tst
- from pp87 a,pp65 b
- where
- b.srcblock in (
- a.destblock - 1 ,
- a.destblock + 7 ,
- a.destblock - 9
- )
- and b.tst+a.tst<:M
- ),
- p18 as (
- select /*+use_hash(a b) */'Y' f from p14 a,p85 b
- where
- b.destblock in (
- a.destblock + 1 ,
- a.destblock - 7 ,
- a.destblock + 9
- ) and b.tst = :M - a.tst
- and rownum = 1
- )
- select nvl(max(f),'N') from p18
复制代码 |
|