不存在和减去

时间:2018-03-15 14:57:26

标签: sql plsql oracle11g oracle10g plsqldeveloper

有人能告诉我SQL中NOT EXIST和MINUS运算符之间的差异吗?我何时可以使用?哪一个可以提供更好的性能?

1 个答案:

答案 0 :(得分:5)

仅当MINUS关于同一列时才使用select dept_id from employees where salary > 1000 minus select dept_id from employees where salary < 500; 。这可以导致非常易读的查询:

NOT EXISTS

MINUS更加灵活,但对于可以使用select dept_id from departments d where exists (select * from employees e where e.dept_id = d.dept_id and e.salary > 1000) and not exists (select * from employees e where e.dept_id = d.dept_id and e.salary < 500); 轻松表达的查询,查询的可读性可能会降低:

MINUS

至于速度,应该没有多大区别。别担心。将您的查询编写为尽可能可读,并且只考虑在遇到性能问题时重新编写它们。 (但这些通常更多地是关于适当的索引而不是关于如何编写查询的常见问题.Oracle善于理解查询,甚至可能将NOT EXISTS查询重写为npm run build查询或反之亦然在执行之前。)