如果设置OPTIMIZER_MODE=choose,并且已经有了表的统计数据,oracle根据什么来决定是用all_rows还是first_rows来做执行计划?如果设置OPTIMIZER_MODE=all_rows,那以后所有的查询都会用表扫描而不用索引么?即使表很大,那这算什么优化??

解决方案 »

  1.   

    错误的优化模式:如果OPTIMIZER_MODE被设置为ALL_ROWS或者CHOOSE,那么SQL优化器会更乐于使用全表扫描。如果想进行快速的OLTP优化,必须首先确认已经将OPTIMIZER_MODE设置成FIRST_ROWS。
      

  2.   

    如果OPTIMIZER_MODE被设置为ALL_ROWS或者CHOOSE,那么SQL优化器会更乐于使用全表扫描如果设置成ALL_ROWS或者CHOOSE,那就是说永远不可能用到索引了么??那ALL_ROWS或者CHOOSE还有什么意义?每次查询都要做全表扫描,这算什么优化?
      

  3.   

    解释一下,其实你的概念可能不是很清晰。1、决定使用哪种模式是由在init<SID>.ora中进行设置来决定的,比如你就想使用基于规则的优化方式,你可以在init<SID>.ora中加入OPTIMIZER_MODE=rule这样一行。2、all_row和first rows不是你说的那种意思,他们是优化器在设定执行计划取样的范围,一般all_rows是不用的。3、默认的是用choose 的方式,即如果你的表有统计信息走基于cose(代价)的方式,如果没有统计信息走基于rule(规则)的方式,这就是有时你的表明明有索相而不走索相的原因。
      

  4.   

    多谢:)all_row和first rows不是你说的那种意思,他们是优化器在设定执行计划取样的范围,一般all_rows是不用的如果我在init.ora里设置成ALL_ROWS或者CHOOSE,那优化器做出的执行计划是不用索引的,实际执行时候也不用到索引,这么说对么?
      

  5.   

    OPTIMIZER_MODE
    Parameter type  String 
     
    Syntax  OPTIMIZER_MODE = {first_rows_[1 | 10 | 100 | 1000] | first_rows | all_rows | choose | rule} 
     
    Default value  choose 
     
    Parameter class  Dynamic: ALTER SESSION 
     
     
    OPTIMIZER_MODE establishes the default behavior for choosing an optimization approach for the instance. Values: rule The optimizer chooses a rule-based approach for all SQL statements regardless of the presence of statistics. choose The optimizer chooses between a cost-based approach and a rule-based approach based on whether statistics are available. If the data dictionary contains statistics for at least one of the accessed tables, then the optimizer uses a cost-based approach and optimizes with a goal of best throughput. If the data dictionary contains only some statistics, then the cost-based approach is used, and the optimizer must guess the statistics for the subjects without any statistics. This can result in sub-optimal execution plans. If the data dictionary contains no statistics for any of the accessed tables, then the optimizer uses a rule-based approach. first_rows_n The optimizer uses a cost-based approach, regardless of the presence of statistics, and optimizes with a goal of best response time to return the first n rows (where n = 1, 10, 100, 1000). first_rows The optimizer uses a mix of costs and heuristics to find a best plan for fast delivery of the first few rows. all_rows The optimizer uses a cost-based approach for all SQL statements in the session regardless of the presence of statistics and optimizes with a goal of best throughput (minimum resource use to complete the entire statement).