鍍金池/ 問答/數(shù)據(jù)庫/ 以MySQL為例,in和exists以及not in 和not exists有什

以MySQL為例,in和exists以及not in 和not exists有什么不同(原理和效率)呢?使用索引方面呢?

以MySQL為例,in和exists以及not in 和not exists有什么不同(原理和效率)呢?使用索引方面呢?說not in和<>不會使用索引,対嗎?

回答
編輯回答
囍槑

從效率來看:

  • 1) select * from T1 where exists(select 1 from T2 where T1.a=T2.a)
  • T1數(shù)據(jù)量小而T2數(shù)據(jù)量非常大時(shí),T1<<T2 時(shí),1) 的查詢效率高。
  • 2) select * from T1 where T1.a in (select T2.a from T2)
  • T1數(shù)據(jù)量非常大而T2數(shù)據(jù)量小時(shí),T1>>T2 時(shí),2) 的查詢效率高。

簡而言之,一般式:外表大,用IN;內(nèi)表大,用EXISTS。

詳情

2018年5月21日 18:15
編輯回答
瘋子范
2018年6月26日 21:36