有个SQL查询语句涉及到一个大表和一个小表,大表建了索引,小表需要建索引吗?

解决方案 »

  1.   

    小表DML不会很多、不会因为维护索引带来额外开销、即便你建了不需要也没关系、
    另外、你的两表的关联方法是nested loop join?
      

  2.   

    你看看你的sql语句对应的执行计划,建个索引还是有必要的。
      

  3.   

    根据执行计划,具体情况具体分析,比如小表建索引一句sql快0.01s,但是跟着大表,要运行一千万次,那也很可观。
    如果建索引后不影响预期的执行计划,也没什么影响,那就无所谓。
      

  4.   

    1、如果表足够小,access table full ,读几个块就可以将小表搞定的话,就没有必要建立外键之外的索引。
    2、如果小表中的单行数据量非常大的话,建立一个索引还是有必要的。
    3、如果小表是有外键约束的话,建立一个索引也是有必要的,这里并不是为了提高查询效率,而是为了避免锁。
    4、如果小标本身仅仅是查询,更新很少,或者没有更新,比如是系统中的一个参数表的话,可以考虑将其建立为一个索引组织表。
    5、一张大表和一张小表,Oracle一般会倾向于使用NL的连接方式,通常将小表作为LEADING表,有必要根据小标的连接条件,在大表中建立相应的索引,或者根据实际情况建立组合索引。
      

  5.   

    贴个SQL吧,大家帮我看看
    select t3.* from (select 
     
    t1.id,t1.deviceid,t2.device_type_id,t1.geomtype,t1.left,t1.top,t1.right,t1.bottom,t1.orientation,t1.scale,t1.text,t1.height,t1.font_orientation from t1,t2 where 
     
    t1.geomtablename=t2.geom_table_name) t3,t4 where (t4.min_scale<={:scale} and t4.max_scale>={:scale} and t3.device_type_id=t4.device_type_id) and ((t3.left>={:xmin} and 
     
    t3.left<={:xmax}and t3.bottom>={:ymin} and t3.bottom<={:ymax}) or (t3.right>={:xmin} and t3.right<={:xmax} and t3.top>={:ymin} and t3.top<={:ymax}) or (t3.left>=
     
    {:xmin} and t3.left<={:xmax} and t3.top>={:ymin} and t3.top<={:ymax}) or (t3.right>={:xmin} and t3.right<={:xmax} and t3.bottom>={:ymin} and t3.bottom<={:ymax}) or 
     
    (t3.left<={:xmin} and t3.bottom<={:ymin} and t3.right>={:xmax} and t3.top>={:ymax}) or (((t3.left>={:xmin} and t3.left<={:xmax}) or (t3.right>={:xmin} and t3.right<=
     
    {:xmax})) and t3.top>={:ymax} and t3.bottom<={:ymin}) or (((t3.bottom>={:ymin} and t3.bottom<={:ymax}) or (t3.top>={:ymin} and t3.top<={:ymax})) and t3.left<={:xmin} 
     
    and t3.right>={:xmax}))
    t1表记录200多万条,t2、t4不到200条记录,t1表以下字段建了索引:id\left\top\right\bottom\deviceid\geomtablename,t2和t4都没有建索引,我这样做合理吗?大家给点意见。