查询计划显示,当实际不涉及行时,插入成本为 54%
在我的一个查询中,有一个数据插入
到临时表中。查看查询计划,它显示实际插入到临时表中的时间为 54%(仅将数据插入到临时表中)。但是,没有行被插入到临时表中。
当没有插入行时,为什么计划显示非零值?
In one of my queries there's an insert
of data into a temp table. Looking at the query plan, it shows the the actual insert into temp table took 54% (just inserting data into temp table). However, no rows are being inserted into the temp table.
Why does the plan show a non zero value when no rows are being inserted?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
即使在实际查询计划中,显示的子树成本也是基于估计以及基于成本的优化器使用的各种启发式方法和幻数。它们可能严重错误,应该大量使用盐。
重现的示例
实际插入为零行,但估计为 1 行,这就是子树成本的来源。
编辑:我刚刚尝试了以下
即使它获得了正确的估计行数仍然为插入分配一个小的非零成本。我猜想它使用的启发式总是分配固定成本的一些小元素。
Even in the actual query plan the subtree costs shown are based on estimates as well as various heuristics and magic numbers used by the cost based optimiser. They can be woefully wrong and should be taken with a big pinch of salt.
Example to reproduce
The actual insert was zero rows but the estimate was 1 row which is where the subtree cost comes from.
Edit: I just tried the following
Even when it gets the estimated number of rows right it still assigns a small non zero cost to the insert. I guess the heuristic it uses always assigns some small element of fixed cost.
看看这个
不是
sargable
,所以它会导致扫描,但如果没有找到行,则插入不会发生......扫描仍然会发生,但如果你这样做了,那么成本应该会急剧下降,因为现在可以使用索引
BTW 我会使用 STATISTICS TIME 和 STATISTICS IO 来衡量性能,这两个是更好的指标..当你看到 3读取次数与 10000 次读取次数相比,您知道发生了什么。45% 准确地告诉您整个过程何时可以运行 3 分钟或 3 秒
take a look at this
that is not
sargable
so it will cause a scan, but if no rows are found the insert doesn't happen...the scan still happensbut if you did this then the cost should drop dramatically because now the index can be used
BTW I would use
STATISTICS TIME
andSTATISTICS IO
instead to measure performance, those two are much better indicators..when you see 3 reads vs 10000 reads you know what is happening..what does 45% tell you exactly when the whole process could run 3 minutes or 3 seconds54 的成本并不意味着需要涉及行。也许在 INSERT into 临时表的 WHERE 子句中存在索引扫描或其他查找操作,或者可能存在一些非最佳查找?
The cost of 54 doesn't mean that rows need be involved. Perhaps there was an index scan or other seek operation or perhaps some non-optimal lookup in the WHERE clause of that INSERT into temp table?