SQL性能优化

[复制链接]
查看11 | 回复9 | 2013-12-18 09:29:08 | 显示全部楼层 |阅读模式
昨天在书上看到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



回复

使用道具 举报

千问 | 2013-12-18 09:29:08 | 显示全部楼层
一样的吧
回复

使用道具 举报

千问 | 2013-12-18 09:29:08 | 显示全部楼层
where是一个筛选器,当有两个条件的时候,第一个条件的输出会作为第二个条件的输入,先筛选哪一个,会对后一个筛选条件有影响。
回复

使用道具 举报

千问 | 2013-12-18 09:29:08 | 显示全部楼层
一样的,优化器自己判断,
对于过滤条件有提示可以控制先后顺序
回复

使用道具 举报

千问 | 2013-12-18 09:29:08 | 显示全部楼层
看下执行计划不就一目了然了
回复

使用道具 举报

千问 | 2013-12-18 09:29:08 | 显示全部楼层
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 *
2fromCOUNTRIES c,
3REGIONS r
4where c.REGION_ID=r.REGION_ID and c.REGION_ID='4';
REGIONS r
*
第 3 行出现错误:
ORA-00942: 表或视图不存在

SQL> select *
2 from hr.COUNTRIES c,
3hr. REGIONS r
4 where c.REGION_ID=r.REGION_ID and c.REGION_ID='4';
CO COUNTRY_NAME
REGION_IDREGION_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_IDREGION_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)

统计信息
----------------------------------------------------------
628recursive calls

0db block gets
127consistent gets
20physical reads

0redo size
825bytes sent via SQL*Net to client
385bytes received via SQL*Net from client

2SQL*Net roundtrips to/from client
13sorts (memory)

0sorts (disk)

6rows processed
SQL>
回复

使用道具 举报

千问 | 2013-12-18 09:29:08 | 显示全部楼层
#############
第二次执行结果、:
SQL> alter system flush shared_pool;
系统已更改。
SQL> alter system flush buffer_cache;
系统已更改。
SQL> select *
2from hr.COUNTRIES c,
3hr. REGIONS r
4where
5c.REGION_ID='4'
6and c.REGION_ID=r.REGION_ID;
CO COUNTRY_NAME
REGION_IDREGION_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_IDREGION_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)

统计信息
----------------------------------------------------------
656recursive calls

0db block gets
131consistent gets
22physical reads

0redo size
825bytes sent via SQL*Net to client
385bytes received via SQL*Net from client

2SQL*Net roundtrips to/from client
13sorts (memory)

0sorts (disk)

6rows processed
SQL>




回复

使用道具 举报

千问 | 2013-12-18 09:29:08 | 显示全部楼层
10g中测试结果如上,两种方式是一样的。
回复

使用道具 举报

千问 | 2013-12-18 09:29:08 | 显示全部楼层
感谢Roy杰!!
回复

使用道具 举报

千问 | 2013-12-18 09:29:08 | 显示全部楼层
楼主你看的是很老的资料那是RBO的时代吧
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行