<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Spatial join to get attributes of nearest neighbor in Data Management Questions</title>
    <link>https://community.esri.com/t5/data-management-questions/spatial-join-to-get-attributes-of-nearest-neighbor/m-p/280326#M16091</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You can force indexes through &lt;A href="https://msdn.microsoft.com/en-us/library/ms181714(v=sql.110).aspx"&gt;Query Hints (Transact - SQL)&lt;/A&gt;.&amp;nbsp; That said, forcing an index may not improve performance, sometimes it makes performance worse.&amp;nbsp; Since it seems you are in a development/testing phase and not writing production code yet, it might not hurt to experiment with forcing the different indexes to see what happens.&amp;nbsp; If forcing an index does help, especially dramatically, then the question becomes why isn't the query optimizer choosing to use either index.&amp;nbsp; &lt;EM&gt;By the way, I believe it is still true that SQL Server can only use one spatial index in a single query, but that may have changed&lt;/EM&gt;.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If you haven't already, you may want to review &lt;A href="https://msdn.microsoft.com/en-us/library/bb895265(v=sql.110).aspx"&gt;SQL Server 2012 - Spatial Indexes Overview&lt;/A&gt;.&amp;nbsp; The more you understand how MS has implemented spatial indexes, the less of a black box it will all seem, which makes for better SQL troubleshooting.&amp;nbsp; Also, there are some very specific syntax rules for maximizing a nearest neighbor type of query, which you are trying to do in some form.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Beyond reading MS's overview, there are some good blogs and MSDN discussions on tuning SQL Server spatial indexes.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Taking a quick glance at your specific code, you are missing some of the requirements for a spatial index to be used.&amp;nbsp; As the overview documentation I linked to above states:&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;&lt;STRONG&gt;Geometry Methods Supported by Spatial Indexes&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;Spatial indexes support the following set-oriented geometry methods under certain conditions: &lt;STRONG&gt;STContains&lt;/STRONG&gt;(), &lt;STRONG&gt;STDistance&lt;/STRONG&gt;(), &lt;STRONG&gt;STEquals&lt;/STRONG&gt;(), &lt;STRONG&gt;STIntersects&lt;/STRONG&gt;(), &lt;STRONG&gt;STOverlaps&lt;/STRONG&gt;(), &lt;STRONG&gt;STTouches&lt;/STRONG&gt;(), and &lt;STRONG&gt;STWithin&lt;/STRONG&gt;(). To be supported by a spatial index, these methods must be used within the WHERE or JOIN ON clause of a query, and they must occur within a predicate of the following general form:&lt;/P&gt;&lt;P&gt;....&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/BLOCKQUOTE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 30 Jan 2015 17:30:58 GMT</pubDate>
    <dc:creator>JoshuaBixby</dc:creator>
    <dc:date>2015-01-30T17:30:58Z</dc:date>
    <item>
      <title>Spatial join to get attributes of nearest neighbor</title>
      <link>https://community.esri.com/t5/data-management-questions/spatial-join-to-get-attributes-of-nearest-neighbor/m-p/280325#M16090</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I've got some data in an enterprise geodatabase (SQL Server 2012).&amp;nbsp; I want to create a view that joins points in a feature class to the nearest line in another feature class.&amp;nbsp; The join includes the shape field and attributes from the point feature class, plus some attributes from the nearest line, plus the distance to the nearest line.&amp;nbsp; I wrote a simple but inefficient view that works for a small test dataset, but performance quickly degrades.&amp;nbsp; 6 seconds for 2000 points against 20 lines.&amp;nbsp; Looking at the execution plan, the spatial index is not being used at all. I manually created the spatial index in ArcCatalog for both the points and the lines with coordinates that bound all of the data with Medium grid granularity at all levels.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Here is the code, looking for tips on how to engage the spatial index or better, more scalable logic in general:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;--POINTS is the point feature class
