oracle where条件顺序有影响吗,where条件顺序不同、性能不同示例探讨

where条件顺序不同、性能不同示例探讨

更新时间:2013年05月19日 15:57:34   作者:

where条件顺序不同,性能不同,这个建议在Oracle11G版本还合适吗,想提高性能的的朋友可以参考下哈

昨天在书上看到SQL语句优化时,where条件顺序不同,性能不同,这个建议在Oracle11G版本还合适吗?方式1优于方式2?

方式1:

select a.*

from students s,

class c

where

s.id = c.id

s.id = 'xxxxxxxx'

方式2:

select a.*

from students s,

class c

where

s.id = 'xxxxxxxx'

s.id = c.id

10g中测试结果证明是一样的。

Microsoft Windows [版本 5.2.3790]

(C) 版权所有 1985-2003 Microsoft Corp.

C:\Documents and Settings\Administrator>sqlplus / as sysdba

SQL*Plus: Release 10.2.0.1.0 - Production on 星期六 5月 11 17:48:55 2013

Copyright (c) 1982, 2005, Oracle. All rights reserved.

连接到:

Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production

With the Partitioning, OLAP and Data Mining options

SQL> alter system flush shared_pool;

系统已更改。

SQL> alter system flush buffer_cache;

系统已更改。

SQL> set autotrace on;

SQL> select *

2 from COUNTRIES c,

3 REGIONS r

4 where c.REGION_ID=r.REGION_ID and c.REGION_ID='4';

REGIONS r

*

第 3 行出现错误:

ORA-00942: 表或视图不存在

SQL> select *

2 from hr.COUNTRIES c,

3 hr. REGIONS r

4 where c.REGION_ID=r.REGION_ID and c.REGION_ID='4';

CO COUNTRY_NAME REGION_ID REGION_ID

-- ---------------------------------------- ---------- ----------

REGION_NAME

-------------------------

EG Egypt 4 4

Middle East and Africa

IL Israel 4 4

Middle East and Africa

KW Kuwait 4 4

Middle East and Africa

CO COUNTRY_NAME REGION_ID REGION_ID

-- ---------------------------------------- ---------- ----------

REGION_NAME

-------------------------

NG Nigeria 4 4

Middle East and Africa

ZM Zambia 4 4

Middle East and Africa

ZW Zimbabwe 4 4

Middle East and Africa

已选择6行。

执行计划

----------------------------------------------------------

Plan hash value: 4030513296

--------------------------------------------------------------------------------

----------------

| Id | Operation | Name | Rows | Bytes | Cost (%

CPU)| Time |

--------------------------------------------------------------------------------

----------------

| 0 | SELECT STATEMENT | | 6 | 168 | 2

(0)| 00:00:01 |

| 1 | NESTED LOOPS | | 6 | 168 | 2

(0)| 00:00:01 |

| 2 | TABLE ACCESS BY INDEX ROWID| REGIONS | 1 | 14 | 1

(0)| 00:00:01 |

|* 3 | INDEX UNIQUE SCAN | REG_ID_PK | 1 | | 0

(0)| 00:00:01 |

|* 4 | INDEX FULL SCAN | COUNTRY_C_ID_PK | 6 | 84 | 1

(0)| 00:00:01 |

--------------------------------------------------------------------------------

----------------

Predicate Information (identified by operation id):

---------------------------------------------------

3 - access("R"."REGION_ID"=4)

4 - filter("C"."REGION_ID"=4)

统计信息

----------------------------------------------------------

628 recursive calls

0 db block gets

127 consistent gets

20 physical reads

0 redo size

825 bytes sent via SQL*Net to client

385 bytes received via SQL*Net from client

2 SQL*Net roundtrips to/from client

13 sorts (memory)

0 sorts (disk)

6 rows processed

SQL>

#############

SQL> alter system flush shared_pool;

系统已更改。

SQL> alter system flush buffer_cache;

系统已更改。

select *

from hr.COUNTRIES c,

hr. REGIONS r

where

c.REGION_ID='4'

6 and c.REGION_ID=r.REGION_ID;

