where條件順序不同、性能不同示例探討
更新時間:2013年05月19日 15:57:34 作者:
where條件順序不同,性能不同,這個建議在Oracle11G版本還合適嗎,想提高性能的的朋友可以參考下哈
昨天在書上看到SQL語句優(yōu)化時,where條件順序不同,性能不同,這個建議在Oracle11G版本還合適嗎?方式1優(yōu)于方式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中測試結(jié)果證明是一樣的。
Microsoft Windows [版本 5.2.3790]
(C) 版權(quán)所有 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;
系統(tǒng)已更改。
SQL> alter system flush buffer_cache;
系統(tǒng)已更改。
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 行出現(xiàn)錯誤:
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行。
執(zhí)行計劃
----------------------------------------------------------
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)
統(tǒng)計信息
----------------------------------------------------------
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;
系統(tǒng)已更改。
SQL> alter system flush buffer_cache;
系統(tǒng)已更改。
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行。
執(zhí)行計劃
----------------------------------------------------------
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)
統(tǒng)計信息
----------------------------------------------------------
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>
方式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中測試結(jié)果證明是一樣的。
Microsoft Windows [版本 5.2.3790]
(C) 版權(quán)所有 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;
系統(tǒng)已更改。
SQL> alter system flush buffer_cache;
系統(tǒng)已更改。
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 行出現(xiàn)錯誤:
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行。
執(zhí)行計劃
----------------------------------------------------------
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)
統(tǒng)計信息
----------------------------------------------------------
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;
系統(tǒng)已更改。
SQL> alter system flush buffer_cache;
系統(tǒng)已更改。
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行。
執(zhí)行計劃
----------------------------------------------------------
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)
統(tǒng)計信息
----------------------------------------------------------
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>
相關(guān)文章
SQL Server2019數(shù)據(jù)庫備份與還原腳本(批量備份)
本文主要介紹了SQL Server2019數(shù)據(jù)庫備份與還原腳本,數(shù)據(jù)庫可批量備份,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-11-11SQL查詢數(shù)據(jù)是否存在的實現(xiàn)示例
在后端開發(fā)中,經(jīng)常需要使用數(shù)據(jù)庫來存儲和管理數(shù)據(jù),一個常見的任務是檢查數(shù)據(jù)庫中是否存在數(shù)據(jù),本文主要介紹了SQL查詢數(shù)據(jù)是否存在,感興趣的可以了解一下2024-02-02SQL Server誤區(qū)30日談 第8天 有關(guān)對索引進行在線操作的誤區(qū)
在線索引操作會在操作開始時和操作結(jié)束時對資源上短暫的鎖。這有可能導致嚴重的阻塞問題2013-01-01SQL Server 復制需要有實際的服務器名稱才能連接到服務器
今天在做sql Server 2005的實驗的時候碰到的問題,問題描述很清楚,懷疑是我以前給計算機修改了名稱而導致的.可以用select @@servername和select serverproperty ('servername')對照一下,兩個的結(jié)果是否一樣2012-06-06SQLServer 參數(shù)化查詢經(jīng)驗分享
本篇文章將介紹參數(shù)化查詢。我將討論如果一個查詢可以被參數(shù)化,那么SQL Server優(yōu)化器怎樣嘗試將其參數(shù)化,以及你可以怎樣建立你自己的參數(shù)化查詢。2010-05-05SQLSERVER語句的執(zhí)行時間顯示的統(tǒng)計結(jié)果是什么意思
在SQL語句調(diào)優(yōu)的時候,大部分都會查看語句執(zhí)行時間,究竟SQLSERVER顯示出來的統(tǒng)計結(jié)果是什么意思,接下來為您一一解釋,感興趣的朋友可以了解下2013-01-01