开发者社区> 问答> 正文

子查询太影响效率,有大神能帮忙看看么?可以怎么优化?

子查询太影响效率了,有大神能帮忙看看么?可以怎么优化?
SELECT
  dw.NAME AS dwname,
  dzz.NAME AS dzzname,
  t.*
FROM
  tbl_i_dzz dw
  LEFT JOIN
  (SELECT
    izbhd.tfid tfid,
    izbhd.hdsj,
    (YEAR(CURDATE()) - YEAR(izbhd.hdsj)) * 12 - MONTH(izbhd.hdsj) + MONTH(CURDATE()) AS result,
    izbhd.dzzid,    izbhd.hdyt,    izbhd.hddd,    izbhd.hdxxId,    izbhd.zcr,    izbhd.yddy,    izbhd.sddy,    izbhd.zw,
    izbhd.tplj,    izbhd.yplj,    izbhd.zttplj,    izbhd.sfpl,    izbhd.pls,    izbhd.dzs,    izbhd.scs,    izbhd.lll,    izbhd.yddyrs,
    izbhd.sddyrs,    izbhd.dwid,    izbhd.deleteFlag , izbhd.createDT
  FROM
    tbl_i_zbhd izbhd
  WHERE izbhd.tfid IN
    (SELECT
      MAX(tfid)
    FROM
      tbl_i_zbhd
    WHERE hdsj IN
      (SELECT
        MAX(hdsj)
      FROM
        tbl_i_zbhd
        WHERE DeleteFlag='0'
      GROUP BY
      <isEqual property="DW" compareValue="1">
          dwid
      </isEqual>
      <isEqual property="DW" compareValue="0">
      dzzid
      </isEqual>
      )   and DeleteFlag='0'
    GROUP BY
    <isEqual property="DW" compareValue="1">
          dwid
      </isEqual>
      <isEqual property="DW" compareValue="0">
      dzzid
      </isEqual>
    )
    AND DeleteFlag='0'
    ) t
  ON  dw.TFID = t.dwid AND t.deleteFlag = '0'
  LEFT JOIN
  tbl_i_dzz dzz
  ON t.dzzid = dzz.TFID
WHERE dw.DeleteFlag='0'
   AND getdzzlevel(dw.allpid,';')=2
<isNotEmpty prepend="and" property="dwid">
  dw.tfid=#dwid#
</isNotEmpty>
<isNotEmpty prepend="and" property="dwname">
  dw.NAME LIKE '%$dwname$%'
</isNotEmpty>
<isNotEmpty prepend="and" property="dzzname">
  dzz.NAME LIKE '%$dzzname$%'
</isNotEmpty>
<isNotEmpty prepend="and" property="zw">
  t.zw LIKE '%$zw$%'
</isNotEmpty>
<isNotEmpty prepend="Order By" property="order">
    $order$
</isNotEmpty>


展开
收起
lingo 2014-08-31 17:49:14 6625 0
1 条回答
写回答
取消 提交回答
  • Re子查询太影响效率,有大神能帮忙看看么?可以怎么优化?

    -------------------------

    Re子查询太影响效率,有大神能帮忙看看么?可以怎么优化?
    改成了 inner join 效率大概可以比你之前代码提升了五十倍
    看其他人是否还有更好的写法,给楼主参考
    2014-08-31 17:59:28
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
MySQL查询优化实战 立即下载
MaxCompute SQL计算成本调优以及优化方法 立即下载
低代码开发师(初级)实战教程 立即下载