Take MySQL as an example, what is the difference (principle and efficiency) between in and exists and between not in and not exists? What about using indexes?

take MySQL as an example, what is the difference (principle and efficiency) between in and exists and between not in and not exists? What about using indexes? Say that not in and < > will not use indexes, won"t you?


refer to this: https://codeshelper.com/a/11.


< H2 > in terms of efficiency: < / H2 >
  • 1) select * from T1 where exists (select 1 from T2 where T1.a=T2.a)
  • when the amount of T1 data is small and T2 data is very large, when T1 < T2, 1) the query efficiency is high.
  • 2) select * from T1 where T1.an in (select T2.a from T2)
  • T1 data is very large and T2 data is small, when T1 > T2, 2) the query efficiency is high.

in short, the general formula is: large appearance, large inner table with IN;, and EXISTS.

" details "

Menu