尝试看看如何提高此 Sql 查询的性能

发布于 2024-11-03 09:46:26 字数 1507 浏览 1 评论 0原文

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

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

Full Plan

在此处输入图像描述

放大 ....

在此处输入图像描述

详细信息

< img src="https://i.sstatic.net/4gvNz.png" alt="在此处输入图像描述">

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'

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

我尝试在空间上创建索引视图table 但这惨败了。

有人可以帮忙吗?

I've got a SQL query which is trying to find all the Neighbourhoods in some Counties.

When I use SQL Sentry Plan Explorer to visualize the query (IMO, a bit better than the tools provided with MS SSMS) it highlights a really slow performing part :-

Full Plan

enter image description here

Zoomed in ....

enter image description here

Details

enter image description here

SQL script:

 -- 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'

Can anyone help interpret this query? What do all these numbers mean? How can I use these numbers to help diagnose and improve my query?

I tried to make an indexed view on the spatial table but that failed miserably.

Can anyone help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

坚持沉默 2024-11-10 09:46:26

这本来是正常的。在大多数搜索的地方都没有粗条。

但是,我确实看到您在 JOIN 中的列上有一个函数,

ON c.OriginalBoundary.STIntersects(d.OriginalBoundary) = 1

这不会有帮助。计算列也无济于事

而且您还在 WHERE = 不可控制的列中进行了计算

(SubQuery.IntersectionArea / SubQuery.CountyArea) * 100

从表面上看,64.5% 的查找可能是这些 JOIN 和围绕它们工作的优化器的结果

This would be normal. You have no thick bars anywhere with mostly seeks.

However, I do see that you have a function on a column in a JOIN

ON c.OriginalBoundary.STIntersects(d.OriginalBoundary) = 1

This won't help. And a computed column won't help either

And you also have a calculation on columns too in a WHERE = non-sargable

(SubQuery.IntersectionArea / SubQuery.CountyArea) * 100

On the face of it, the 64.5% seek is probably a consequence of these JOINs and the optimiser working around them

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文