不知道大家有没有遇到过一个 sql 连了 7,8 张表

2017-03-04 13:32:59 +08:00
 chaleaoch
在实际的生产环境中,这样会带来很大的效率问题吗?
昨天面试一个大兄弟,单张表只有 7--8 万的记录.连 7,8 张表查询,需要 7--8 秒.我表示很差异.

当然了目前我所遇到过的最大连表只连三张,单表数据也不是很大.大概是一张几千,另一张几百万的样子.

不过很快就查出来了.
4164 次点击
所在节点    数据库
86 条回复
nilai
2017-03-06 17:07:43 +08:00
都让开都让开, 这是正文教务系统中的一句 SQL 如下:
select * from (select case when a.kcxzdm is null then '98' else a.kcxzdm end kcxzdm,a.kcxzmc,case when a.xfyq is null then 0 else to_number(a.xfyq) end xfyq ,b.xfh1,b.xfh2,to_number(to_number((case when a.xfyq is null then 0 else to_number(a.xfyq) end))-to_number(b.xfh1)) xfc from (select * from jxjhxfyqview where jxjhh=(select dqszj||zydm from xsjbxxb where xh='0407100522')) a left join (select kcxz,case when sum(xf1) is null then 0 else sum(xf1) end xfh1,case when sum(xf2) is null then 0 else sum(xf2) end xfh2 from (select kcdm,kcmc,kcxz,cj,xf,decode(floor(McjN/60),1,xf) xf1,decode(floor(McjN/60),0,xf) xf2 from (select a.*,b.xymc from (select a.*,b.kkbmdm from (select xn,xq,xkkh,xh,xm,kcmc,qzxs,xf,cj,zscj,bz,xgsj,xgs,cxbj,tzf,tzfjd,kcdm,pscj,qmcj,sycj,bkcj,cxcj,kcxz,tj,tjbz,cxxnxq,qzcj,kcgs,fxbj,jf,xsqr,sfkc,ysbkcj,McjN,McxcjN,zpZ,bkcjZ,cxcjZ,cxcj1Z,cxcj2Z,cxcj3Z,cxcj4Z,cxcj5Z,cxcj1,cxcj2,cxcj3,cxcj4,cxcj5,McjX,McxcjX,McjzPN,jycj,MjyN,MjycjN,MjybkcjN,MjycxcjN,MjycxbkcjN ,(case when (select x.kcdm from wjdszb x where x.kcdm=substr(a.xkkh,case when instr(a.xkkh,'-',1,3)<>0 then instr(a.xkkh,'-',1,3) else length(a.xkkh)+1 end+1,case when instr(a.xkkh,'-',1,4)<>0 then instr(a.xkkh,'-',1,4) else length(a.xkkh)+1 end-(case when instr(a.xkkh,'-',1,3)<>0 then instr(a.xkkh,'-',1,3) else length(a.xkkh)+1 end+1))) is null then (case when ((select x.mc from cjlrbzb x where (','||a.bz||',' like '%,'||x.mc||',%' or ','||a.cj||',' like '%,'||x.mc||',%') and x.sfwjd='是') is null) then to_char( ( (case when McxcjN>=60 then ( select max(to_number(jd)) from (select substr(cj,1,case when instr(cj,'-',1,1)<>0 then instr(cj,'-',1,1) else length(cj)+1 end-(1)) as qsd,substr(cj,case when instr(cj,'-',1,1)<>0 then instr(cj,'-',1,1) else length(cj)+1 end+1,case when instr(cj,'-',1,2)<>0 then instr(cj,'-',1,2) else length(cj)+1 end-(case when instr(cj,'-',1,1)<>0 then instr(cj,'-',1,1) else length(cj)+1 end+1)) as jsd,jd from bkcxjddzb where lb='重修' and cj like '%-%') a where to_number(qsd)<=McxcjN and to_number(jsd)>McxcjN ) when bkcjZ>=60 then ( select max(to_number(jd)) from (select substr(cj,1,case when instr(cj,'-',1,1)<>0 then instr(cj,'-',1,1) else length(cj)+1 end-(1)) as qsd,substr(cj,case when instr(cj,'-',1,1)<>0 then instr(cj,'-',1,1) else length(cj)+1 end+1,case when instr(cj,'-',1,2)<>0 then instr(cj,'-',1,2) else length(cj)+1 end-(case when instr(cj,'-',1,1)<>0 then instr(cj,'-',1,1) else length(cj)+1 end+1)) as jsd,jd from bkcxjddzb where lb='补考' and cj like '%-%') a where to_number(qsd)<=bkcjZ and to_number(jsd)>bkcjZ ) else to_number( ( select max(to_number(jd)) from (select substr(cj,1,case when instr(cj,'-',1,1)<>0 then instr(cj,'-',1,1) else length(cj)+1 end-(1)) as qsd,substr(cj,case when instr(cj,'-',1,1)<>0 then instr(cj,'-',1,1) else length(cj)+1 end+1,case when instr(cj,'-',1,10)<>0 then instr(cj,'-',1,10) else length(cj)+1 end-(case when instr(cj,'-',1,1)<>0 then instr(cj,'-',1,1) else length(cj)+1 end+1)) as jsd,jd from jddzb where cj like '%-%') a where to_number(qsd)<=ZpZ and to_number(jsd)>ZpZ )) end ) *(case when (select max(to_number(nvl(z.kcqzxs,1))) from kcxzdmb Z where a.kcxz=Z.kcxzmc) is null then 1 else (select max(to_number(nvl(z.kcqzxs,1))) from kcxzdmb Z where a.kcxz=Z.kcxzmc) end) )) else '' end) else '' end) jd ,case when McxcjX is not null then '*' else '' end dcxbj,cxdtcj,case when bkcj is not null then '*' else '' end bkcjbj from ( select a.*,case when McjN=zpZ then cj when McjN=bkcjZ then bkcj||'' when (McjN=cxcjZ and cxcj is not null) then cxcj||'' when (McjN=cxcj1Z and cxcj1 is not null) then cxcj1||'' when (McjN=cxcj2Z and cxcj2 is not null) then cxcj2||'' when (McjN=cxcj3Z and cxcj3 is not null) then cxcj3||'' when (McjN=cxcj4Z and cxcj4 is not null) then cxcj4||'' when (McjN=cxcj5Z and cxcj5 is not null) then cxcj5||'' end McjX, case when (McjN=cxcjZ and cxcj is not null) then cxcj||'' when (McjN=cxcj1Z and cxcj1 is not null) then cxcj1||'' when (McjN=cxcj2Z and cxcj2 is not null) then cxcj2||'' when (McjN=cxcj3Z and cxcj3 is not null) then cxcj3||'' when (McjN=cxcj4Z and cxcj4 is not null) then cxcj4||'' when (McjN=cxcj5Z and cxcj5 is not null) then cxcj5||'' end McxcjX from (select a.*, case when pscjN is null then 0 else to_number(pscjN) end pscjZ, case when qmcjN is null then 0 else to_number(qmcjN) end qmcjZ, case when sycjN is null then 0 else to_number(sycjN) end sycjZ, case when zpN is null then 0 else to_number(zpN) end zpZ, case when bkcjN is null then 0 else to_number(bkcjN) end bkcjZ, case when cxcjN is null then 0 else to_number(cxcjN) end cxcjZ, case when cxcj1N is null then 0 else to_number(cxcj1N) end cxcj1Z, case when cxcj2N is null then 0 else to_number(cxcj2N) end cxcj2Z, case when cxcj3N is null then 0 else to_number(cxcj3N) end cxcj3Z, case when cxcj4N is null then 0 else to_number(cxcj4N) end cxcj4Z, case when cxcj5N is null then 0 else to_number(cxcj5N) end cxcj5Z, case when bybkcjN is null then 0 else to_number(bybkcjN) end bybkcjZ, greatest(case when zpN is null then 0 else to_number(zpN) end,case when bkcjN is null then 0 else to_number(bkcjN) end,case when cxcjN is null then 0 else to_number(cxcjN) end, case when cxcj1N is null then 0 else to_number(cxcj1N) end,case when cxcj2N is null then 0 else to_number(cxcj2N) end,case when cxcj3N is null then 0 else to_number(cxcj3N) end ,case when cxcj4N is null then 0 else to_number(cxcj4N) end,case when cxcj5N is null then 0 else to_number(cxcj5N) end ) McjN, least(case when zpN is null then 0 else to_number(zpN) end,case when bkcjN is null then 0 else to_number(bkcjN) end,case when cxcjN is null then 0 else to_number(cxcjN) end, case when cxcj1N is null then 0 else to_number(cxcj1N) end,case when cxcj2N is null then 0 else to_number(cxcj2N) end,case when cxcj3N is null then 0 else to_number(cxcj3N) end ,case when cxcj4N is null then 0 else to_number(cxcj4N) end,case when cxcj5N is null then 0 else to_number(cxcj5N) end ) MincjN, greatest(to_number('0'||bkcjN),to_number('0'||zpN) ) McjzPN, greatest(case when cxcjN is null then 0 else to_number(cxcjN) end, case when cxcj1N is null then 0 else to_number(cxcj1N) end,case when cxcj2N is null then 0 else to_number(cxcj2N) end,case when cxcj3N is null then 0 else to_number(cxcj3N) end ,case when cxcj4N is null then 0 else to_number(cxcj4N) end,case when cxcj5N is null then 0 else to_number(cxcj5N) end ) McxcjN ,least(nvl(upper(jycj),'W'),nvl(upper(MjycxcjN),'W')) MjycjN ,least(nvl(upper(jybkcj),'W'),nvl(upper(MjycxbkcjN),'W')) MjybkcjN ,least(nvl(upper(jycj),'W'),nvl(upper(MjycxcjN),'W'),nvl(upper(jybkcj),'W'),nvl(upper(MjycxbkcjN),'W')) MjyN from (select cjb.*, case when (select dycj from cjdzb where cj=cjb.pscj) is null then cjb.pscj else (select to_char(dycj) from cjdzb where cj=cjb.pscj) end pscjN, case when (select dycj from cjdzb where cj=cjb.qmcj) is null then cjb.qmcj else (select to_char(dycj) from cjdzb where cj=cjb.qmcj) end qmcjN, case when (select dycj from cjdzb where cj=cjb.sycj) is null then cjb.sycj else (select to_char(dycj) from cjdzb where cj=cjb.sycj) end sycjN, case when (select dycj from cjdzb where cj=cjb.cj) is null then cjb.cj else (select to_char(dycj) from cjdzb where cj=cjb.cj) end zpN, case when (select dycj from cjdzb where cj=cjb.bkcj) is null then cjb.bkcj else (select to_char(dycj) from cjdzb where cj=cjb.bkcj) end bkcjN, case when (select dycj from cjdzb where cj=cjb.cxcj) is null then cjb.cxcj else (select to_char(dycj) from cjdzb where cj=cjb.cxcj) end cxcjN, case when (select dycj from cjdzb where cj=cjb.cxcj1) is null then cjb.cxcj1 else (select to_char(dycj) from cjdzb where cj=cjb.cxcj1) end cxcj1N, case when (select dycj from cjdzb where cj=cjb.cxcj2) is null then cjb.cxcj2 else (select to_char(dycj) from cjdzb where cj=cjb.cxcj2) end cxcj2N , case when (select dycj from cjdzb where cj=cjb.cxcj3) is null then cjb.cxcj3 else (select to_char(dycj) from cjdzb where cj=cjb.cxcj3) end cxcj3N, case when (select dycj from cjdzb where cj=cjb.cxcj4) is null then cjb.cxcj4 else (select to_char(dycj) from cjdzb where cj=cjb.cxcj4) end cxcj4N, case when (select dycj from cjdzb where cj=cjb.cxcj5) is null then cjb.cxcj5 else (select to_char(dycj) from cjdzb where cj=cjb.cxcj5) end cxcj5N, case when (select dycj from cjdzb where cj=cjb.bybkcj) is null then cjb.bybkcj else (select to_char(dycj) from cjdzb where cj=cjb.bybkcj) end bybkcjN from (select xn,xq,xkkh,xh,xm,kcmc,qzxs,xf,cj,zscj,bz,xgsj,xgs,cxbj,tzf,tzfjd,kcdm,pscj,qmcj,sycj,bkcj,cxcj,kcxz,tj,tjbz,cxxnxq,qzcj,kcgs,fxbj,jf,xsqr,jd,cxcj1,cxcj2,cxcj3,cxcj4,cxcj5,cxdtcj,jycj,jybkcj,MjycxcjN,MjycxbkcjN,bybkcj,sfkc,ysbkcj from cjb_3) cjb ) a) a ) a ) a,kcdmb b where a.xh='0407100522' and (sfkc is null or sfkc<>'否') and substr(a.xkkh,case when instr(a.xkkh,'-',1,3)<>0 then instr(a.xkkh,'-',1,3) else length(a.xkkh)+1 end+1,case when instr(a.xkkh,'-',1,4)<>0 then instr(a.xkkh,'-',1,4) else length(a.xkkh)+1 end-(case when instr(a.xkkh,'-',1,3)<>0 then instr(a.xkkh,'-',1,3) else length(a.xkkh)+1 end+1))=b.kcdm(+) order by a.xkkh) a,xydmb b where a.kkbmdm=b.xydm(+) order by xn,xq,kcxz)) group by kcxz) b on a.kcxzmc=b.kcxz union all select '99','合计',sum(xfyq),sum(xfh1),sum(xfh2),sum(xfc) from (select case when a.kcxzdm is null then '98' else a.kcxzdm end kcxzdm,a.kcxzmc,case when a.xfyq is null then 0 else to_number(a.xfyq) end xfyq ,b.xfh1,b.xfh2,to_number(to_number((case when a.xfyq is null then 0 else to_number(a.xfyq) end))-to_number(b.xfh1)) xfc from (select * from jxjhxfyqview where jxjhh=(select dqszj||zydm from xsjbxxb where xh='0407100522')) a left join (select kcxz,case when sum(xf1) is null then 0 else sum(xf1) end xfh1,case when sum(xf2) is null then 0 else sum(xf2) end xfh2 from (select kcdm,kcmc,kcxz,cj,xf,decode(floor(McjN/60),1,xf) xf1,decode(floor(McjN/60),0,xf) xf2 from (select a.*,b.xymc from (select a.*,b.kkbmdm from (select xn,xq,xkkh,xh,xm,kcmc,qzxs,xf,cj,zscj,bz,xgsj,xgs,cxbj,tzf,tzfjd,kcdm,pscj,qmcj,sycj,bkcj,cxcj,kcxz,tj,tjbz,cxxnxq,qzcj,kcgs
nilai
2017-03-06 17:10:30 +08:00
接楼上, 你以为这就完了,图样图森破, 我只帖了四分之一, V2EX 字数限制。
zhuchaowe
2017-03-06 17:30:51 +08:00
@nilai 卧槽 6 的飞起
nilai
2017-03-06 17:58:56 +08:00
@zhuchaowe 对了, 要看完整版,请移步 http://www.doc88.com/p-3367592493132.html
Z1076
2017-03-06 18:04:45 +08:00
@nilai 66666666
chaleaoch
2017-03-06 18:36:12 +08:00
@nilai 太牛逼了!!!

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/344855

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX