SQL:实际行数差异

发布于 2024-11-30 20:05:48 字数 372 浏览 0 评论 0原文

可能的重复:
SQL Server 如何计算出估计的行数行?

我只是在查看SQL Server图形执行计划,遇到以下两个信息:

  1. 实际行数-2385
  2. 估计行数-180

我想知道为什么会出现这种差异。你能解释一下它们之间有什么区别吗?如果您能用一些例子进行解释,将会很有帮助。

谢谢。

Possible Duplicate:
How does SQL server work out the estimated number of rows?

I was just looking through SQL Server Graphical Execution plan and I encountered the following two information:

  1. actual number of rows-2385
  2. estimated number of rows-180

I was wondering why this difference was coming. Can you explain me what is the difference between them. It will be helpful if you can explain with some example.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

﹂绝世的画 2024-12-07 20:05:48

行估计基于统计数据。不准确的估计可能来自多种因素:

  • 数据或统计数据中的基数较低 统计
  • 数据过时或不完整
  • 执行计划效率
  • 低下 并发问题(从创建执行计划到执行时间的数据变化)

一般来说,不必担心直到出现问题为止。

根据我的经验,它通常基于统计数据中的基数。

如果您基于两个字段进行选择,这两个字段都有索引,则行估计将基于相应索引中值的可能性乘以总行数的乘积。

Row estimates are based on statistics. Inaccurate estimates can come from a number of factors:

  • Low cardinality in your data or stats
  • Out of date or incomplete stats
  • Inefficient execution plan
  • Concurrency issues (changes in data from creation of execution plan to execution time)

As a rule, don't worry about it until you have issues.

I my experience, it's most often based on the cardinality in your stats.

If you are selecting based on two fields, both of which have indexes, the row estimate will be based on a product of the likelihood of the values in the respective indexes, times the total number of rows.

伤感在游骋 2024-12-07 20:05:48

我对 SQL Server 不太了解,所以这是一个有根据的猜测:表中保存的统计数据与表中的实际数据之间存在差异。

数据库保存有关表的统计数据,优化器可以使用这些数据为要执行的 SQL 找到最佳(最便宜)的查询计划。这些统计数据包括例如总行数和跨列的数据分布(例如字段“性别”包含 60% 的值“m”和 40% 的值“f”)。您可能在这里看到的是统计信息已过时,因此优化器完成的计算不准确:优化器假定结果包含 180 行,但实际上(执行查询时)它返回 2385。尝试更新统计信息(不知道 SQL Server 中的确切语法)并看看这是否会改变数字。

I don't know SQL Server too much, so this is an educated guess: There is a discrepancy between the statistical data kept about the table and the actual data in the table.

Databases keep statistical data about tables, which can be used by the optimizer to find an optimal (least expensive) query plan for the SQL that you want to execute. Those statistics include for instance the total number of rows and the distribution of data across columns (for instance that the field 'gender' contains 60% values 'm' and 40% values 'f'). What your likely seeing here, is that the statistics are outdated and the calculations done by the optimizer hence inacurate: The optimizer assumed the result to contain 180 rows, in reality though (when executing the query) it returns 2385. Try updating the statistics (don't know the exact syntax in SQL server) and see if that changes the numbers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文