Mysql not in, not exists 사용시 쿼리속도가 느릴때.

  • systemddc
  • Mysql
  • 14063
  • 0


Mysql not in, not exists 사용시 쿼리 속도가 느릴때.


특정 항목을 제외 시키기 위해 not in, not exists를 이용합니다.


제가 전문가는 아니지만.


단건 몇개의 경우 not in으로 처리하고 다건일 경우에는 not exists를 사용합니다.


속도가 너무 느릴경우엔.


left join을 이용하여 붙이고 null인 항목을 가져오면


제외된 항목을 가져옵니다.

예)


  1. -- not exists 조회
  2. explain extended
  3. select
  4. count(a.user_id) as cnt,
  5. c.chnl_id,
  6. c.use_intt_id
  7. from user_ldgr a
  8. left join use_intt_per_user c
  9. on a.user_id = c.user_id
  10. where a.ATHZ_DT < '20160101'
  11. and not exists
  12. (
  13. select
  14. b.user_id
  15. from lgn_prhs b
  16. where b.lgn_dt between '20150101' and '20151231'
  17. and b.user_id = a.user_id
  18. group by b.user_id
  19. )
  20. group by chnl_id
  21. ;


  1. -- left join 조회
  2. explain
  3. select
  4. count(a.user_id) as cnt,
  5. c.chnl_id,
  6. c.use_intt_id
  7. from user_ldgr a
  8. left join use_intt_per_user c
  9. on a.user_id = c.user_id
  10. left join
  11. (
  12. select
  13. b.user_id
  14. from lgn_prhs b
  15. where b.lgn_dt between '20150101' and '20151231'
  16. group by b.user_id
  17. ) d
  18. on d.user_id = a.user_id
  19. where a.ATHZ_DT < '20160101'
  20. and d.user_id is null
  21. group by c.chnl_id
  22. ;

^^