Oracle使用子查詢,和左串連查詢同樣結果效能差距。

來源:互聯網
上載者:User

這條SQL,執行,查詢所有員工當月的考勤記錄匯總,展示全部資料耗時2.3秒

String sql = "select userid,orgid," + 子查詢,效能太慢,不適用
"(select username from t_acl_userinfo where userid=t.userid) username," +
"(select count(state) from t_chk_manage where userid=t.userid and state='01'"+sqlappend+") state01 ," +
"(select count(state) from t_chk_manage where userid=t.userid and state='02'"+sqlappend+") state02 ," +
"(select count(state) from t_chk_manage where userid=t.userid and state='03'"+sqlappend+") state03 ," +
"(select count(state) from t_chk_manage where userid=t.userid and state='04'"+sqlappend+") state04 ," +
"(select count(state) from t_chk_manage where userid=t.userid and state='05'"+sqlappend+") state05 ," +
"(select count(state) from t_chk_manage where userid=t.userid and state='06'"+sqlappend+") state06 ," +
"(select count(state) from t_chk_manage where userid=t.userid and state='07'"+sqlappend+") state07 ," +
"(select count(state) from t_chk_manage where userid=t.userid and state='08'"+sqlappend+") state08 ," +
"(select count(state) from t_chk_manage where userid=t.userid and state='09'"+sqlappend+") state09 " +
"from t_chk_manage t group by userid,orgid HAVING 1=1";

//使用左串連,先查出基本資料,再串連, Oracle 耗時0.2秒(速度提升將近10倍)

String sql = "select s.* , userInfo.username as username , state1.state as state01 , state2.state as state02 , state3.state as state03 , state4.state as state04 , " +
"state5.state as state05 , state6.state as state06 , state7.state as state07 , state8.state as state08 , state9.state as state09 " +
"from (select userid,orgid from t_chk_manage t where 1=1 "+dataSearch+"  group by userid , orgid HAVING 1=1 "+search+") s " +
"left join (select username,userid from t_acl_userinfo)userInfo on userInfo.userid = s.userid "+
"left join (select userid,count(state) as state from t_chk_manage where state = '01'  "+dataSearch+" group by userid)state1 on state1.userid = s.userid " +
"left join (select userid,count(state) as state from t_chk_manage where state = '02'  "+dataSearch+" group by userid)state2 on state2.userid = s.userid " +
"left join (select userid,count(state) as state from t_chk_manage where state = '03'  "+dataSearch+" group by userid)state3 on state3.userid = s.userid " +
"left join (select userid,count(state) as state from t_chk_manage where state = '04'  "+dataSearch+" group by userid)state4 on state4.userid = s.userid " +
"left join (select userid,count(state) as state from t_chk_manage where state = '05'  "+dataSearch+" group by userid)state5 on state5.userid = s.userid " +
"left join (select userid,count(state) as state from t_chk_manage where state = '06'  "+dataSearch+" group by userid)state6 on state6.userid = s.userid " +
"left join (select userid,count(state) as state from t_chk_manage where state = '07'  "+dataSearch+" group by userid)state7 on state7.userid = s.userid " +
"left join (select userid,count(state) as state from t_chk_manage where state = '08'  "+dataSearch+" group by userid)state8 on state8.userid = s.userid " +
"left join (select userid,count(state) as state from t_chk_manage where state = '09'  "+dataSearch+" group by userid)state9 on state9.userid = s.userid";

//這條sql查詢當月員工考勤明細記錄,耗時0.4秒,(原本使用子查詢需要7秒,提升20倍)

select day0.*,username,"+dayAppend+" from (select userid,orgid,substr(t.create_time,0,7) from t_chk_manage t where substr(t.create_time,0,7)='"+daystart.substring(0, 7)+"' " +
"group by userid,orgid,substr(t.create_time,0,7) HAVING 1=1 "+search+")day0 " +
"left join (select userid,username from t_acl_userinfo)username on username.userid = day0.userid " +
""+sqlAppend

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.