http://www.dewen.net.cn/q/15423

 

 

Mysql里不外乎就是 子查询 和 连接 两种方式.

设第一个表为table1, 第二个为table2, table1包含table2.

sql为:

//子查询
select table1.id from table1 
  where not exists 
    (select 1 from table2 
     where table1.id = table2.id
    );

//外连接
select table1.id from table1 
  left join table2
  on table1.id=table2.id
where table2.id is null;

 

 

高性能mysql里有类似的例子, 见 "When a correlated subquery is good" 一节. 它给出的数据, 外连接要快.

还有就是, 要最快, 那么最好只对 两张表的主键 做差集, 这样只过覆盖索引, 会快一些.

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-01-27
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-11-28
  • 2021-12-22
  • 2022-12-23
  • 2022-12-23
  • 2022-02-10
相关资源
相似解决方案