如何改进选择不在火鸟上

时间:2015-07-23 20:08:17

标签: sql select firebird notin

select mid from aplicacao
where mid not in
(
select distinct mid from aplicacao
inner join prod_app on prod_app.mid=aplicacao.mid
where prod_app.coditem=1
)

我尝试在firebird上搜索解决方案以改进此查询,但不幸的是没有成功。请有人帮帮我吗?

1 个答案:

答案 0 :(得分:3)

IN(和NOT IN)性能问题的最常见解决方案是使用EXISTS(或NOT EXISTS)代替:

select mid from aplicacao
where not exists (
  select 1 from prod_app
  where prod_app.mid = aplicacao.mid and prod_app.coditem=1
)

另一个解决方案是使用LEFT JOIN并过滤右侧不存在:

select mid from aplicacao
left join prod_app
  on prod_app.mid = aplicacao.mid and prod_app.coditem=1
where prod.app.coditem is null

请注意,prod_app上的其他过滤条件(如prod_app.coditem=1)必须是加入条件的一部分,而不是where子句的一部分。