CO COUNTRY_NAME REGION_ID REGION_ID

-- ---------------------------------------- ---------- ----------

REGION_NAME

-------------------------

EG Egypt 4 4

Middle East and Africa

IL Israel 4 4

Middle East and Africa

KW Kuwait 4 4

Middle East and Africa

CO COUNTRY_NAME REGION_ID REGION_ID

-- ---------------------------------------- ---------- ----------

REGION_NAME

-------------------------

NG Nigeria 4 4

Middle East and Africa

ZM Zambia 4 4

Middle East and Africa

ZW Zimbabwe 4 4

Middle East and Africa

已选择6行。

执行计划

----------------------------------------------------------

Plan hash value: 4030513296

--------------------------------------------------------------------------------

----------------

| Id | Operation | Name | Rows | Bytes | Cost (%

CPU)| Time |

--------------------------------------------------------------------------------

----------------

| 0 | SELECT STATEMENT | | 6 | 168 | 2

(0)| 00:00:01 |

| 1 | NESTED LOOPS | | 6 | 168 | 2

(0)| 00:00:01 |

| 2 | TABLE ACCESS BY INDEX ROWID| REGIONS | 1 | 14 | 1

(0)| 00:00:01 |

|* 3 | INDEX UNIQUE SCAN | REG_ID_PK | 1 | | 0

(0)| 00:00:01 |

|* 4 | INDEX FULL SCAN | COUNTRY_C_ID_PK | 6 | 84 | 1

(0)| 00:00:01 |

--------------------------------------------------------------------------------

----------------

Predicate Information (identified by operation id):

---------------------------------------------------

3 - access("R"."REGION_ID"=4)

4 - filter("C"."REGION_ID"=4)

统计信息

----------------------------------------------------------

656 recursive calls

0 db block gets

131 consistent gets

22 physical reads

0 redo size

825 bytes sent via SQL*Net to client

385 bytes received via SQL*Net from client

2 SQL*Net roundtrips to/from client

13 sorts (memory)

0 sorts (disk)

6 rows processed

SQL>

相关文章

1a1b05c64693fbf380aa1344a7812747.png

如果你在附加SQL数据库,出现只读或失败的情况,来看下本文的解决方案吧。2010-03-03

4f55910a645b073bc4fc65dc10dc14bd.png

使用sql存储过程实现分页,在网上能找到好多种解决方案,但是如何用asp.net后台调用呢,通过本篇文章小编给大家详解asp.net中如何调用sql存储过程实现分页,有需要的朋友可以来参考下2015-08-08

0ea3c7666119d5615e582f823fb3fad6.png

需要管理sql server的朋友,需要掌握的,大家可以收藏下,方便以后使用2008-09-09

4f96a78db829b1556ff16de21e013c7a.png

MS-SQL Server 2005 其中三个版本的比较...2006-12-12

8cc1031babc6aff2319f1c6af8544aa0.png

Replace关键字主要是用来将字符串中的某个字符替换成别的字符,今天要为大家介绍下使用它来查询是否包含某个特定字符串,示例代码如下,感兴趣的朋友可以参考下2013-08-08

0c932a99bb7b6f23c937db507070cc7b.png

这篇文章主要介绍了SQL语句的并集UNION,交集JOIN(内连接,外连接),交叉连接(CROSS JOIN笛卡尔积),差集(NOT IN) ,需要的朋友可以参考下2014-07-07

cca732bf65a93ed2ec0ac80c638460fe.png

非常不错的SQL语句学习手册实例版...2007-03-03

2d9f31f2af7b675a3d153d2b7f1035a7.png

这篇文章主要介绍了SQL Server中利用正则表达式替换字符串的方法,非常不错,具有参考借鉴价值,需要的朋友可以参考下2017-03-03

b452cee8ec5cd9e58ab98eba17281e59.png

这篇文章主要介绍了sql查询表中根据某列排序的任意行,需要的朋友可以参考下2014-03-03

f4838ec7e2d4da28e0b57d4e852dadd4.png

MSSQL將首字母替換成大寫的实现语句,需要的朋友可以参考下。2009-09-09

最新评论

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值