sql - SQL Server 将 Self-Join 更改为具有范围的 CTE
问题描述
我有一个很好的基于范围左连接的自连接查询。在上一个时期,查询的性能开始下降,我正在尝试通过将其迁移到 CTE 或任何其他解决方案来优化此查询。
select distinct Main.Id, Main.Name, Main.PolicyRef, Main.ExtCategoryId, Main.NodeLevel, Main.Description
from tbl1 as Main left join
tbl1 as Cat
on (Cat.LeftN between Main.LeftN and Main.RightN and Cat.RightN between Main.LeftN and Main.RightN
OR
Cat.LeftN between Main.RightN and Main.LeftN and Cat.RightN between Main.RightN and Main.LeftN
) and cat.Id=main.Id
where Cat.Id in (select CategoryRef from tbl2 where AccountRef=@AccountRef and ItemRef=@MId)
set statistics io on
表“工作台”。扫描计数 0,逻辑读取 0,物理读取 0,预读读取 0,lob 逻辑读取 0,lob 物理读取 0,lob 预读读取 0。
表'tbl1'。扫描计数 1,逻辑读取 19903,物理读取 0,预读读取 0,lob 逻辑读取 0,lob 物理读取 0,lob 预读读取 0。
表'tbl2'。扫描计数 1,逻辑读取 123,物理读取 0,预读读取 0,lob 逻辑读取 0,lob 物理读取 0,lob 预读读取 0。
解决方案
推荐阅读
- django - Django Forms:非模型字段不呈现具有“查看”权限的值
- git - 我如何克服致命的存储库未找到错误
- python - 有没有办法在列表中的特定项目之后打印一个移动到新行的列表?
- r - Sapply with LM 返回一个 Stargazer 无法使用的 Call 函数。我该如何改变呢?
- javascript - Hooks 中的 for 循环和 if 语句?
- pandas - 在熊猫中减去具有不同时间戳的数据帧
- excel - 寻找 perl 代码以关闭 Excel 文件中的数据过滤器
- python - 你可以用熊猫对特定的字符间隔进行 value_counts 吗?
- javascript - 在 socket.io 中调用 socket.connect() 时如何传递附加数据?
- math - 使用 Julia 语言在图像中应用 SVM