I have a large table (~1.5m rows) with spatial location (X and Y and a geometry field). I need to create a smoothed version of one of the values in this table by averaging with nearby points. I want the influence of each point to be the inverse of the square of this distance (this is sometimes called gravity weighting).
The problem is that the query is incredibly slow and I have never got it to finish even after >50 hours running. Does anyone have any suggestions on how I could improve the performance please? I've tried with a subset of the data (1000 rows from the table) and it calculates in <10 seconds.
The geometry field has a spatial index and the query stats show that it is being used.
SELECT t1.[X]
,t1.[Y]
,sum(t2.myvar*t2.persons/(power((t1.eastings-t2.eastings)/100+1,2)+power((t1.northings-t2.northings)/100+1,2)+1)) as wtXfit
,sum(t2.persons/(power((t1.eastings-t2.eastings)/100+1,2)+power((t1.northings-t2.northings)/100+1,2)+1)) as wt
FROM [Faststore].[dbo].[mytable] t1
full outer join [Faststore].[dbo].[mytable] t2 on t1.geom.STDistance(t2.geom)<=500
group by t1.[X]
,t1.[Y]
Sample of the data:
I have a primary key on the column ID and geometry auto grid index on GEOM.
I've also tried using the X and Y columns <500 instead (with an index on those) but get similarly poor performance. The main problem as AlwaysLearning commented is that it is trying to do 1.5m*1.5 calculations rather than just using the nearby points.

geom.STBuffer(500)and then querying for neighbors withgeom.STIntersects(buffer) = 1or similar. - Ben Thul