Sql Server 中的提示

发布于 2024-07-07 17:14:04 字数 89 浏览 5 评论 0原文

每个sql语句都需要提示吗? 我们有一个 dba,他对此很反感,并要求我们在存储过程中的每个 select 和 update 语句上添加提示。 这真的有必要吗?

Are hints really necessary for every sql statement? We have a dba who is anal about it and asks us to put hints on every select and update statements in our stored procs. Is this really necessary?

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

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

发布评论

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

评论(5

暗藏城府 2024-07-14 17:14:04

通常不会。 把它们放在所有东西上听起来有点矫枉过正。

文档

因为 SQL Server 查询优化器通常会选择查询的最佳执行计划,我们建议经验丰富的开发人员和数据库管理员仅将 join_hint、query_hint 和 table_hint 作为最后的手段使用

Not usually. Putting them on everything sounds like overkill.

The documentation says

Because the SQL Server query optimizer typically selects the best execution plan for a query, we recommend that join_hint, query_hint, and table_hint be used only as a last resort by experienced developers and database administrators

缺⑴份安定 2024-07-14 17:14:04

您的 DBA 错了

来自 MS

因为SQL Server查询优化器
通常选择最佳执行
计划查询,我们建议
连接提示查询提示
表格提示仅作为最后使用
度假村由经验丰富的开发商和
数据库管理员。

Your DBA is wrong.

From MS:

Because the SQL Server query optimizer
typically selects the best execution
plan for a query, we recommend that
join hint, query hint, and
table hint be used only as a last
resort by experienced developers and
database administrators.

泛滥成性 2024-07-14 17:14:04

提示只是提示。 它们帮助优化器尽可能地完成最好的工作。 但与任何优化一样,您应该关注实际存在问题的语句。

Hints are merely hints. They help the optimizer to do the best job possible. But like any optimization, you should focus on the statements that are actually problems.

我是男神闪亮亮 2024-07-14 17:14:04

取决于 - 查询优化器做出了很好的意图选择。 您的 DBA 需要什么提示? @Ned 有点不对劲 - 提示明确告诉优化器不要找出路径 - 而是使用您的优化。

立法规定您应该始终使用或从不使用提示,这在某种程度上忽视了提示要解决的问题。 在某些情况下,提示至关重要:

  • NOLOCK 显式地从事务中查询的域查找表中删除读锁。
  • 将查询计划钉在特定索引上,因为在大量更新期间统计信息“漂移”(在本例中,计划恢复为在 10m 行表上进行表扫描,而不是使用聚集索引)

永远不必使用连接提示。

Depends - the query optimizer makes pretty good choices of intent. What hints are your DBA's demanding? @Ned is a little amiss - a hint explicitly tells the optimizer not to figure-out a path - but use your optimization instead.

To legislate that you should always or never use hints is somewhat ignorant of the issues that hints are there to solve. Some occassions where hints have been critical:

  • NOLOCK to explicitly remove read-locks from domain lookup tables queried within a transaction.
  • nailing a query plan to a specific index because of statistics "drift" during heavy updates (in this instance the plan reverted to table-scan on a 10m row table than use the clustered index)

Never had to use join hints.

抚笙 2024-07-14 17:14:04

通常这只是倒退。 但是,根据您的情况,这可能是可取的。

例如,我们有一个数据库(实际上是一台服务器上的一组数据库),其中的数据都是大型机系统的夜间快照转储,用于报告和其他目的。 除了每晚重新创建数据库的批处理过程之外,没有任何操作对该系统进行任何写入操作。 在这种情况下,默认的锁定方案并不合适,而且我们的团队与管理所有服务器的 IT 团队之间的政治斗争阻止我们对其进行更改。 所以:几乎所有对这些数据库的查询都有 with (nolock) 提示。

我想在其他情况下,您可能拥有没有写入的报告数据库,或者相反:很少读取的归档或日志记录数据库。 关键是,有时可能会设置一个专用数据库,但默认锁定方案不适合并且您无法更改它。 然后你将需要大量的皮纳塔......我的意思是提示。

但这是证明规则的例外。 一般来说,当涉及到诸如锁定之类的事情时,数据库优化器比您更聪明。

Normally this is just backwards. However, depending on your situation it might be desirable.

For example, we have one database (a set of databases on one server, actually) where the data is all a nightly snapshot dump of a mainframe system, used for reporting and other purposes. Aside from the batch process that recreates the databases each night, nothing does any writing to this system. In that context, the default locking scheme is not appropriate and politics between our group and the IT group that manages all our servers prevents us from changing it. So: pretty much all queries to those dbs have the with (nolock) hints.

I imagine there are other situations where you may have reporting databases with no writes, or perhaps the reverse: an archiving or logging database that is rarely read. The point is that occasionally a specialized database might be set up where the default locking scheme doesn't fit and you are not able to change it. Then you will need a plethora of pinatas... I mean hints.

But that's the exception that proves the rule. In general, the database optimizer is smarter than you are when it comes to things like locking.

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