开发者

SQL Slow Spatial Data

I have two tables one called Point and other called Poly i have around 400 polygons in the Poly table and around 200 thousand points in Point table. what i wanted is to find how many points are there in a certain polygon. The query i am using is:

select COUNT(*) from point p
inner join Poly pl  on pl.GeoDataID = 101 and p.geopoint.STIntersects(pl.Poly)=1 

I have also created Spatial Indexes on both G开发者_StackOverflow社区eoPoint and Poly Columns and both columns are geography type.

Edit:Using SQL Server 2008

Question: Above query returns results in around 1 min to 40 secs that is too slow for my case because i wanted to show them on Google Maps on real time. Is my approach for this problem is right? or it there any better way to achieve this?


First check your spatial indexes. You can create them using a SQL statement such as

CREATE SPATIAL INDEX MyIndexName ON MyTable(GeomFieldName) USING GEOMETRY_GRID
WITH (
BOUNDING_BOX =(-1493907.5664457313, 6128509.51667404, -578861.3521250226, 7703103.135644257),
GRIDS =(LEVEL_1 = MEDIUM,LEVEL_2 = MEDIUM,LEVEL_3 = MEDIUM,LEVEL_4 = MEDIUM),
CELLS_PER_OBJECT = 16, PAD_INDEX  = OFF, SORT_IN_TEMPDB = OFF,
DROP_EXISTING = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON)

A key parameter to change is the BOUNDING_BOX. This should be set to the bounding box of your data, or maximum bounding box of expected data. So if your data is restricted to North America, set the bounding box to the extent of North America rather than the world.

You can add calculated fields to show the bound of each feature as noted at http://geographika.co.uk/sql-server-spatial-sql - and can then use a standard query to see the maximum extents.

Also check using profiler to see that the indexes are actually being used. Sometimes you have to force their use with the WITH (INDEX (MyIndexName)) statement.

SELECT *
FROM MyTable
WITH (INDEX (MyIndexName))
WHERE (geometry::Point(@x,@y,3785).STWithin(MyGeomField) = 1)

I have also found that sometimes it is quicker when querying by points not to use a spatial index, so it is worth experimenting.

Alternatives

A final option would be to create a trigger when a new record is added that would assign it a polygon ID when it is created or updated. This would allow almost instant queries on your data.

And Finally

In the latest Denali SQL Server release spatial indexes can be created with an auto option rather than manually. There has apparently also been a performance boost for STWithin and STIntersects.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