Oracle SQL:执行过滤的最有效方法

时间:2015-02-16 11:39:10

标签: sql oracle filter inner-join where

当我开始在大表上编写SQL查询时,我想知道哪一个是最有效的。对我来说,他们看起来都一样。寻找SQL如何分解和执行这些查询的解释

选项1:

select a.*
from table_a a
where a.column in (select filter from table_b)

选项2:

select a.*
from table_a a, table_b b
where a.column=b.filter

选项3:

select a.*
from table_a a
inner join table_b b
on a.column=b.filter

1 个答案:

答案 0 :(得分:3)

在最新版本的Oracle中,优化器非常智能,可以完成其工作。所以它不重要,您的两个查询都将在内部进行优化,以便有效地完成任务。优化工具可能会执行查询重写并选择高效执行计划

让我们通过EMPDEPT表的一个小例子来理解这一点。

查询1:

SQL> explain plan for
  2  select a.*
  3  from emp a
  4  WHERE A.deptno IN (SELECT deptno FROM dept);

Explained.

SQL>
SQL> SELECT * FROM TABLE(dbms_xplan.display);

PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------
Plan hash value: 3956160932

--------------------------------------------------------------------------
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      |    14 |   546 |     3   (0)| 00:00:01 |
|*  1 |  TABLE ACCESS FULL| EMP  |    14 |   546 |     3   (0)| 00:00:01 |
--------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------

   1 - filter("A"."DEPTNO" IS NOT NULL)

13 rows selected.

SQL>

查询2:

SQL> explain plan for
  2  SELECT A.*
  3  FROM emp A, dept b
  4  where a.deptno=b.deptno;

Explained.

SQL>
SQL> SELECT * FROM TABLE(dbms_xplan.display);

PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------
Plan hash value: 3956160932

--------------------------------------------------------------------------
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      |    14 |   546 |     3   (0)| 00:00:01 |
|*  1 |  TABLE ACCESS FULL| EMP  |    14 |   546 |     3   (0)| 00:00:01 |
--------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------

   1 - filter("A"."DEPTNO" IS NOT NULL)

13 rows selected.

SQL>

查询3:

SQL> explain plan for
  2  select a.*
  3  from emp a
  4  INNER JOIN dept b
  5  ON A.deptno=b.deptno;

Explained.

SQL>
SQL> SELECT * FROM TABLE(dbms_xplan.display);

PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------
Plan hash value: 3956160932

--------------------------------------------------------------------------
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      |    14 |   546 |     3   (0)| 00:00:01 |
|*  1 |  TABLE ACCESS FULL| EMP  |    14 |   546 |     3   (0)| 00:00:01 |
--------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------

   1 - filter("A"."DEPTNO" IS NOT NULL)

13 rows selected.

SQL>

因此,无论您以不同方式编写查询的方式如何,优化程序都会选择最有效的执行计划。除非您强制优化器使用提示等选择不同的计划。您将始终看到使用相同的计划执行。