-- Create table
create table T_AGENT_STATUS_LOG
(
  STATISTICDATE       VARCHAR2(50),
  STATUS              VARCHAR2(50),
  PHONENO             VARCHAR2(50),
  GROUPNO             VARCHAR2(50),
  AGENTNO             VARCHAR2(50),
  STATISTICTIME       INTEGER,
  CALLID              VARCHAR2(50),
  CALLERID            VARCHAR2(50),
  CALLEEID            VARCHAR2(50),
  CALLDIR             VARCHAR2(50),
  CALLTYPE            VARCHAR2(50),
  MONITEDAGENTPHONENO VARCHAR2(50),
  MONITEDAGENTNO      VARCHAR2(50),
  TRANSFERNO          VARCHAR2(50)
)
tablespace TBSETL
  pctfree 10
  initrans 1
  maxtrans 255
  storage
  (
    initial 8M
    next 8M
    minextents 1
    maxextents unlimited
    pctincrease 0
  );
-- Create/Recreate indexes 
create index IDX_AGENTSTATUS_STATISTICDATE on T_AGENT_STATUS_LOG (STATISTICDATE)
  tablespace TBSETL
  pctfree 10
  initrans 2
  maxtrans 255
  storage
  (
    initial 8M
    next 8M
    minextents 1
    maxextents unlimited
    pctincrease 0
  );在对表数据进行查询的时候使用STATISTICDATE > 和<查询的时候,部分区间可以用到缩影部分区间用不到索引,这是为什么!
select * from t_agent_status_log t
where t.statisticdate > '2010-09-02'
and t.statisticdate < '2010-12-09'
and t.phoneno = '55613'
这个是全表扫描select * from t_agent_status_log t
where t.statisticdate > '2010-12-02'
and t.statisticdate < '2010-12-09'
and t.phoneno = '55613'
这是是正常使用到IDX_AGENTSTATUS_STATISTICDATE索引statisticdate 数据格式为yyyy-mm-dd hh24:mi:ss格式的字符串!
前期程序直接将时间当字符串入库。谁帮忙解答下。谢谢

解决方案 »

  1.   

    t.statisticdate > '2010-09-02'
    and t.statisticdate < '2010-12-09'之间的数据比较多吧,数据库自动选择要不要使用索引。
      

  2.   


    不对的,
    select count(*) from t_agent_status_log t
    where t.statisticdate > '2010-11-30'
    and t.statisticdate < '2010-12-01'
    这样都不会用索引的,这个数据有几个月的数据。
    对表重新分析也没用的测试了下貌似是
    字符串索引在使用><进行筛选的时候,字符串不一样的位置至末尾都是同一类型的字符就可以正常使用的到缩影,否则不行!如果2010-11-01 和2010-11-99 这个是可以用的到索引的因为字符比较到01和99才有区别,
    而2010-11-30 和2010-12-01 这个就不行,中间混杂着其他的字符1-30,2-01求正解!
      

  3.   

    select * from t_agent_status_log t
    where t.statisticdate > '2010-09-02'
    and t.phoneno = '55613'
    union all
    select * from t_agent_status_log t
    where t.statisticdate < '2010-12-09'
    and t.phoneno = '55613'
      

  4.   

    10g之后,ORACLE会自动查找数据最快的路线,从而判断走不走索引,CBO基于成本优化
      

  5.   

    同意, 这个sql没有那么复杂,Oracle会默认不用索引