--LINES is the line feature class
select
&amp;nbsp; p.OBJECTID,
&amp;nbsp; p.SHAPE,
&amp;nbsp; p.PointAttribute,
&amp;nbsp; closestline.LineAttribute,
&amp;nbsp; p.shape.STDistance(closestline.shape)
from 
&amp;nbsp; POINTS as p 
&amp;nbsp; cross apply (
&amp;nbsp; select top 1 l.LineAttribute, l.Shape
&amp;nbsp; from LINES as l
&amp;nbsp; order by p.SHAPE.STDistance(l.SHAPE)
&amp;nbsp; ) as closestline
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 13:37:04 GMT</pubDate>
      <guid>https://community.esri.com/t5/data-management-questions/spatial-join-to-get-attributes-of-nearest-neighbor/m-p/280325#M16090</guid>
      <dc:creator>TedChapin</dc:creator>
      <dc:date>2021-12-11T13:37:04Z</dc:date>
    </item>
    <item>
      <title>Re: Spatial join to get attributes of nearest neighbor</title>
      <link>https://community.esri.com/t5/data-management-questions/spatial-join-to-get-attributes-of-nearest-neighbor/m-p/280326#M16091</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You can force indexes through &lt;A href="https://msdn.microsoft.com/en-us/library/ms181714(v=sql.110).aspx"&gt;Query Hints (Transact - SQL)&lt;/A&gt;.&amp;nbsp; That said, forcing an index may not improve performance, sometimes it makes performance worse.&amp;nbsp; Since it seems you are in a development/testing phase and not writing production code yet, it might not hurt to experiment with forcing the different indexes to see what happens.&amp;nbsp; If forcing an index does help, especially dramatically, then the question becomes why isn't the query optimizer choosing to use either index.&amp;nbsp; &lt;EM&gt;By the way, I believe it is still true that SQL Server can only use one spatial index in a single query, but that may have changed&lt;/EM&gt;.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If you haven't already, you may want to review &lt;A href="https://msdn.microsoft.com/en-us/library/bb895265(v=sql.110).aspx"&gt;SQL Server 2012 - Spatial Indexes Overview&lt;/A&gt;.&amp;nbsp; The more you understand how MS has implemented spatial indexes, the less of a black box it will all seem, which makes for better SQL troubleshooting.&amp;nbsp; Also, there are some very specific syntax rules for maximizing a nearest neighbor type of query, which you are trying to do in some form.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Beyond reading MS's overview, there are some good blogs and MSDN discussions on tuning SQL Server spatial indexes.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Taking a quick glance at your specific code, you are missing some of the requirements for a spatial index to be used.&amp;nbsp; As the overview documentation I linked to above states:&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;&lt;STRONG&gt;Geometry Methods Supported by Spatial Indexes&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;Spatial indexes support the following set-oriented geometry methods under certain conditions: &lt;STRONG&gt;STContains&lt;/STRONG&gt;(), &lt;STRONG&gt;STDistance&lt;/STRONG&gt;(), &lt;STRONG&gt;STEquals&lt;/STRONG&gt;(), &lt;STRONG&gt;STIntersects&lt;/STRONG&gt;(), &lt;STRONG&gt;STOverlaps&lt;/STRONG&gt;(), &lt;STRONG&gt;STTouches&lt;/STRONG&gt;(), and &lt;STRONG&gt;STWithin&lt;/STRONG&gt;(). To be supported by a spatial index, these methods must be used within the WHERE or JOIN ON clause of a query, and they must occur within a predicate of the following general form:&lt;/P&gt;&lt;P&gt;....&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/BLOCKQUOTE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 30 Jan 2015 17:30:58 GMT</pubDate>
      <guid>https://community.esri.com/t5/data-management-questions/spatial-join-to-get-attributes-of-nearest-neighbor/m-p/280326#M16091</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2015-01-30T17:30:58Z</dc:date>
    </item>
    <item>
      <title>Re: Spatial join to get attributes of nearest neighbor</title>
      <link>https://community.esri.com/t5/data-management-questions/spatial-join-to-get-attributes-of-nearest-neighbor/m-p/280327#M16092</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;&lt;A href="https://community.esri.com/migration-blogpost/54807"&gt;Witch Magic, Snake Oil Medicine, and Spatial Index Tuning&lt;/A&gt; might help you here. &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 07 Jul 2015 18:15:08 GMT</pubDate>
      <guid>https://community.esri.com/t5/data-management-questions/spatial-join-to-get-attributes-of-nearest-neighbor/m-p/280327#M16092</guid>
      <dc:creator>ThomasColson</dc:creator>
      <dc:date>2015-07-07T18:15:08Z</dc:date>
    </item>
  </channel>
</rss>

