SQL Server 2008 R2 和索引视图中的执行计划
我创建了一个由三列组成的索引视图 (MyView):
Table1_ID (int not null)
Object_CreationDate (datetime, null)
Objec_Count(bigint null)
我在两列上创建了聚集唯一索引 IX_1:Table1_ID
并且Object_CreationDate
我想运行两个查询:
1.
Select * from [dbo].MyView
where Table1_ID = 10
2.
Select * from [dbo].MyView
where Table1_ID = 10
AND Object_CreationDate <= GETDATE()
第一个查询运行速度很快(即使使用DBCC DROPCLEANBUFFERS()),并通过使用 MyView 和 IX_1 使用简单的执行计划
第二个查询运行得不太快,因为它使用“旧”执行计划(通过三个表中的多个索引和嵌套循环进行查找)
我误解了这种情况。对于我来说,很自然地使用 IX_1 和 MyView 进行第二次查询。
此外,我等待第二个查询的运行速度与第一个查询的速度相同甚至更快,因为它使用聚集索引中的两列in where 子句。
我尝试运行第二个查询 with(index=IX_1)
并更新列的统计信息,但仍然具有相同的执行计划。
是否可以强制 sql 使用 MyView 和 IX_1 ?
I've created an indexed view (MyView) that consists of three columns:
Table1_ID (int not null)
Object_CreationDate (datetime, null)
Objec_Count(bigint null)
I have created clustered unique index IX_1 on two columns: Table1_ID
And Object_CreationDate
I want to run two queries:
1.
Select * from [dbo].MyView
where Table1_ID = 10
2.
Select * from [dbo].MyView
where Table1_ID = 10
AND Object_CreationDate <= GETDATE()
1-st query runs fast (even with DBCC DROPCLEANBUFFERS()) and use simple execution plan via using MyView and IX_1
2-nd query runs not so fast because it uses "old" execution plan (seeking by several indexes in three tables and nested looping)
I misunderstand this situation. As for me, it is natural use IX_1 And MyView for 2-nd query.
Moreover, I wait that 2-nd query runs the same speed or even faster then 1-st, because it uses two columns in where clause that are in clustered index.
I tried run 2-nd query with(index=IX_1)
and update statistics for columns, but still have the same execution plan.
Is it possible to force sql use MyView AND IX_1 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
除非您使用的是 Enterprise/Developer 版本,否则需要包含 WITH NOEXPAND 提示< /a>
来自设计索引视图:
(而开发版基本上就是企业版,有不同的许可)
Unless you're using Enterprise/Developer edition, you need to include the WITH NOEXPAND hint
From Designing Indexed Views:
(And Developer Edition is basically Enterprise Edition, with different licensing)