这段sql怎么优化?

gang00ge 2011-01-25 11:22:03
select t.con_id,
t.con_no,
t.con_name,
to_char(t.transact_id),
to_char(t.proc_start_time, 'yyyy-mm-dd'),
t.transact_status,
t.dishi_contract,
t.proc_id
from cu_contract_detail t
where t.transact_status in ('2', '3')
and (t.con_participant like '%,103209,%' or
t.con_participant like '%103209,%' or
t.con_participant like '%,103209%' or t.con_participant = '103209')
union
select t.con_id,
t.con_no,
t.con_name,
to_char(t.transact_id),
to_char(t.proc_start_time, 'yyyy-mm-dd'),
t.transact_status,
t.dishi_contract,
t.proc_id
from cu_contract_detail t
where t.transact_status in ('2', '3')
and t.proc_id in (select t1.proc_ins_id
from cu_back_idea t1
where t1.from_person_id = '103209')

cu_contract_detail 这个表有100多个字段,几w条数据。。。。。。。。
cu_back_idea 这个表有70多W条数据。。。。。。。。。。。
这段sql在pl/sql执行要4秒多,能优化吗,怎么优化呢?
...全文
103 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
gang00ge 2011-01-28
  • 打赏
  • 举报
回复
谢谢各位大哥的热心解答。
明天就要放假了,把贴给结了。
gelyon 2011-01-25
  • 打赏
  • 举报
回复
鄙视这丫D领导,屁都不放个就让人优化。。。。
楼主你把你原来的union 换成union all 试试,这丫减少排序时间
Oraclefans_ 2011-01-25
  • 打赏
  • 举报
回复
你仔细对比下记录吧。。。。语句应该没什么问题的。。你看使用我的语句哪些记录没有选择上,让后再修改。。
gang00ge 2011-01-25
  • 打赏
  • 举报
回复
领导说页面显示的太慢,让优化优化。。。。。
我一刚毕业的菜鸟,也不是搞数据库的,sql就会点基础,(Oraclefans_)大哥说的instr函数我都没听过。。。。。
不过(Oraclefans_)大哥写的结果不对啊,原来的查出来有162条,换成(Oraclefans_)大哥写的就只有109条。
那个union是怎么去掉的呢?
gelyon 2011-01-25
  • 打赏
  • 举报
回复
几十万数据,才4秒多点,已经不错了,楼主也要求太高了吧。。
Oraclefans_ 2011-01-25
  • 打赏
  • 举报
回复

select t.con_id,
t.con_no,
t.con_name,
to_char(t.transact_id),
to_char(t.proc_start_time, 'yyyy-mm-dd'),
t.transact_status,
t.dishi_contract,
t.proc_id
from cu_contract_detail t
where t.transact_status in ('2', '3')
and instr(t.con_participant, '103209') > 0
and exists (select 1
from cu_back_idea t1
where t1.from_person_id = '103209'
and t1.proc_ins_id = t.proc_id)
Oraclefans_ 2011-01-25
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 oraclefans_ 的回复:]

(t.con_participant like '%,103209,%' or
t.con_participant like '%103209,%' or
t.con_participant like '%,103209%' or t.con_participant = '103209')
1.把这个使用instr函数来代替
2.sql应该可以写成
SQL ……
[/Quote]
把那个in使用exist代替,然后在cu_back_idea 的proc_ins_id列上建立索引,正好符合exist的要求
外层的表是小表,里层表是大表,这样是最合适使用exists
Oraclefans_ 2011-01-25
  • 打赏
  • 举报
回复
(t.con_participant like '%,103209,%' or
t.con_participant like '%103209,%' or
t.con_participant like '%,103209%' or t.con_participant = '103209')
1.把这个使用instr函数来代替
2.sql应该可以写成

select t.con_id,
t.con_no,
t.con_name,
to_char(t.transact_id),
to_char(t.proc_start_time, 'yyyy-mm-dd'),
t.transact_status,
t.dishi_contract,
t.proc_id
from cu_contract_detail t
where t.transact_status in ('2', '3')
and instr(t.con_participant, '103209') > 0
and t.proc_id in (select t1.proc_ins_id
from cu_back_idea t1
where t1.from_person_id = '103209')
UPC子夜 2011-01-25
  • 打赏
  • 举报
回复
楼主啊,union一般用在显示结果相同,但表不一样,或者用一个sql很难查询出结果的情况
你这两个sql表一样,条件也差不多,就可以不用union,把两个条件合到一起,用or就行
Oraclefans_给你的sql结果变少了,就是因为他把or写成and了,你可以试一下
不过用or会影响效率,用union一般要快一点
还有,exists一般比in快一点
instr是在字符串中查询子字符串,并返回位置,
substr是在字符串中取出一定长度的子字符串
你可以找个oracle函数大全,用到的时候就去查
每个函数的用法都记住也不大现实
Oraclefans_ 2011-01-25
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 gang00ge 的回复:]

额。。。。楼上的大哥莫激动,我也就是打打下手,帮忙搞搞,真优化不了也不会把我咋样滴。。。。
union all 不行啊,结果变多了。
这是个老项目,现在数据量大了,变得很慢,客户要求让优化优化,领导给我分配点任务,让我参与一下。
这些天还是学到不少东西的,今天又学会了个instr函数。。
[/Quote]
嘿嘿。。。不错。。不错。。。。
gang00ge 2011-01-25
  • 打赏
  • 举报
回复
额。。。。楼上的大哥莫激动,我也就是打打下手,帮忙搞搞,真优化不了也不会把我咋样滴。。。。
union all 不行啊,结果变多了。
这是个老项目,现在数据量大了,变得很慢,客户要求让优化优化,领导给我分配点任务,让我参与一下。
这些天还是学到不少东西的,今天又学会了个instr函数。。

3,491

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 高级技术相关讨论专区
社区管理员
  • 高级技术社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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