这段sql有没有可以优化的地方

eeff 2010-07-06 03:04:04
就是一个行转列,t_cp_infoext(pk:object_id,lang,name)(fk:object_id)
t_cp_info(pk:OBJECT_ID)
select cpid, lang, spshortname, spname, status, credit, contact, contacttel
from (select distinct a.cpid,
c.lang,
nvl((select b.value
from t_cp_infoext b
where b.object_id = a.object_id
and lower(b.name) = 'shortname'
and c.lang = b.lang),
' ') spshortname,
nvl((select b.value
from t_cp_infoext b
where b.object_id = a.object_id
and lower(b.name) = 'name'
and c.lang = b.lang),
' ') spname,
a.status,
a.credit,
a.contact,
a.contacttel
from t_cp_info a, t_cp_infoext c)
where spshortname <> ' '
or spname <> ' '
order by 1
...全文
85 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
eeff 2010-07-07
  • 打赏
  • 举报
回复
楼上答案稍微改了下,是对的
select a.cpid,
b.lang,
nvl(max(decode(lower(b.name), 'shortname', b.value, null)), ' ') as spshortname,
nvl(max(decode(lower(b.name), 'name', b.value, null)), ' ') as spname,
a.status,
a.credit,
a.contact,
a.contacttel
from t_cp_info@dblink01 a,
t_cp_infoext@dblink01 b
where lower(b.name) in ('shortname', 'name')
and b.object_id = a.object_id
group by a.cpid,
b.lang,
a.status,
a.credit,
a.contact,
a.contacttel
order by 1,2
westneverwin 2010-07-06
  • 打赏
  • 举报
回复
select a.cpid, c.lang, 
nvl(max(decode(lower(b.name),'shortname',b.value,null)),' ') as spshortname,
nvl(max(decode(lower(b.name),'name',b.value,null)),' ') as spname, a.status, a.credit, a.contact, a.contacttel
from t_cp_info a, t_cp_infoext c,t_cp_infoext b
where lower(b.name) in ('shortname','name')
and b.object_id = a.object_id
and c.lang = b.lang
group by a.cpid, c.lang, spshortname, spname, a.status, a.credit, a.contact, a.contacttel
order by 1
iqlife 2010-07-06
  • 打赏
  • 举报
回复
select distinct a.cpid,
c.lang,
c.name ,
c.value namevalue,
a.status,
a.credit,
a.contact,
a.contacttel
from t_cp_info a, t_cp_infoext c
where b.object_id = a.object_id
and lower(c.name) in('shortname','name')
中间的笛卡尔及改成上面的,然后再判断 name,分解spshortname,spname
eeff 2010-07-06
  • 打赏
  • 举报
回复
不做笛卡尔积,没办法返回唯一行啊
然后用distinct去重了,错是没错误了,但是效率太低
iqlife 2010-07-06
  • 打赏
  • 举报
回复
select distinct a.cpid,
c.lang,
nvl((select b.value
from t_cp_infoext b
where b.object_id = a.object_id
and lower(b.name) = 'shortname'
and c.lang = b.lang),
' ') spshortname,
nvl((select b.value
from t_cp_infoext b
where b.object_id = a.object_id
and lower(b.name) = 'name'
and c.lang = b.lang),
' ') spname,
a.status,
a.credit,
a.contact,
a.contacttel
from t_cp_info a, t_cp_infoext c
好怪..怎么会让A,C做笛卡尔集?
mahanso 2010-07-06
  • 打赏
  • 举报
回复
cmd

sqlplus 登陆

set autotrace on
set timing on

执行你这个sql

看一下执行计划,最好把执行计划贴出来,然后让大家帮你看看。

17,086

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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