Link to home
Start Free TrialLog in
Avatar of Paulconsulting
Paulconsulting

asked on

T-SQL Query Optimization Minimum Distance

I have a table that is a cross join on itself to find the distance between all points.  The structure is basically.

initial table: Parcels
ParcelID, X, Y

the derived distance table is built via the attached code and it's structure is:
ParcelID1, ParcelID2, Distance.

I now need a table that has the following structure:
ParcelID, ClosestNegihbor1,ClosestNegihbor2,...ClosestNegihbor5, Distance1, Distance2...Distance5

There are 37 million records in the derived table.  There are 6130 ParcelID's for which I need to calculate their 5 closest neighbors.  How should I approach building the final output table?

Select	p.parcelID as ParcelID,
		l.parcelID as ParcelID2, 
		dbo.distance(
			(dbo.x(p.parcelid))
			,(dbo.x(l.parcelid))
			,(dbo.y(p.parcelid))
			,(dbo.y(l.parcelid))
		) as Distance
into DistanceTable
from liberty p
cross join liberty l 
where p.parcelID <> l.parcelID

Open in new window

Avatar of bull_rider
bull_rider
Flag of India image

Can you provide the actual structure of the table along with the datatypes of each column? And could you provide some rows from the table for me to better understand your needs?
ASKER CERTIFIED SOLUTION
Avatar of FVER
FVER

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Rob Farley
Ok... I wouldn't use scalar functions to create your table - that must take a long time in itself. There's lots of information out there about why this might be a bad idea.

But...

Why not use SQL 2008, use a geometry type, and put some spatial indexes in place. Then it should be very easy to find the five closest items, because the indexing will help. The query above should then work better, because the system will be better able to find the top 5 closest items.

Rob
This is what Rob is referring to:
Investigating the new Spatial Types in SQL Server 2008 - Part 1
http://www.sqlservercentral.com/articles/SQL+Server+2008/64601/
Investigating the new Spatial Types in SQL Server 2008 - Part 2
http://www.sqlservercentral.com/articles/Spatial+Data/64734/
Avatar of Paulconsulting
Paulconsulting

ASKER

Excellent!  Exactly what I needed thank you.