原来是9i的数据库,现在移植到10g上来,10g的是用两台服务器搭建rac模式的,9i只是单台的。
库表简单的列下,如下:
create table trans_item
(
   trans_nbr       char(14) not null,      /*流水号*/
   add_info varchar2(9) not null,   /*商户*/
   trans_time      char(14) not null,      /*日期时间*/
   trans_month     char(2) not null,       /*月份*/
deduct_flag number(1)  default 0  /*商户状态*/
)
partition by list (trans_month)
(
  partition P01 values ('01'),
  partition P02 values ('02'),
  partition P03 values ('03'),
  partition P04 values ('04'),
  partition P05 values ('05'),
  partition P06 values ('06'),
  partition P07 values ('07'),
  partition P08 values ('08'),
  partition P09 values ('09'),
  partition P10 values ('10'),
  partition P11 values ('11'),
  partition P12 values ('12')
);
alter table trans_item add constraint pk_trans_item primary key (trans_nbr)
  using index tablespace user_index;
create index idx_trans_item1 on trans_item (trans_time) tablespace user_index;
create index idx_trans_item2 on trans_item (deduct_flag) tablespace user_index;现在问题是:
库表记录数1500w万,但deduct=1的记录估计在100条左右
使用select sum(trans_amount) from trans_item where add_ifno=:lsAddInfo and deduct_flag=1该语句的时候,在9i的机器上执行正常,大概在0.5秒左右,但在10g的机器上运行,60多秒还没出来。使用执行计划查看,在 9i的机器上是如下:
id    operation
0     select statement
1     sort aggregate
2     table access by global index rowid
3     index range scan
但在10g上,却如下:
id    operation
0     select statement
1     sort aggregate
2     partition list all
3     tables access full
10g的数据库,是从9i上用export完全备份用 import导入的,为什么在9i上查询用到了索引idx_trans_item2 ,而10g上却是全表扫描?
后来我在10g上把idx_trans_item2索引删除后再重新建、把库表删除重新建、甚至把add_info、deduct_flag建成双索引,结果都还是扫描全表。但后来把用户drop掉,重新import一遍,结果居然执行计划的结果和9i的一样,都用到了索引。
哪位高手能给解释下?实在搞不懂!

解决方案 »

  1.   

    应该是你的表是刚刚导入的,没有做执行计划方面的前处理:
    1.先执行下面语句:
    select table_name, num_rows, last_analyzed 
    from user_tables where table_name = '大写表名';看看num_rows是不是0,last_analyzed是不是NULL
    如果是,再做步骤22.手动执行一下下面的Package:
    execute dbms_stats.gather_table_stats('表的用户名', '表名');--都要大写
    3.执行完之后,再查看一下步骤1的SQL
    此时的num_rows应该是你表里count(*)的数量,last_analyzed应该是你执行上面Package的时间现在再看你的表的执行计划试试。
      

  2.   

    num_rows        last_analyzed 
    14778860 2012-3-7 22:13:47唉,现在模拟不出全表扫描的情况了。是不是这个一定要做表分析?如果不做表分析的话,不是会自动分析吗?