What are SQL hints?

I am a supporter of ORM solutions, and from time to time I give a seminar about Hibernate.

Speaking about the SQL generated by the frame, people usually start talking about how they need to use “hints”, and this may not be possible within the framework of ORM.

Usually something like: “We tried Hibernate. At first it looked promising, but when we released it into our very complex manufacturing base, it broke because we couldn’t apply the prompts!”

But when he was asked to give a concrete example, the memory of these people is suddenly not so clear ...

I usually feel intimidated because all the “hints” - the topic sounds like voodoo to me ... So can anyone enlighten me? What is meant by SQL prompts or DB-Hints?

The only thing I know is somehow a “hint” - this is SELECT ... FOR UPDATE. But it is supported by the Hibernate-API ...

+4
source share
5 answers

The SQL statement, which is especially complex, can actually be executed by the database engine in any number of ways (which table in the first connection to read, which index to use based on many different parameters, etc.)

An experienced dba can use the prompts to prompt the DB engine to select a specific method when it generates its execution plan. Usually you only had to do this after thorough testing and analysis of specific queries (since the database mechanisms are usually pretty good at determining the optimal execution plan).

Some discussions and syntax specific to MSSQL:
http://msdn.microsoft.com/en-us/library/ms181714.aspx

Edit: some additional examples in
http://geeks.netindonesia.net/blogs/kasim.wirama/archive/2007/12/31/sql-server-2005-query-hints.aspx

+9
source

Hint tips are used to guide the query optimizer when, by default, it does not create reasonable query plans. Firstly, a small background in query optimizers:

Database programming is different from almost all other software because it has a mechanical component. Disk and rotation latency (expecting a particular sector to fall under the disk head) are very expensive compared to a processor. Different query resolution strategies will result in different amounts of I / O, often radically different amounts. Proper or incorrect use can significantly affect query performance. For an overview of query optimization, see this article.

SQL is declarative — you define the logic of the query and let the DBMS figure out how to solve it. The modern cost-based query optimizer (some systems, such as Oracle, also have a proactive query optimizer, maintained for backward compatibility) will conduct a series of query transformations. They support semantic equivalence, but differ in the order and choice of operations. Based on the statistics collected from the tables (sizes, histograms of key distribution), the optimizer calculates an estimate of the amount of work needed for each query plan. He chooses the most effective plan.

Cost-based optimization is heuristic and depends on accurate statistics. As the complexity of queries increases, heuristics can create incorrect plans that can potentially be extremely inefficient.

In this situation, you can use hints to request certain strategies in terms of the request, for example, the type of connection. For example, in a query that usually returns very small result sets, you can force nested loops to be inserted. You might also want to force the table join order.

O / R mappers (or any tool that generates SQL) generates its own query, which usually does not have a hint of information. In case this request does not work efficiently, you have limited options, some of which are:

  • Learn indexing tables. Perhaps you can add an index. Some systems (such as recent versions of Oracle) allow you to index joins in multiple tables.

  • Some database management systems (again, Oracle comes to mind) allow you to manually associate a query plan with a specific query string. Request plans are cached by the hash value of the request. If the queries are parameterized, the base query string is constant and will be resolved to the same hash value.

  • As a last resort, you can change the database schema, but this is only possible if you are running the application.

If you control SQL, you can prompt queries. In practice, this is quite rare, in fact, you need to do this. A more general failure mode on O / R cards with complex database schemas is that they can make it difficult to express complex query predicates or perform complex operations on large data arrays.

I tend to advocate the use of an O / R mapper for 98% of the work for which it is suitable and thrown into stored procedures, where they are a suitable solution. If you really need to hint at a request, this might be the appropriate strategy. If there is something unusual in your application (for example, some DSS), you only need to get away from the O / R mapper in a minority of situations. You can also find (again, for example, DSS tools that work with data together) that O / R matching is not a good strategy for an application.

+6
source

While HINTS does as the other answers describe, you should use them only in rare, investigated circumstances. 9 times out of 10 A TIP will lead to bad queries. If you really do not know what you are doing, do not use them.

+4
source

All modern RDBMS-es have a kind of query optimizer that calculates the best query plan, which is the sequence of read / write operations necessary to execute the SQL query.

Sometimes plans can be suboptimal, so RDBMS developers included “hints” in SQL. Hints are instructions that you can embed in your SQL that affect the query optimizer. With hints, you can specify a query optimizer, for example. what indexes should he use, in what order should the data be read from tables, ...

So, with the tips, you can solve some bottlenecks that the query optimizer cannot solve on its own.

For example, here is a list of Oracle prompts .

+3
source

There is no such thing as "optimized SQL code" because SQL code is never executed.

The SQL code is converted to an Optimizer execution plan. The optimizer will use the information that it should select (among other things).

  • the order in which the tables participate
  • join method for each table involved (nested / merge / hash)
  • how to access tabular data (direct access to the table / index when searching through bookmarks / direct access to the index) (search / search)
  • parallelism should be used, and when to end parallelism (collect threads)

Query hints allow the programmer to overload (in most cases) or offer politely (in other cases) optimizer options.

Query hints can allow you to turn off parallelism, make all connections be implemented as a nested loop, force one index to be used over another ... as a few examples.

Since the optimizer is really good if someone overrides the optimizer, a suboptimal plan is usually requested. Hint tips are best used when the optimizer does not have the required information to make a good choice.

In one place, I use hints for table variables. It is assumed that the variable tables have 0 rows by the Optimizer, so the Optimizer always joins the variable tables using a nested loop (the best implementation of joining for a small number of rows). If I have a large table variable - it is already arranged favorably for merge joins, I can specify that the merge join be used using the query hint.

+2
source

All Articles