首页 > Linux操作系统 > Linux操作系统 > 6-exists的使用
6、exists的使用
===================
1、用表连接代替exists
实验之后才发现情况比想象的复杂
an1 20000数据
an2 320000数据,是an1数据重复插入的结果
NAME TYPE VALUE
------------------------------------ ----------- ---------
optimizer_mode string ALL_ROWS
SQL> select count(*) from an2 where exists (select an2.id from an1,an2 where an1.id=an2.id and an1.name=an2.name);
COUNT(*)
----------
320000
已用时间: 00: 00: 00.43
SQL> select count(*) from an2,an1 where an1.id=an2.id;
COUNT(*)
----------
320000
已用时间: 00: 00: 00.25
在基于成本的优化器中,用表连接代替exists有效
SQL> alter system set optimizer_mode='rule';
系统已更改。
已用时间: 00: 00: 00.09
SQL> select count(*) from an2 where exists (select an2.id from an1,an2 where an1
.id=an2.id and an1.name=an2.name);
COUNT(*)
----------
320000
已用时间: 00: 00: 00.26
SQL> select count(*) from an1,an2 where an1.id=an2.id and an1.name=an2.name;
COUNT(*)
----------
320000
已用时间: 00: 00: 00.64
基于规则的优化器,情况相反,用exists快些
-----------------------------
2、用exists代替distinct
SQL> select count(distinct an2.id) from an2;
COUNT(DISTINCTAN2.ID)
---------------------
20000
已用时间: 00: 00: 00.29
SQL> select count(an2.id) from an2 where exists(select an2.id from an2,an1 where
an2.id=an1.id);
COUNT(AN2.ID)
-------------
320000
已用时间: 00: 00: 00.15
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/13177610/viewspace-676342/ ,如需转载,请注明出处,否则将追究法律责任。