SQL hints, index hints and query cache
Using SQL hint
Section titled “Using SQL hint”The optimizer normally finds a good plan on its own. When it does not, for example because the statistics cannot capture a correlation in your data, hints tell it which join order, join method or index to prefer. Check the effect of a hint with the query plan.
A hint is a comment that starts with a plus sign, placed directly after the first keyword of a SELECT, UPDATE, DELETE or MERGE statement. All three comment styles work: /*+ hint */, --+ hint and //+ hint. Several hints are separated by spaces.
{ SELECT | UPDATE | DELETE } /*+ hint [ hint ... ] */ ...MERGE /*+ merge_hint [ merge_hint ... ] */ INTO ...
hint ::= ORDERED | USE_NL [ ( table [, table ...] ) ] | USE_IDX [ ( table [, table ...] ) ] | USE_MERGE [ ( table [, table ...] ) ] | USE_DESC_IDX | NO_DESC_IDX | INDEX_SS [ ( table [, table ...] ) ] | NO_INDEX_SS | INDEX_LS | NO_INDEX_LS | NO_COVERING_IDX | NO_MULTI_RANGE_OPT | NO_SORT_LIMIT | NO_HASH_AGGREGATE | NO_HASH_LIST_SCAN | NO_LOGGING | RECOMPILE | QUERY_CACHE | QUERY_NO_CACHE
merge_hint ::= USE_UPDATE_IDX ( index [, index ...] ) | USE_INSERT_IDX ( index [, index ...] ) | RECOMPILEUnknown hints are ignored without error, so check the plan when a hint seems to have no effect: the Query stmt part shows the hints that were recognized.
Join hints
| Hint | Effect |
|---|---|
| ORDERED | Join the tables in the order of the FROM clause; the first is the outer table. |
| USE_NL | Use nested-loop joins. |
| USE_IDX | Use index joins (nested loops that look up the inner table in an index). |
| USE_MERGE | Use sort-merge joins. Merge joins are considered only with this hint or with optimizer_enable_merge_join = yes. |
With a table list, USE_NL, USE_IDX and USE_MERGE apply only to joins with those tables: in SELECT /*+ ORDERED USE_NL(b) USE_MERGE(d) */ … FROM a JOIN b … JOIN c … JOIN d …, b is joined by nested loop and d by merge join. USE_NL and USE_MERGE together without a table list are ignored. A hint cannot always be followed: a RIGHT OUTER JOIN, for example, is converted to a left outer join internally, which can change the join order.
Index hints
| Hint | Effect |
|---|---|
| USE_DESC_IDX | Scan indexes in descending order where it helps; see Index scan in descending order. |
| NO_DESC_IDX | Do not scan indexes in descending order. |
| INDEX_SS / NO_INDEX_SS | Consider / do not consider an index skip scan. |
| INDEX_LS / NO_INDEX_LS | Consider / do not consider a loose index scan. |
| NO_COVERING_IDX | Do not use covering indexes. |
| NO_MULTI_RANGE_OPT | Do not use the multiple key ranges optimization. |
| NO_SORT_LIMIT | Do not use the SORT-LIMIT optimization. |
Other hints
- NO_HASH_AGGREGATE: aggregate by sorting instead of with an in-memory hash table. Hash aggregation reduces or avoids sorting; its memory is limited by max_agg_hash_size. Use the hint when you know that hashing will not pay off for a query. Hash aggregation is never used for aggregates on DISTINCT values (such as
AVG(DISTINCT x)), GROUP_CONCAT and MEDIAN, which need sorted input.
- NO_HASH_LIST_SCAN: scan the result of a subquery as a list instead of building and probing a hash table. Hash list scans speed up equality lookups in subquery results; their memory is limited by max_hash_list_scan_size. They are used only for
=conditions and not for OID values. Use the hint when the outer query produces very few rows. - NO_LOGGING: do not write redo log records for the heap changes of an INSERT, UPDATE or DELETE. Index changes are still logged, so after a crash the table and its indexes can disagree, and committed changes may not be recoverable. Use it only for data you can reload.
- RECOMPILE: ignore the cached plan of the statement and compile a new one. Use it when you examine plans, and for statements whose best plan depends strongly on parameter values.
- QUERY_CACHE (also SQL_CACHE): cache the result of a SELECT; see Query cache. QUERY_NO_CACHE (SQL_NO_CACHE) turns caching off for the statement.
MERGE hints
- USE_UPDATE_IDX (indexes): indexes to use for the join and update conditions of the UPDATE part of a MERGE.
- USE_INSERT_IDX (indexes): indexes to use for the join condition of the INSERT part.
- RECOMPILE: as above.
Example: join order and method
Section titled “Example: join order and method”To list the orders of one customer, the natural plan reads the customer first and looks up its orders in the foreign key index. ORDERED and USE_NL make that choice explicit:
SELECT /*+ RECOMPILE USE_NL ORDERED */ c.company_name, o.order_dateFROM customer c, orders oWHERE c.company_name = 'Jones LLC' AND c.id = o.customer_id;Query plan:
idx-join (inner join) outer: sscan class: c node[0] sargs: term[1] cost: 14 card 1 inner: iscan class: o node[1] index: fk_orders_customer_id term[0] cost: 3 card 2914 cost: 27 card 3A nested-loop join whose inner table is read through an index is shown as idx-join. Join hints can be limited to some tables: /*+ USE_NL(c, o) */.
Index hint
Section titled “Index hint”Index hints restrict or prioritize the indexes the optimizer may use for a table. There are two equivalent forms: a USING INDEX clause at the end of the statement, and USE, FORCE or IGNORE INDEX after a table in the FROM clause. The second form is rewritten into the first.
USING INDEX
Section titled “USING INDEX”SELECT ... WHERE ... [ USING INDEX index_list ]UPDATE ... WHERE ... [ USING INDEX index_list ]DELETE ... WHERE ... [ USING INDEX index_list ]
index_list ::= NONE | [ ALL EXCEPT ] index_spec [, index_spec ...]
index_spec ::= [table.]index_name [ (+) | (-) ] | table.NONE| Form | Effect |
|---|---|
USING INDEX idx1, idx2 |
Only the listed indexes are considered; a sequential scan remains possible if it is cheaper. |
USING INDEX idx(+) |
idx is preferred whenever it can be used. |
USING INDEX idx(-) |
idx is not used. |
USING INDEX ALL EXCEPT idx |
All indexes except idx are considered. |
USING INDEX NONE |
No index is used: sequential scans on all tables. |
USING INDEX t.NONE |
No index is used on table t. |
Tables for which no index is named keep all their indexes. With USING INDEX tab1.idx1, the optimizer chooses between idx1 and a sequential scan for tab1, and among all indexes and a sequential scan for every other table.
An index scan returns rows in index order, so USING INDEX can also be used to obtain an order without ORDER BY. Prefer an explicit ORDER BY, which the optimizer can still satisfy from the index without sorting (see Optimizing ORDER BY clause).
USE, FORCE, IGNORE INDEX
Section titled “USE, FORCE, IGNORE INDEX”FROM table [ { USE | FORCE | IGNORE } INDEX ( index_spec [, index_spec ...] ) ] ...
index_spec ::= [table.]index_name| Hint | Same as | Effect |
|---|---|---|
| USE INDEX (idx) | USING INDEX idx |
Consider only idx (or a sequential scan) |
| FORCE INDEX (idx) | USING INDEX idx(+) |
Prefer idx |
| IGNORE INDEX (idx) | USING INDEX idx(-) |
Do not use idx |
Examples of index hint
Section titled “Examples of index hint”The orders table has indexes on customer_id (fk_orders_customer_id), order_date (idx_orders_order_date) and others. For the orders of customer 17 since 2024, the optimizer picks the more selective customer index:
SELECT /*+ RECOMPILE */ id, order_dateFROM ordersWHERE customer_id = 17 AND order_date >= DATE'2024-01-01';iscan class: orders node[0] index: fk_orders_customer_id term[1] sargs: term[0] cost: 3 card 1Each of the following makes it use the date index instead, with the customer condition as a data filter:
SELECT /*+ RECOMPILE */ id, order_date FROM orders USE INDEX (idx_orders_order_date)WHERE customer_id = 17 AND order_date >= DATE'2024-01-01';
SELECT /*+ RECOMPILE */ id, order_date FROM orders FORCE INDEX (idx_orders_order_date)WHERE customer_id = 17 AND order_date >= DATE'2024-01-01';
SELECT /*+ RECOMPILE */ id, order_date FROM orders IGNORE INDEX (fk_orders_customer_id)WHERE customer_id = 17 AND order_date >= DATE'2024-01-01';
SELECT /*+ RECOMPILE */ id, order_date FROM ordersWHERE customer_id = 17 AND order_date >= DATE'2024-01-01'USING INDEX ALL EXCEPT fk_orders_customer_id;iscan class: orders node[0] index: idx_orders_order_date term[0] sargs: term[1] cost: 5 card 1The Query stmt part of the plan shows the rewriting: USE INDEX becomes using index orders.idx_orders_order_date, FORCE INDEX becomes … (+) and IGNORE INDEX becomes … (-).
USING INDEX NONE forces a sequential scan:
SELECT /*+ RECOMPILE */ id, order_date FROM ordersWHERE customer_id = 17 AND order_date >= DATE'2024-01-01'USING INDEX NONE;sscan class: orders node[0] sargs: term[0] AND term[1] cost: 30 card 1In a join, indexes can be chosen per table, for example USING INDEX o.idx_orders_order_date, c.NONE.
KEYLIMIT
Section titled “KEYLIMIT”An index named in USING INDEX can carry a KEYLIMIT clause, which limits the number of index keys the scan reads:
USING INDEX index_name KEYLIMIT countUSING INDEX index_name KEYLIMIT skip, countWith skip, the scan first skips that many keys. Because the keys are read in index order, this returns the first rows by the index key without a sort, much like LIMIT over an ORDER BY on the key. KEYLIMIT cannot be combined with USING INDEX NONE or with an index excluded by (-).
The orders from 30 December 2025 onward in idx_orders_order_date order: the first three, then three after skipping two:
SELECT id, order_date FROM orders WHERE order_date >= DATE'2025-12-30'USING INDEX idx_orders_order_date KEYLIMIT 3;| id | order_date |
|---|---|
| 455 | 2025-12-30 |
| 933 | 2025-12-30 |
| 1325 | 2025-12-30 |
SELECT id, order_date FROM orders WHERE order_date >= DATE'2025-12-30'USING INDEX idx_orders_order_date KEYLIMIT 2, 3;| id | order_date |
|---|---|
| 1325 | 2025-12-30 |
| 1603 | 2025-12-30 |
| 2232 | 2025-12-30 |
Rows with equal keys are returned in the order of the index entries; add the primary key to the index or use ORDER BY … LIMIT when that order matters.
Query cache
Section titled “Query cache”The QUERY_CACHE hint caches a SELECT together with its result. When the same query text is executed again with the same bind values in the same database, the result is returned from the cache instead of running the query. This helps queries that are repeated often and are expensive, such as dashboard counts:
SELECT /*+ QUERY_CACHE */ YEAR(order_date) AS y, COUNT(*) FROM orders GROUP BY YEAR(order_date);The first execution runs the query and stores the result; later executions read the stored result and are much faster.
The hint has no effect when the query contains:
- Current date or time values, such as SYSDATE or
ADDDATE(SYSDATE, -1); - Serials, column paths, methods, stored procedures or stored functions;
- System tables such as
db_rootor_db_attribute; - System functions such as SYS_GUID().
In corasql, ;info qcache shows the cache: n_entries (cached queries), n_pages (pages of cached results), lookup, hit and miss counters, and for each entry its query_string, creation and last-use time:
corasql> ;info qcache
LIST_CACHE { n_hts 1010 n_entries 1 n_pages 1 lookup_counter 2 hit_counter 1 miss_counter 1 full_counter 0}... query_string = select /*+ QUERY_CACHE */ ... time_created = ... time_last_used = ... ref_count = 1...The number of cached queries is limited by max_query_cache_entries and the size of cached results by query_cache_size_in_pages. When either limit is reached, about 20% of the entries are removed.