简体   繁体   English

查询优化器无法推送谓词过去汇总? 提示也不起作用

[英]Query Optimizer can't push predicate past rollup? HINTs doesn't work also

This is the schema : 这是架构:

在此输入图像描述

And this is the sql that as I understand is too complex for SQL Optimizer: 这是我理解的SQL对于SQL Optimizer来说太复杂了:

SELECT * FROM 
(
    select pp.Id as PaymentPartId,  b.Id as BudgetId, grouping(bp.ID) as g1 , sum(pp.Amount) PaymentsSum, sum(bp.Amount) BudgetSum
    from  Projects pr 
            inner join Payments p      ON pr.Id = p.ProjectID
            inner join PaymentParts pp ON p.Id = pp.PaymentId
            inner join Budgets b       ON pr.Id = b.ProjectID
            inner join Budgetparts bp  ON b.Id = bp.BudgetId
    group by pp.Id, b.Id, rollup(bp.ID)
)  x
WHERE   x.PaymentPartId = 777 

SQLFIDDLE: http://sqlfiddle.com/#!6/aa74e/11 (with autogenerated data) SQLFIDDLE: http ://sqlfiddle.com/#!6/aa74e/11(带有自动生成的数据)

What I expect: execution plan should contain index seek on x.PaymentPartId . 我的期望: 执行计划应包含x.PaymentPartId上的索引搜索 Why? 为什么? Because this query is equivalent to: 因为此查询等效于:

select pp.Id as PaymentPartId,  b.Id as BudgetId,  grouping(bp.ID) as g1, sum(pp.Amount) PaymentsSum, sum(bp.Amount)  BudgetSum
from  Projects pr 
        inner join Payments p      ON pr.Id = p.ProjectID
        inner join PaymentParts pp ON p.Id = pp.PaymentId
        inner join Budgets b       ON pr.Id = b.ProjectID
        inner join Budgetparts bp  ON b.Id = bp.BudgetId
WHERE   pp.Id = 777
group by pp.Id, b.Id, rollup(bp.ID)

...and the last query uses index seek. ...并且最后一个查询使用索引搜索。

But SQL Optimizer not only refuse to use the index but ignore all hints (I propose you to expirement wiht sqlfiddle - it is really interesting). 但是SQL Optimizer不仅拒绝使用索引而忽略了所有提示(我建议你使用sqlfiddle来实现它 - 这真的很有趣)。

So the question is: am I right that it is impossible to force SQL Server Optimizer to use index seek there? 所以问题是: 我是对的,不可能强制SQL Server Optimizer在那里使用索引查找吗? It seems like rollup is something that split sql optimizer "optimization frame" to two parts and it makes it impossible to optimize WHOLE query. 似乎汇总是将sql优化器“优化框架”分成两部分的东西,它使得无法优化整个查询。

PS For those who votes for closing this "non-programming question": try to put optimizer hints (sqlfiddle is ready to test your programming skills!). PS对于那些谁票关闭这个“非编程的问题”:试图把优化提示(sqlfiddle准备测试你的编程技能!)。

Why hints doesn't work? 为什么提示不起作用? - Roman Pokrovskij - 罗马波克罗夫斯基

It is in the documentation: http://technet.microsoft.com/en-us/library/ms181714.aspx 它在文档中: http//technet.microsoft.com/en-us/library/ms181714.aspx

Query hints can be specified only in the top-level query, not in subqueries 查询提示只能在顶级查询中指定,而不能在子查询中指定

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM