Wednesday, June 15, 2011

Oracle Hints


Introduction
Performance is one of the primary objectives of a database. Fine tuned data retrieval and database operations can save lot of time for users and avoid troubleshoot nightmares of database developers.
The root cause of database performance degradation lies with the working of optimizer. Optimizer speed lowers and hence the query speed. There are multiple approaches and many ways to achieve database process performance. Thorough analysis of the data, explain plan is required to adopt the most feasible method to tune a SQL query. 
Out of these many approaches, Oracle SQL hints are one of the tricks to tune a query. SQL hints are optimizer directives which were introduced in Oracle 8.1.0 release. They instruct optimizer to follow an alternative path of query execution. At times, a developer can analyze a complex data better than an Optimizer. Therefore, he may choose a better execution plan than the optimizer by enforcing hints in the queries.  Like other approaches, it is not a full proof method of tuning but it is suggestible in case of complex queries.
SQL Hints Usage  Below are some situations when Hints are required to be used when the Optimizer fails:- 
  • Bugs
  • Use of Bind Variables which disable Histograms
  • Poorly written SQLs
  • High frequency of Change of Data
  • Faulty (incomplete) Configuration Settings
  • Dynamic vs. Static SQL 
Other circumstances where the use of Hints may be required:- 
  • Hints can be used to join external tables.
  • Hints can be used to force the optimizer to choose different join paths which will fetch different results in the query execution.
  • Hints are used to tell the optimizer which data access method to use (as for example in a flashback query). The method may be a full table scan or a different index. 
Note: – Sometimes, the optimizer can ‘lock’ the statistics when they look ideal. In such a case ‘Hints’ lose their importance.
Syntax:-  Hints appear as comments to the SELECT and DML statements. Without these keywords, they hold no meeting and would only appear as a comment. Hints can be used in any of the following ways: 
  • /*+ hint */
  • /*+ hint (argument) */
  • /*+ hint (argument-1 argument-2) */
Notes 
  • Hints can be used in SELECT, INSERT, UPDATE or DELETE statements
  • It can be a part of subquery too
  • It can appear in any of the participating queries of a compound query using SET operator
  • There should be no schema names in hints
  • All hints except the /*+ rule */ cause the Cost Based Optimizer (CBO) to be used
  • Hints operate on a simple view but not on a complex view 
Illustration 
1. The query below uses ALL_ROWS hint to select employee details from EMPLOYEE table 
SELECT /*+ ALL_ROWS */ EMPNO, ENAME, SALARY, DETPNO
FROM EMPLOYEE WHERE EMPNO=100
2. The SQL query below uses RULE hint to change the optimizer mode from COST based to RULE based
SELECT /*+ RULE */ EMPNO, ENAME, SALARY, DETPNO 
FROM EMPLOYEE WHERE EMPNO=100
3. The FULL hint used in the below SQL query enforces optimizer to do FULL TABLE SCAN
SELECT /*+ FULL(E) */ EMPNO, ENAME, SALARY
FROM EMPLOYEE E WHERE DEPTNO IN (SELECT DEPTNO 
                                 FROM DEPARTMENT
                                 WHERE LOCATION_ID=1009)
Types of Hints
Based on their usage area, SQL hints can be classified as below. 
  • Hints for Optimization Approaches and Goals
  • Hints for Access Paths, Hints for Query Transformations
  • Hints for Join Orders
  • Hints for Join Operations
  • Hints for Parallel Execution
  • Additional Hints 
Hints for Optimization Approaches and Goals
HintFunctionUse
ALL_ROWSInvokes the CBOUsed for batch processing, data warehousing systems
FIRST_ROWSInvokes the CBOUsed for On Line Transfer Protocol (OLTP) systems
CHOOSEInvokes the CBOIt lets the server choose between ALL_ROWS and FIRST_ROWS, based on the statistics gathered by the CBO
 Hints for Access Paths 
CLUSTERINDEXINDEX_FFS
FULLINDEX_ASCINDEX_JOIN
HASHINDEX_COMBINENO_INDEX
ROWIDINDEX_DESCAND_EQUAL
Hints for Query Transformations 
FACTNO_FACT
MERGENO_MERGE
NO_EXPANDNOREWRITE
NO_EXPAND_GSET_TO_UNIONREWRITE
USE_CONCATSTAR_TRANSFORMATION
Hints for Join Operations 
DRIVING_SITEMERGE_AJUSE_HASH
HASH_AJMERGE_SJUSE_MERGE
HASH_SJNL_AJUSE_NL
LEADINGNL_AJ 
 Hints for Parallel Execution 
NO PARALLEL
PARALLEL
NOPARALLEL_INDEX
PARALLEL_INDEX
PQ_DISTRIBUTE
Miscellaneous Hints 
ANTIJOINDYNAMIC_SAMPLING
APPENDINLINE
BITMAPMATERIALIZE
BUFFERNO_ACCESS
CACHENO_BUFFER
CARDINALITYNO_MONITORING
CPU_COSTINGNO_PUSH_PRED
NO_PUSH_SUBQORDERED_PREDICATES
NO_QKN_BUFFPUSH_PRED
NO_SEMIJOINPUSH_SUBQ
NOAPPENDNOCACHE
STARSEMIJOIN
SWAP_JOIN_INPUTSSEMIJOIN_DRIVER
USE_ANTIOR_EXPAND
USE_SEMIORDERED
QB_NAMESELECTIVITY
Hint introduced in Oracle 11g 
RESULT_CACHE – The hint was introduced to cache the result of the SELECT query for the same set of inputs and values. It results in better performance if the same query has to be executed for multiple numbers of times.
Undocumented Hints 
BYPASS_RECURSIVE_CHECKCURSOR_SHARING_EXACT
BYPASS_UJVCDEREF_NO_REWRITE
CACHE_CBDML_UPDATE
CACHE_TEMP_TABLEDOMAIN_INDEX_NO_SORT
CIV_GBDOMAIN_INDEX_SORT
COLLECTIONS_GET_REFSDYNAMIC_SAMPLING
CUBE_GBDYNAMIC_SAMPLING_EST_CDN
SYS_PARALLEL_TXNSYS_RID_ORDER
REMOTE_MAPPEDRESTORE_AS_INTERVALS
SYS_DL_CURSORNO_UNNEST
SQLLDRUSE_TTT_FOR_GSETS
NESTED_TABLE_GET_REFSNESTED_TABLE_SET_SETID
NESTED_TABLE_SET_REFSNO_FILTERING
EXPAND_GSET_TO_UNIONPIV_GB
FORCE_SAMPLE_BLOCKTIV_GB
GBY_CONC_ROLLUPSAVE_AS_INTERVALS
GLOBAL_TABLE_HINTSNOCPU_COSTING
HWM_BROKEREDPQ_NOMAP
NO_PRUNE_GSETSNO_ORDER_ROLLUPS
INDEX_RRSSCN_ASCENDING
INDEX_SSOVERFLOW_NOMOVE
INDEX_SS_ASCPQ_MAP
INDEX_SS_DESCNO_STATS_GSETS
LIKE_EXPANDUNNEST
LOCAL_INDEXESSKIP_EXT_OPTIMIZER
MV_MERGEPIV_SSF
TIV_SSFIGNORE_ON_CLAUSE
IGNORE_WHERE_CLAUSE 
Obsolete Hints RULE – Its usage has been deprecated by Oracle after 9i release. Earlier, it used to work similar to the current COST hint i.e. to toggle over the optimizers.

No comments:

Post a Comment