{"id":89,"date":"2009-04-02T12:26:36","date_gmt":"2009-04-02T06:56:36","guid":{"rendered":"http:\/\/www.oratraining.com\/blog\/?p=89"},"modified":"2009-04-02T12:26:36","modified_gmt":"2009-04-02T06:56:36","slug":"oracle-sql-hints","status":"publish","type":"post","link":"https:\/\/www.oratraining.com\/blog\/2009\/04\/oracle-sql-hints\/","title":{"rendered":"Oracle SQL hints"},"content":{"rendered":"<p>\/*+ hint *\/<\/p>\n<p>\/*+ hint(argument) *\/<\/p>\n<p>\/*+ hint(argument-1 argument-2) *\/<\/p>\n<p>All hints except \/*+ rule *\/ cause the CBO to be used. Therefore, it is good practise to analyze the underlying tables if hints are used (or the query is <em>fully<\/em> hinted.<\/p>\n<p>There should be no schema names in hints. Hints must use aliases if alias names are used for table names. So the following is wrong:<\/p>\n<p>select \/*+ index(scott.emp ix_emp) *\/ from scott.emp <strong>emp_alias<\/strong><\/p>\n<p>better:<\/p>\n<p>select \/*+ index(<strong>emp_alias<\/strong> ix_emp) *\/ &#8230; from scott.emp <strong>emp_alias<\/strong><\/p>\n<p><strong>Why using hints<\/strong><\/p>\n<p><!--more--><br \/>\nIt is a perfect valid question to ask why hints should be used. Oracle comes with an <span style=\"text-decoration: underline;\">optimizer<\/span> that promises to optimize a <span style=\"text-decoration: underline;\">query&#8217;s execution plan<\/span>. When this optimizer is really doing a good job, no hints should be required at all.<\/p>\n<p>Sometimes, however, the characteristics of the data in the database are changing rapidly, so that the optimizer (or more accuratly, its statistics) are out of date. In this case, a hint could help.<\/p>\n<p>It must also be noted, that Oracle allows to <em>lock<\/em> the statistics when they look ideal which should make the hints meaningless again.<\/p>\n<p><strong>Hint categories<\/strong><\/p>\n<p>Hints can be categorized as follows:<\/p>\n<ul class=\"unIndentedList\">\n<li> Hints for Optimization Approaches and Goals,<\/li>\n<li> Hints for Access Paths, Hints for Query Transformations,<\/li>\n<li> Hints for Join Orders,<\/li>\n<li> Hints for Join Operations,<\/li>\n<li> Hints for Parallel Execution,<\/li>\n<li> Additional Hints<\/li>\n<\/ul>\n<p><strong>Documented Hints<\/strong><\/p>\n<p><strong>Hints for Optimization Approaches and Goals<\/strong><\/p>\n<ul class=\"unIndentedList\">\n<li> ALL_ROWS<br \/>\nOne of the hints that &#8216;invokes&#8217; the <span style=\"text-decoration: underline;\">Cost based optimizer<\/span><br \/>\nALL_ROWS is usually used for <em>batch processing<\/em> or <em>data warehousing<\/em> systems.<\/li>\n<li> FIRST_ROWS<br \/>\nOne of the hints that &#8216;invokes&#8217; the <span style=\"text-decoration: underline;\">Cost based optimizer<\/span><br \/>\nFIRST_ROWS is usually used for <em>OLTP<\/em> systems.<\/li>\n<li> CHOOSE<br \/>\nOne of the hints that &#8216;invokes&#8217; the <span style=\"text-decoration: underline;\">Cost based optimizer<\/span><br \/>\nThis hint lets the server choose (between ALL_ROWS and FIRST_ROWS, based on <span style=\"text-decoration: underline;\">statistics gathered<\/span>.<\/li>\n<li> RULE<br \/>\nThe RULE hint should be considered deprecated as it is dropped from Oracle9i2.<\/li>\n<\/ul>\n<p>See also the following initialization parameters: <span style=\"text-decoration: underline;\">optimizer_mode<\/span>, <span style=\"text-decoration: underline;\">optimizer_max_permutations<\/span>, <span style=\"text-decoration: underline;\">optimizer_index_cost_adj<\/span>, <span style=\"text-decoration: underline;\">optimizer_index_caching<\/span> and<\/p>\n<p><strong>Hints for Access Paths<\/strong><\/p>\n<ul class=\"unIndentedList\">\n<li> CLUSTER<br \/>\nPerforms a <span style=\"text-decoration: underline;\">nested loop<\/span> by the cluster index of one of the tables.<\/li>\n<li> FULL<br \/>\nPerforms <span style=\"text-decoration: underline;\">full table scan<\/span>.<\/li>\n<li> HASH<br \/>\nHashes one table (full scan) and creates a hash index for that table. Then hashes other table and uses hash index to find corresponding records. Therefore not suitable for &lt; or &gt; join conditions.<\/li>\n<li> ROWID<br \/>\nRetrieves the row by <span style=\"text-decoration: underline;\">rowid<\/span><\/li>\n<li> INDEX<br \/>\nSpecifying that index index_name should be used on table tab_name: \/*+ index (tab_name index_name) *\/<br \/>\nSpecifying that the index should be used the the <span style=\"text-decoration: underline;\">CBO<\/span> thinks is most suitable. (Not always a good choice).<br \/>\nStarting with Oracle 10g, the index hint can be described: \/*+ index(my_tab my_tab(col_1, col_2)) *\/. Using the index on my_tab that starts with the columns col_1 and col_2.<\/li>\n<li> INDEX_ASC<\/li>\n<li> INDEX_COMBINE<\/li>\n<li> INDEX_DESC<\/li>\n<li> INDEX_FFS<\/li>\n<li> INDEX_JOIN<\/li>\n<li> NO_INDEX<\/li>\n<li> AND_EQUAL<br \/>\nThe AND_EQUAL hint explicitly chooses an execution plan that uses an access path that merges the scans on several single-column indexes<\/li>\n<\/ul>\n<p><strong>Hints for Query Transformations<\/strong><\/p>\n<ul class=\"unIndentedList\">\n<li> FACT<br \/>\nThe FACT hint is used in the context of the star transformation to indicate to the transformation that the hinted table should be considered as a fact table.<\/li>\n<li> MERGE<\/li>\n<li> NO_EXPAND<\/li>\n<li> NO_EXPAND_GSET_TO_UNION<\/li>\n<li> NO_FACT<\/li>\n<li> NO_MERGE<\/li>\n<li> NOREWRITE<\/li>\n<li> REWRITE<\/li>\n<li> STAR_TRANSFORMATION<\/li>\n<li> USE_CONCAT<\/li>\n<\/ul>\n<p><strong>Hints for Join Operations<\/strong><\/p>\n<ul class=\"unIndentedList\">\n<li> DRIVING_SITE<\/li>\n<li> HASH_AJ<\/li>\n<li> HASH_SJ<\/li>\n<li> LEADING<\/li>\n<li> MERGE_AJ<\/li>\n<li> MERGE_SJ<\/li>\n<li> NL_AJ<\/li>\n<li> NL_SJ<\/li>\n<li> USE_HASH<\/li>\n<li> USE_MERGE<\/li>\n<li> USE_NL<\/li>\n<\/ul>\n<p><strong>Hints for Parallel Execution<\/strong><\/p>\n<ul class=\"unIndentedList\">\n<li> NOPARALLEL<\/li>\n<li> PARALLEL<\/li>\n<li> NOPARALLEL_INDEX<\/li>\n<li> PARALLEL_INDEX<\/li>\n<li> PQ_DISTRIBUTE<\/li>\n<\/ul>\n<p><strong>Additional Hints<\/strong><\/p>\n<ul class=\"unIndentedList\">\n<li> ANTIJOIN <a name=\"append\"><\/a><\/li>\n<li> APPEND<br \/>\nIf a table or an index is specified with <span style=\"text-decoration: underline;\">nologging<\/span>, this hint applied with an insert statement produces a <strong>direct path insert<\/strong> which reduces generation of <span style=\"text-decoration: underline;\">redo<\/span>.<\/li>\n<li> BITMAP<\/li>\n<li> BUFFER<\/li>\n<li> CACHE<\/li>\n<li> CARDINALITY<\/li>\n<li> CPU_COSTING<\/li>\n<li> <span style=\"text-decoration: underline;\">DYNAMIC_SAMPLING<\/span><\/li>\n<li> INLINE<\/li>\n<li> MATERIALIZE<\/li>\n<li> NO_ACCESS<\/li>\n<li> NO_BUFFER<\/li>\n<li> NO_MONITORING<\/li>\n<li> NO_PUSH_PRED<\/li>\n<li> NO_PUSH_SUBQ<\/li>\n<li> NO_QKN_BUFF<\/li>\n<li> NO_SEMIJOIN<\/li>\n<li> NOAPPEND<\/li>\n<li> NOCACHE<\/li>\n<li> OR_EXPAND<\/li>\n<li> ORDERED<\/li>\n<li> ORDERED_PREDICATES<\/li>\n<li> PUSH_PRED<\/li>\n<li> PUSH_SUBQ<\/li>\n<li> <span style=\"text-decoration: underline;\">QB_NAME<\/span><\/li>\n<li> <span style=\"text-decoration: underline;\">RESULT_CACHE<\/span> (<span style=\"text-decoration: underline;\">Oracle 11g<\/span>)<\/li>\n<li> SELECTIVITY<\/li>\n<li> SEMIJOIN<\/li>\n<li> SEMIJOIN_DRIVER<\/li>\n<li> STAR<br \/>\nThe STAR hint forces a star query plan to be used, if possible. A star plan has the largest table in the query last in the join order and joins it with a nested loops join on a concatenated index. The STAR hint applies when there are at least three tables, the large table&#8217;s concatenated index has at least three columns, and there are no conflicting access or join method hints. The optimizer also considers different permutations of the small tables.<\/li>\n<li> SWAP_JOIN_INPUTS<\/li>\n<li> USE_ANTI<\/li>\n<li> USE_SEMI<\/li>\n<\/ul>\n<p><strong>Undocumented hints:<\/strong><\/p>\n<ul class=\"unIndentedList\">\n<li> BYPASS_RECURSIVE_CHECK<br \/>\nWorkaraound for bug 1816154<\/li>\n<li> BYPASS_UJVC<\/li>\n<li> CACHE_CB<\/li>\n<li> CACHE_TEMP_TABLE<\/li>\n<li> CIV_GB<\/li>\n<li> COLLECTIONS_GET_REFS<\/li>\n<li> CUBE_GB<\/li>\n<li> CURSOR_SHARING_EXACT<\/li>\n<li> DEREF_NO_REWRITE<\/li>\n<li> DML_UPDATE<\/li>\n<li> DOMAIN_INDEX_NO_SORT<\/li>\n<li> DOMAIN_INDEX_SORT<\/li>\n<li> DYNAMIC_SAMPLING<\/li>\n<li> DYNAMIC_SAMPLING_EST_CDN<\/li>\n<li> EXPAND_GSET_TO_UNION<\/li>\n<li> FORCE_SAMPLE_BLOCK<\/li>\n<li> GBY_CONC_ROLLUP<\/li>\n<li> GLOBAL_TABLE_HINTS<\/li>\n<li> HWM_BROKERED<\/li>\n<li> IGNORE_ON_CLAUSE<\/li>\n<li> IGNORE_WHERE_CLAUSE<\/li>\n<li> INDEX_RRS<\/li>\n<li> INDEX_SS<\/li>\n<li> INDEX_SS_ASC<\/li>\n<li> INDEX_SS_DESC<\/li>\n<li> LIKE_EXPAND<\/li>\n<li> LOCAL_INDEXES<\/li>\n<li> MV_MERGE<\/li>\n<li> NESTED_TABLE_GET_REFS<\/li>\n<li> NESTED_TABLE_SET_REFS<\/li>\n<li> NESTED_TABLE_SET_SETID<\/li>\n<li> NO_FILTERING<\/li>\n<li> NO_ORDER_ROLLUPS<\/li>\n<li> NO_PRUNE_GSETS<\/li>\n<li> NO_STATS_GSETS<\/li>\n<li> NO_UNNEST<\/li>\n<li> NOCPU_COSTING<\/li>\n<li> OVERFLOW_NOMOVE<\/li>\n<li> PIV_GB<\/li>\n<li> PIV_SSF<\/li>\n<li> PQ_MAP<\/li>\n<li> PQ_NOMAP<\/li>\n<li> REMOTE_MAPPED<\/li>\n<li> RESTORE_AS_INTERVALS<\/li>\n<li> SAVE_AS_INTERVALS<\/li>\n<li> SCN_ASCENDING<\/li>\n<li> SKIP_EXT_OPTIMIZER<\/li>\n<li> SQLLDR<\/li>\n<li> SYS_DL_CURSOR<\/li>\n<li> SYS_PARALLEL_TXN<\/li>\n<li> SYS_RID_ORDER<\/li>\n<li> TIV_GB<\/li>\n<li> TIV_SSF<\/li>\n<li> UNNEST<\/li>\n<li> USE_TTT_FOR_GSETS<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\/*+ hint *\/ \/*+ hint(argument) *\/ \/*+ hint(argument-1 argument-2) *\/ All hints except \/*+ rule *\/ cause the CBO to be used. Therefore, it is good practise to analyze the underlying tables if hints are used (or the query is fully hinted. There should be no schema names in hints. Hints must use aliases if [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[26,30,4,10],"tags":[204,196,98,198],"class_list":["post-89","post","type-post","status-publish","format-standard","hentry","category-applications-performance-tuning","category-database-performance-tuning","category-oracle","category-oracle-dba","tag-concurrent-manager","tag-oracle","tag-oracle-applications","tag-oracle-applications-dba"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.oratraining.com\/blog\/wp-json\/wp\/v2\/posts\/89","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.oratraining.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.oratraining.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.oratraining.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.oratraining.com\/blog\/wp-json\/wp\/v2\/comments?post=89"}],"version-history":[{"count":0,"href":"https:\/\/www.oratraining.com\/blog\/wp-json\/wp\/v2\/posts\/89\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.oratraining.com\/blog\/wp-json\/wp\/v2\/media?parent=89"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.oratraining.com\/blog\/wp-json\/wp\/v2\/categories?post=89"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.oratraining.com\/blog\/wp-json\/wp\/v2\/tags?post=89"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}