简单的SQL更新执行速度非常慢

时间:2015-03-02 22:24:10

标签: sql sql-server indexing indexed-views

SQL Server 2008 R2。

运行一个看起来像这样的简单更新命令

UPDATE [group_mtm] SET group = foobar where user in (u1,u2,u3,...u19)

查询仅更新1行,但需要超过2秒。这是一个2属性表,用作多对多结点。表目前只有大约300条记录。主键=组和用户属性的组合键。

我已设置统计信息以检查它,并且查询执行涉及许多其他没有涉及业务的表:

Table 'foobar'. Scan count 1, logical reads 21214, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'Worktable'. Scan count 1664, logical reads 42674, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'mail_mtm'. Scan count 1, logical reads 428, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'event'. Scan count 1, logical reads 893, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'user'. Scan count 0, logical reads 91, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'addresses'. Scan count 1, logical reads 12, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'mail'. Scan count 1, logical reads 79, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'links'. Scan count 1, logical reads 18, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'group_mtm'. Scan count 20, logical reads 120, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

(20 row(s) affected)

(1 row(s) affected)

我唯一能想到的是foobar是和索引视图,也许花费在维护它上,所以我检查了执行计划;它确实发挥作用(但只有11%的成本)。

为什么需要2秒钟?

我在这看什么?

执行计划建议我在事件中添加一个索引,但事件甚至不应该涉及这里,而且只需要13%的费用。

如果由于某种原因导致其他表格被拉入其中,我觉得这应该是闪电般快速的。

有人可能会给我关于索引视图增加一般数据库开销的sage建议;但是所有其他的操作都会下降到厕所似乎是一个相当陡峭的成本,因为索引视图提供的某些查询可以更快地查看。

教我;拜托,谢谢!

XML执行计划太大而无法发布为文本而且我没有看到附加选项,所以粘贴到救援:http://pastebin.com/BgjBxLfc

1 个答案:

答案 0 :(得分:0)

这可能不是完整的答案,但扩展sql查询以进一步限定字段名称:[table]。[field]。我认为' group'是group_mtm中的一个字段,但您似乎正在尝试分配' foobar'表到它?