0

1.Spatial index does not work(Query cost 10s)

SELECT geomFROM cj_pl_l WHERE geom.MakeValid ( ).STWithin (geometry :: STGeomFromText 
('POLYGON ((……))', 4547 ).MakeValid ()) =1

2.Spatial index work(Query cost 2s,I save above-mentioned polygon to temp table[testGeom])

select b.* from  testGeom a,cj_pl_l b where b.geom.STWithin(a.geom)=1

If I want Condition1 the Spatial Index to work, how should I solve?

Thanks!

5
  • 1
    My first guess is that the MakeVald() call on the table side of the query is the culprit. Why is that necessary? Do you have invalid data in your table? Commented Dec 3, 2021 at 3:48
  • Due to the nature of spatial indexes they only support a limited number of predicate forms. Have you read the Methods Supported by Spatial Indexes documentation yet? Commented Dec 3, 2021 at 4:11
  • @AlwaysLearning I did,the STWhin method is supported. Commented Dec 6, 2021 at 2:11
  • @BenThul Because the sql run error. System.ArgumentException: at Microsoft.SqlServer.Types.SqlGeometry.ThrowIfInvalid() at Microsoft.SqlServer.Types.SqlGeometry.STWithin(SqlGeometry other) When I use other table and don't use MakeVald() method,the sql not thorw error,but the Spatial Index also don't work. Commented Dec 6, 2021 at 2:24
  • 1
    What I'm saying is that if you have to continually run MakeValid() against the data in your table, you have invalid data. If suggest fixing the data (something like update yourTable set yourColumn = yourColumn MakeValid(). Commented Dec 6, 2021 at 3:03

1 Answer 1

0

The culprit is the first MakeValid in geom.MakeValid ().

The index covers geom column, but MakeValid returns a new geometry value, which might be different and is not indexed, and thus SQL cannot use it.

Remove the first MakeValid call, preferably after making sure all the geometries in the table are valid. You generally don't want to fix stored geometries with MakeValid anywhere in the query path - it is slow and it prevents index usage.

That said, second MakeValid, on the constant geometry value, should be fine.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you suggestion, I tried,you can see comment above. When I use other spatial table and don't use MakeVald() method,but the Spatial Index also don't work.
Can you please update the question? It currently says in the second case the index works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.