现在有cust,serv两张表,现在必须要关联两张表进行count,数据量量张表都在50W左右,现在查询要6秒左右,有什么好的优化方法吗,sql语句如下:
select  /*+ index_ffs(cust) */ count(distinct a.cust_no) as record_count From cust a, serv b 
Where  a.cust_type =1 And  a.vip_type >=-1 
And a.mature_grade >=1 And  a.state <>'70X'  and a.cust_id = b.cust_id  And (a.organ_code like '%@@%' )

解决方案 »

  1.   

    从语句本身是很难优化的,要根据实现的目的,和表结构来做文章,比如建立适当的索引
    , distinct 是否可以去掉? 还有 <> 和 %%都会引起表扫描.
      

  2.   

    /*+ index_ffs(cust) */ 提示要去掉,这个语句a表就是要全表扫描,你用索引反而会慢,
    b表b.cust_id字段一定要加索引即可,
      

  3.   

    把语句调整一下,oracle where子句是从右到左解析的,
    select  /*+ index_ffs(cust) */ count(distinct a.cust_no) as record_count From cust a, serv b 
    Where  a.cust_type =1 And  a.vip_type >=-1 
    And a.mature_grade >=1 And  a.state <>'70X'  and a.cust_id = b.cust_id  And (a.organ_code like '%@@%' )改成
    select  /*+ index_ffs(cust) */ count(distinct a.cust_no) as record_count From cust a, serv b 
    Where a.cust_id = b.cust_id and a.organ_code like '%@@%' and (a.state >'70X' or a.state<'70X')
    and a.mature_grade >=1 And  a.vip_type >=-1 a.cust_type =1