This topic describes the guidelines for using hints in OceanBase Database.
Hints should be used sparingly, only after the optimizer has evaluated the query plan without hints and after statistics have been collected for the relevant tables. Changes in database conditions or improvements in query performance in later versions may significantly impact the performance of code that includes hints.
Introduction to HINT
The cost-based optimizer, similar to Oracle's HINT.
If the server does not recognize the HINT in the SQL statement, it will be ignored without any error.
HINT only affects the logic of the optimizer generating plans, not the semantics of the SQL statement itself.
HINT is a mechanism that allows the optimizer to generate a specific plan. Generally, the optimizer selects the best execution plan for the user's query without the need for HINTs. However, in some scenarios, the optimizer's generated plan may not meet the user's requirements, necessitating the use of HINTs to explicitly specify a particular execution plan.
HINT syntax
{CREATE|DELETE|INSERT|SELECT|UPDATE|REPLACE} /*+ [HINT_text][hin_text]... */
A statement block can contain only one comment with HINT, and this comment must follow the SELECT, UPDATE, INSERT, MERGE, or DELETE keyword.
From a syntactic perspective, HINT is a special SQL comment. The difference is that a '+' is added after the left delimiter of the comment (
'/*'symbol). Since it is a comment, if the server cannot recognize the HINT in the SQL statement, the optimizer will ignore the user's HINT and use the default plan generation logic. Additionally, it is important to note that HINT only affects the logic of the optimizer generating plans, not the semantics of the SQL statement.Some rules to keep in mind when defining HINTs:
The '+' makes the database interpret the comment as a HINT list. The '+' must immediately follow the left delimiter of the comment, with no space allowed.
The space between the '+' and the HINT text is optional. If the comment contains multiple HINTs, they must be separated by at least one space or a comma.
HINTs with spelling or syntax errors will be ignored. However, the database will consider other correctly specified HINTs within the same comment.
HINTs that do not follow the DELETE, INSERT, MERGE, SELECT, or UPDATE keywords are invalid.
In addition to the HINT concept, OceanBase Database differs from Oracle in the following aspects:
- Oracle leading HINT: When an invalid table is encountered, whether the HINT takes effect is calculated, leading to unpredictable behavior, where it may be effective in some cases and completely ineffective in others.
Examples of HINT usage
Without parameters, such as
/*+ KAKA */.With parameters, such as
/*+ HAHA(param) */.Multiple HINTs can be written in the same comment, separated by commas or at least one space, such as
/*+ KAKA, HAHA(param)*/.For SELECT statements, the HINT must be immediately after the SELECT keyword, before any other keywords. For example:
SELECT /*+ KAKA */.For UPDATE and DELETE statements, the HINT must be immediately after the UPDATE or DELETE keyword. For example:
UPDATE /*+ KAKA */,DELETE /*+ KAKA */.
Common HINT types
Hint Name |
Hint Parameters |
Hint Semantics |
|---|---|---|
| NO_REWRITE | Do not rewrite the SQL statement. | |
| READ_CONSISTENCY | weak|strong|frozen | weak: weak consistency read; strong: strong consistency read; frozen: read data from the last frozen point. |
| INDEX_HINT | [ qb_name ] table_nameindex_name | Specify the index to use when querying a table. |
| QUERY_TIMEOUT | int64 | Specify the statement execution timeout in microseconds (us). |
| LEADING | [ qb_name ] table_name [, table_name ] | Specify the order of tables in a multi-table join. |
| ORDERED | Specify the order of tables in a multi-table join to follow the order in which they appear in the SQL statement. | |
| FULL | [qb_name] table_name | Specify a full table scan (reads the primary key if one exists). |
| USE_MERGE | [qb_name] table_name [,table_name] | Specify the use of the MERGE algorithm for multi-table joins. |
| USE_NL | [ qb_name ] table_name [, table_name] | Specify the use of the NESTED LOOP algorithm for multi-table joins. |
| USE_BNL | [qb_name] table_name [,table_name] | Specify the use of the BLOCK NESTED LOOP algorithm for multi-table joins. |
| USE_HASH_AGGREGATION | [ qb_name] | Specify the use of the HASH AGGREGATE method for aggregate operations, such as HASH GROUP BY and HASH DISTINCT. |
| NO_USE_HASH_AGGREGATION | [ qb_name ] | Specify not to use the HASH AGGREGATE method for aggregate operations. Instead, use the MERGE GROUP BY and MERGE DISTINCT methods. |
| QB_NAME | [ qb_name] | Specify the name of the query block. |
| PARALLEL | int64 | Specify the degree of parallelism for distributed execution. |
| DISABLE_TRIGGER | trigger_name | Disable the specified trigger in DML statements. |
For more information about HINTs, see SQL optimization.
