Tuesday, March 10, 2015

Why doesn't Oracle use my index?

Why doesn't Oracle use my index?

Question:  I have a SQL query where Oracle is not using an index.  The explain plan shows TABLE ACCESS FULL.  I verified that the index exists, but I cannot seem to force Oracle to use the index.  Why does Oracle ignore an index?  How do you force an index to be used?
Answer:  The Oracle cost-based optimizer carefully evaluates every query when making the decision whether to invoke a full-table scan or index access, and you can force index usage with in index hint.  The goal of the SQL optimizer is to only force an index when it's the "best" access plan, given your optimization goals:


Some of the variables that influence the decision to force or ignore an index include:
  • The number of blocks in the table - Small tables are accessed faster with a full scan and forcing index usage may hurt performance.
     
  • System statistics - the dbms_stats.gather_system_stats procedure measures external timing for index access (sequential reads) and full-scan access (scattered reads).  If Oracle sees expensive index disk reads, it may ignore an index.
     
  • Optimizer parms - You can adjust several optimizer parms to force Oracle to use an index:
optimizer_mode - The all_rows access method often favors a parallel full-table scan over an index scan. The first_rows optimizer_mode will often stop Oracle from ignoring an index because it favors index access over computing resources.
optimizer_index_cost_adj - This parameter alters the costing algorithm for access paths involving indexes. The smaller the value, the lower the cost of index access.

sort_area_size (if not using pga_aggregate_target) - The sort_area_size influences the CBO when deciding whether to perform an index access or a sort of the result set. The higher the value for sort_area_size, the more likely that a sort will be performed in RAM, and the more likely that the CBO will favor a sort over presorted index retrieval.

No comments:

Post a Comment

  How to Change Instance Type & Security Group of EC2 in AWS By David Taylor Updated April 29, 2023 EC2 stands for Elastic Compute Cloud...