0

I am using Sql Server 2012,

I am trying to do something like

SELECT T1.*
FROM (SELECT DISTINCT COMMUNITY FROM [TTDS].[dbo].[EXPORT_OUTPUT] WHERE COMMUNITY <> '') T1
    LEFT JOIN (SELECT City FROM [TTDS].[dbo].[CITY]) T2 ON (T1.COMMUNITY = T2.City)
WHERE T2.City IS NULL 

Where EXPORT_OUTPUT has a field COMMUNITY that has a bunch of duplicated values that may or may not be in CITY

For example COMMUNITY may have a,a,a,a,b,b,c,d,e and the CITY.City has b,d,e then it would output just a,c


This works all fine and well. This is the use-case but now instead of doing that with varchar fields, I want to do it with geometry. My attempt at it works but is very very slow.

SELECT T1.*
FROM (

    SELECT cast(UNIQUE_GEOM as geometry) [geometry_field]
    FROM
    (
        Select cast(Shape as varbinary(max)) as UNIQUE_GEOM FROM [TTDS].[dbo].[EXPORT_OUTPUT]
        UNION
        SELECT cast(Shape as varbinary(max)) FROM [TTDS].[dbo].[EXPORT_OUTPUT]
    ) GET_UNIQUE_GEOM

) T1
    LEFT JOIN (SELECT [geometry] FROM [TTDS].[dbo].[LINKDIR_GEOMETRY]) T2 ON (T1.[geometry_field].STEquals(T2.[geometry]) = 1)
WHERE T2.[geometry] IS NULL 

The T1 is the table which has DISTINCT geometry from EXPORT_OUTPUT.

What is a more performant way to do this?

4
  • why you cast Shape to varbinary and then again to geometry? And why the both union parts looks like is the same select? Aug 31, 2015 at 23:12
  • You cant directly compare geometry. I saw another SO: ( stackoverflow.com/questions/13752176/… ) that recommended casting to binary and doing this UNION with itself to find the distinct geometry in a table
    – ParoX
    Aug 31, 2015 at 23:14
  • But the select union. Both select do the same? in that case should be SELECT distinct without union. Aug 31, 2015 at 23:15
  • Yes, this is to get the unique/distinct values. You cannot use distinct with geometry. If that was the case the answer would be trivial and almost identical to the varchar example.
    – ParoX
    Aug 31, 2015 at 23:21

1 Answer 1

0

Your problem is trying to join table by geometry using a function STEquals because that comparation take time specially without index.

For design database maybe have more sense

Have a table geometry_objects with fields geometry_id + geom

In that case you only put geometry_id both comunity and city

8
  • Both geometry fields have a spatial index on them. I have a table geometry_objects its what my LINKDIR_GEOMETRY that I am trying to see from. I am trying to denormalize the output into the LINKDIR_GEOMETRY table.
    – ParoX
    Aug 31, 2015 at 23:22
  • Again you ask for performace, And split the table as my suggestion will improve the search by great porcentage. Because you will be comparing integers instead of geoms. Also comunity and city table will be smaller in size because int take less bytes than geometry. Also if you update a geometry only have to update one row in geometry table instead all comunityand city rows with that geom Aug 31, 2015 at 23:25
  • Community and city aren't even involved with the problem. I am denormalizing [EXPORT_OUTPUT] by selecting the values that are not in LINKDIR_GEOMETRY and inserting them (I don't insert in my example but that is the end result).
    – ParoX
    Aug 31, 2015 at 23:27
  • I believe you are suggesting to store the intermediate step of finding distinct geometries into an indexed table and then execute the join on that. In that case it still wouldnt be joining on int
    – ParoX
    Aug 31, 2015 at 23:30
  • Sorry I just read you 'for example' comment, if your query isn't related to that then rewrite the example. Is hard to guess what is doing your query if the things doesnt match Aug 31, 2015 at 23:30

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.