高分求救,关于count语句的优化

jy02209334 2007-09-24 11:37:23
现在有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 '%@@%' )
...全文
146 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
WangZWang 2007-09-24
  • 打赏
  • 举报
回复
从语句本身是很难优化的,要根据实现的目的,和表结构来做文章,比如建立适当的索引
, distinct 是否可以去掉? 还有 <> 和 %%都会引起表扫描.
kinglht 2007-09-24
  • 打赏
  • 举报
回复
把语句调整一下,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
bjt_ 2007-09-24
  • 打赏
  • 举报
回复
/*+ index_ffs(cust) */ 提示要去掉,这个语句a表就是要全表扫描,你用索引反而会慢,
b表b.cust_id字段一定要加索引即可,

17,377

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 基础和管理
社区管理员
  • 基础和管理社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