Oracle10g 中 NOT IN 和 NOT EXISTS 哪个更快?

发布于 2024-12-23 06:56:27 字数 581 浏览 2 评论 0 原文

有人可以解释一下 INEXISTSNOT IN 之间的区别吗 和不存在。 因为我读到 EXISTS 会比 IN 工作得更好,而 NOT EXISTS 会比 NOT IN 工作得更好。 我创建的查询如下。

delete from tt_left t
 where t.val = 0
   and t.text in (select t1.text
                      from tt_left t1
                     where t.text = t1.text
                       and t.resulttext = t1.resulttext
                       and t.val = 0
                       and t1.val = 1);

如何将其转换为EXISTS? 还有其他更好的方法吗?

Can someone explain the difference between IN and EXISTS and NOT IN
and NOT EXISTS.
Because I have read that EXISTS will work better than IN and NOT EXISTS will work better than NOT IN.
The query i created is follows..

delete from tt_left t
 where t.val = 0
   and t.text in (select t1.text
                      from tt_left t1
                     where t.text = t1.text
                       and t.resulttext = t1.resulttext
                       and t.val = 0
                       and t1.val = 1);

How to convert this to EXISTS?
Is there any other better method?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

開玄 2024-12-30 06:56:27

优化器会将 INEXISTS 视为相同(if IN 子句不是常量列表)
那是因为它是“半连接”。

同样,NOT INNOT EXISTS 通常将被视为相同。这是一种“反半连接”。例外情况是 NOT IN 子查询中有 NULL。这会导致 NOT IN 始终为 false

因此,性能方面没有差异,但 EXISTS/NOT EXISTS 将始终正确

请参阅“NOT IN 与 NOT EXISTS 与 LEFT JOIN / IS NULL:Oracle"

只要列表值声明为 NOT NULL,Oracle 的优化器就能够看到 NOT EXISTS、NOT IN 和 LEFT JOIN / IS NULL 在语义上是等效的。

它对所有三种方法使用相同的执行计划,并且它们在同一时间产生相同的结果。

在 Oracle 中,使用上述三种方法中的任何一种方法从一个表中选择另一个表中缺少的值都是安全的。

但是,如果不能保证这些值不为空,则应使用 LEFT JOIN / IS NULL 或 NOT EXISTS 而不是 NOT IN,因为后者会根据表达式中是否存在 NULL 值而产生不同的结果。子查询结果集。

IN 与 JOIN 与 EXISTS:Oracle 也有类似的结论

The optimiser will treat IN and EXISTS the same (if the IN clause is not a list of constants)
That's because it is a "semi-join"

Likewise, NOT IN and NOT EXISTS will be usually treated the same. This is an "anti-semi-join". The exception is where you have a NULL in the NOT IN subquery. This causes the NOT IN to always be false

So, performance wise there is no difference but EXISTS/NOT EXISTS will always be correct

See "NOT IN vs. NOT EXISTS vs. LEFT JOIN / IS NULL: Oracle"

Oracle‘s optimizer is able to see that NOT EXISTS, NOT IN and LEFT JOIN / IS NULL are semantically equivalent as long as the list values are declared as NOT NULL.

It uses same execution plan for all three methods, and they yield same results in same time.

In Oracle, it is safe to use any method of the three described above to select values from a table that are missing in another table.

However, if the values are not guaranteed to be NOT NULL, LEFT JOIN / IS NULL or NOT EXISTS should be used rather than NOT IN, since the latter will produce different results depending on whether or not there are NULL values in the subquery resultset.

And IN vs. JOIN vs. EXISTS: Oracle which has a similar conclusion

同展鸳鸯锦 2024-12-30 06:56:27

一些论坛和帖子说“NOT IN 和 NOT EXIST 使用相同的执行计划,并且它们在同一时间产生相同的结果。”

但根据我和我对行和鞋带的经验几十张桌子,
原因是:

当使用“NOT IN”时,查询执行嵌套全表扫描,
而对于“NOT EXISTS”,查询可以在子查询中使用索引。

类似的原因也适用于“IN”和“IN”。 “存在”...

谢谢:D

Some Forums & Posts say thats "NOT IN & NOT EXIST uses same execution plan, and they yield same results in same time."

But According to Me & my Experience with lacs of rows & dozens of tables,
And the REASON is:

When using “NOT IN”, the query performs nested full table scans,
whereas for “NOT EXISTS”, query can use an index within the sub-query.

Similar reason is for 'IN' & 'EXIST'...

Thankx :D

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文