Top 5 Recent Articles
ARTICLES CATEGORIES
- Algorithms (22)
- All (399)
- Biography (1)
- Blog (44)
- Business Requirements (1)
- Commentary (1)
- Conversion (2)
- Customers (2)
- Data Models (1)
- Education (2)
- GeoRaptor (13)
- GPS (1)
- Image Processing (2)
- Import Export (8)
- Licensing (2)
- LiDAR (1)
- Linear Referencing (4)
- Manifold GIS (3)
- Mapping (1)
- MySQL Spatial (7)
- Networking and Routing (including Optimization) (5)
- Open Source (18)
- Oracle Spatial and Locator (194)
- Partitioning (1)
- PostGIS (36)
- Projections (1)
- Published Articles (1)
- qGIS (1)
- Recommendations (1)
- Services (1)
- Software Change Log (1)
- Source Code (37)
- Space Curves (9)
- Spatial Database Functions (109)
- Spatial DB comparison (1)
- Spatial XML Processing (11)
- SQL Server Spatial (92)
- Standards (3)
- Stored Procedure (17)
- Tessellation or Gridding (10)
- Tools (2)
- Topological Relationships (1)
- Training (2)
- Triangulation (2)
On hinting spatial indexes
Recently, using SQL Server 2008 (not R2), I came across an issue with hinting a SELECT statement with a spatial index. This particular issue is different from the one Bob Beauchem wrote about in his article On hinting spatial indexes and query complexity (in turn he references Isaac Kunen’s Is my spatial index being used?).
The difference with the issue I found is to do with a very simple query that uses a constructed geometry. Here is the original coding of the query:
SELECT COUNT(*) FROM dbo.admin_sorted a WITH (INDEX (admin_sorted_geom)) WHERE a.geom.STIntersects(geometry::STLineFromText('LINESTRING(320000,5340000,321000,5341000)',a.geom.STSrid)) = 1; -- Result Msg 8635, Level 16, State 8, Line 1 The query processor could NOT produce a query plan FOR a query WITH a spatial INDEX hint. Reason: The spatial parameter REFERENCES a COLUMN that IS defined below the predicate. Try removing the INDEX hints OR removing SET FORCEPLAN.
Now, there could not be anything more simple that this query, so why is an error being thrown?
Following a hunch I discovered (I think) that the geometry::STLineFromText() constructor is the culprit. On this basis, I played around with the query and, using a CTE, came up with the following that correctly uses the spatial index.
Note: The reason for the “(SELECT TOP 1…. )” in the CTE is because I want the constructed linestring to have the same SRID as the host table.
WITH sg AS ( SELECT geometry::STLineFromText('LINESTRING(300000 5200000,500000 5400000)',a.Geom.STSrid) AS geom FROM (SELECT TOP 1 geom FROM dbo.admin_sorted) a ) SELECT COUNT(*) AS AdminCount FROM sg a, dbo.admin_sorted b WITH (INDEX (admin_sorted_geom)) WHERE b.geom.STIntersects(a.geom) = 1; -- Results AdminCount ---------- 101
Here is that part of the actual explanation plan showing the use of the spatial index.
Perhaps this is no longer an issue in R2 or Denali.
Documentation
- MySQL Spatial General Functions
- Oracle LRS Objects
- Oracle Spatial Exporter (Java + pl/SQL)
- Oracle Spatial Object Functions
- Oracle Spatial Object Functions (Multi Page)
- PostGIS pl/pgSQL Functions
- SC4O Oracle Java Topology Suite (Java + pl/SQL)
- SQL Server Spatial General TSQL Functions
- SQL Server Spatial LRS TSQL Functions