应用中有这样一个查询:select * from document_history where document_id in (select document_id from document_reports where ...)
这里如果 in 中的子查询数据很多是不是会有性能问题?该如何改造?

解决方案 »

  1.   


    select * from document_history a 
    inner join document_reports b on a.document_id = b.document_id
    where b..... = ...(条件)
      

  2.   

    select * from document_history a where exists
    (select 1 from document_reports where document_id=a.document_id)
      

  3.   

    两个表的document_id需要索引优化
      

  4.   

    我写SQL都不用IN的..都用JOIN或EXISTS
      

  5.   

    最好用left join,inner join或者exists
    用IN查询速度慢