oracle9icreate table test(
id                number(9),
mobile            varchar2(15),
service_name      varchar2(20),
service_id        varchar2(20),
content           varchar2(400),
get_time          date);这个表以后可能要有几百万的数据,以下是经常要用到的查询语句
select * from test where mobile='xxxxx';
select * from test where id=xxxx;
select * from test where service_id='xxxxx';select * from test where to_chr(get_time,'yyyymm')='xxxx' and mobile='xxxxx';select * from test where to_chr(get_time,'yyyymm')='xxxx' and service_id='xxxxx';第一个问题:如何建立索引才能达到很好的查询效果?第二个问题:get_time字段建表时是用date类型好,还是换成varchar2类型好,那种会让以后的查询更快?

解决方案 »

  1.   

    id mobile service_name service_id  get_time   
    分别建立索引
    时间最好用data 不过查询的时候 不要to_chr(get_time,'yyyymm') 而应该 get_time between
     a and b
    运算后用不到索引
      

  2.   

    1.检查要在一起查询的字段建复合索引
    2.service_id,get_time,mobile建立复合索引
      

  3.   

    复核索引必须使用前导列,才能被使用上,否则select的时候,用不上索引
      

  4.   

    id如果是主键会带主键索引如果不是就独立建索引;
    mobile 和 get_time建一组索引;
    service_id 和 get_time建一组索引;
    查询时把get_time写在mobile或service_id的后面,否则用不上索引,任何查询字段最好不要做运算,做了运算的字段用不上索引;索引多了会影响插入效率,但是在常用查询字段上建索引可以及大提高查询速度,同时有没有对插入造成的影响在可以容忍范围内的话,多建几个索引也可以,不过要是每个字段都建就没意义了,索引这东西要根据需求来追求查询效率和插入效率的一个均衡
      

  5.   

    1、ID若为主键就不需在索引,否则在ID上建一个
    2、mobile 和serice_id各建一个索引
    3、get_time是否要建根据你的数据的分布,若同一个mobile下的get_time有多少,一般不需要在建了,