recursion - Nearest record with recursive SQL Server CTE and stepped STDistance -
when improving closest spatial match performance (geography stdistance) in sql server 2012, have found iteratively stepping search radius increases performance on our datasets.
i have below 3 step query trying turn recursive cte max depth 3, distance comparison [level]*500.
select n.workid, m.workid matchworkid, n.location.stdistance(m.location) meters #matchwork500 newwork n cross apply (select top (1) c.workid, c.location currwork c n.location.stdistance(c.location) <= 500 order n.location.stdistance(c.location)) m select n.workid, m.workid matchworkid, n.location.stdistance(m.location) meters #matchwork1000 newwork n left join #matchwork500 m500 on m500.workid = n.workid cross apply (select top (1) c.workid, c.location currwork c n.location.stdistance(c.location) <= 1000 order n.location.stdistance(c.location)) m m500.workid null select n.workid, m.workid matchworkid, n.location.stdistance(m.location) meters #matchwork1000 newwork n left join #matchwork500 m500 on m500.workid = n.workid left join #matchwork1000 m1000 on m1000.workid = n.workid cross apply (select top (1) c.workid, c.location currwork c n.location.stdistance(c.location) <= 1500 order n.location.stdistance(c.location)) m m500.workid null , m1000.workid null each iteration needs process had not been matched previously, single cte table preferably. please no stored procedure answers, assistance appreciated.
wiki
Comments
Post a Comment