Question

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?

Was it helpful?

Solution

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

OTHER TIPS

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.

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.

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top