SQL Server 2005中的SQL查询优化(CTE+范围函数)
我想知道这样的查询是否可以优化。 我已经极大地简化了它,你可以看到它的核心。
with Rec (Id,Name,ParentId)
as
(
select Id,Name,ParentId from Departments where ParentId is null
union all
select d.Id, d.Name, d.ParentId from Departments d join Rec r on
(d.ParentId=r.Id)
)
select q.* from (
select ROW_NUMBER() OVER (ORDER BY r.Id DESC) AS [ROW_NUMBER], r.* from Rec r
) as q
where q.[ROW_NUMBER] between 100 and 200
它的作用是分层查询下属部门,然后对其进行范围分析。
我最终得到了一个巨大的执行计划,并想知道是否可以以不同的方式完成它。
谢谢。
I'm wondering whether such query could be potentially optimized.
I've hugely simplified it, and you see the core of it.
with Rec (Id,Name,ParentId)
as
(
select Id,Name,ParentId from Departments where ParentId is null
union all
select d.Id, d.Name, d.ParentId from Departments d join Rec r on
(d.ParentId=r.Id)
)
select q.* from (
select ROW_NUMBER() OVER (ORDER BY r.Id DESC) AS [ROW_NUMBER], r.* from Rec r
) as q
where q.[ROW_NUMBER] between 100 and 200
What it does is hierarchically query the desendent departments and then do a ranging upon it.
I'm ending up with a huge execution plan and wondering if it can be done in a different manner.
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想考虑到以下假设,情况可能会更好一些:
可以做什么:为了减少 io 子系统的负载,我们可以首先组成一个 Id 列表,对它们进行分页(即按 RowNumber 进行过滤),然后才包含所有其他列。 这将有效地导致使用 ParentId 索引,考虑到上述两个假设,这应该会快得多。
所以这是我“亲自”提出的建议:
I quess it could be a bit better given the following assumptions:
What can be done: in order to reduce load on io subsystem we can first compose a list of Id's, page them (i.e. filter by RowNumber) and only after that include all other columns. This will effectively result in working with index by ParentId which should be a lot faster given the above two assumptions.
So here's my proposition 'in person' so to say: