试着看看如何提高这个Sql查询的性能

时间:2011-04-27 04:43:04

标签: performance sql-server-2008

我有一个SQL查询,试图查找某些县的所有社区。

当我使用SQL Sentry Plan Explorer可视化查询(IMO,比MS SSMS提供的工具稍好一点)时,它突出显示了一个非常慢的表现部分: -

完整计划

enter image description here

放大....

enter image description here

详细

enter image description here

SQL脚本:

 -- Update which Neighbourhoods are in these Counties.
INSERT INTO @NeighbourhoodCounties (NeighbourhoodId, CountyId)
 SELECT SubQuery.NeighbourhoodId, SubQuery.CountyId
 FROM (
  SELECT e.LocationId AS NeighbourhoodId, b.LocationId AS CountyId, 
      c.OriginalBoundary.STArea() AS CountyArea,
      c.OriginalBoundary.STIntersection(d.OriginalBoundary).STArea() AS IntersectionArea
  FROM @CountyIds a
   INNER JOIN [dbo].[Counties] b ON a.Id = b.LocationId
   INNER JOIN [dbo].[GeographyBoundaries] c ON b.LocationId = c.LocationId
   INNER JOIN [dbo].[GeographyBoundaries] d ON c.OriginalBoundary.STIntersects(d.OriginalBoundary) = 1
   INNER JOIN [dbo].[Neighbourhoods] e ON d.LocationId = e.LocationId
   ) SubQuery
    WHERE (SubQuery.IntersectionArea / SubQuery.CountyArea) * 100 > 5 -- a Neighbourhood has to be 5% or more to be considered 'Inside'

任何人都可以帮助解释此查询吗?所有这些数字意味着什么?如何使用这些数字来帮助诊断和改进我的查询?

I tried to make an indexed view on the spatial table但是失败了。

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

这是正常的。你在任何地方都没有厚条,主要是寻求。

但是,我确实看到你在JOIN

中的列上有一个函数
ON c.OriginalBoundary.STIntersects(d.OriginalBoundary) = 1

这无济于事。计算列也无济于事

您还可以在WHERE = non-sargable

中对列进行计算
(SubQuery.IntersectionArea / SubQuery.CountyArea) * 100

从表面上看,64.5%的搜索可能是这些JOIN和优化者在他们周围工作的结果