實驗-資料分佈對執行計劃的影響.txt
D:\Documents and Settings\tian>sqlplus /nolog
SQL*Plus: Release 10.2.0.1.0 - Production on 星期日 5月 5 10:22:14 2013
Copyright (c) 1982, 2005, Oracle. All rights reserved.
SQL> conn scott/scott@test
已連線。
SQL> create table t1 as select trunc((rownum-1)/100) id,rpad(rownum,100) t_pad
2 from dba_Source
3 where rownum<10000;
Table created
SQL> create index t1_idx1 on t1(id);
Index created
SQL> exec dbms_stats.gather_table_stats(user,'t1',cascade=>true);
PL/SQL procedure successfully completed
SQL> drop table t2;
Table dropped
SQL> create table t2 as select mod(rownum,100) id,rpad(rownum,100) t_pad
2 from dba_Source
3 where rownum<10000;
Table created
SQL> create index t2_idx1 on t2(id);
Index created
SQL> exec dbms_stats.gather_table_stats(user,'t2',cascade=>true);
PL/SQL procedure successfully completed
以上分別通過trunc和mod兩個函式建立了兩張內容相同,但資料排列不一樣的表。
注意一下查詢中兩個表的前十行,ID排列不同。
SQL> select * from t1 where rownum<10;
ID T_PAD
---------- --------------------------------------------------------------------------------
0 1
0 2
0 3
0 4
0 5
0 6
0 7
0 8
0 9
9 rows selected
SQL> select * from t2 where rownum<10;
ID T_PAD
---------- --------------------------------------------------------------------------------
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
9 rows selected
分別在兩個表上進行查詢,選取第二次的結果:
SQL> select * from t1 where id=1;
已選擇100行。
執行計劃
----------------------------------------------------------
Plan hash value: 2623418078
---------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 100 | 10300 | 3 (0)| 00:00:01 |
| 1 | TABLE ACCESS BY INDEX ROWID| T1 | 100 | 10300 | 3 (0)| 00:00:01 |
|* 2 | INDEX RANGE SCAN | T1_IDX1 | 100 | | 1 (0)| 00:00:01 |
---------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("ID"=1)
統計資訊
----------------------------------------------------------
0 recursive calls
0 db block gets
19 consistent gets
0 physical reads
0 redo size
12167 bytes sent via SQL*Net to client
451 bytes received via SQL*Net from client
8 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
100 rows processed
SQL> select * from t2 where id=1;
已選擇100行。
執行計劃
----------------------------------------------------------
Plan hash value: 1513984157
--------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 100 | 10300 | 39 (3)| 00:00:01 |
|* 1 | TABLE ACCESS FULL| T2 | 100 | 10300 | 39 (3)| 00:00:01 |
--------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("ID"=1)
統計資訊
----------------------------------------------------------
0 recursive calls
0 db block gets
163 consistent gets
0 physical reads
0 redo size
11845 bytes sent via SQL*Net to client
451 bytes received via SQL*Net from client
8 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
100 rows processed
T1是順序排列的,使用索引方式查詢,成本比全表掃描要低;
T2是分散儲存的,使用全表掃描的效率比索引高。
以上實驗提示我們:資料的分佈情況在很大程度上也會影響執行計劃。
通常來說,根據執行查詢的頻率,將資料有序排列,使每次需要獲取的一批資料放在一起,能夠提高效率。
索引的聚簇因子:向優化器表明具有同樣索引值的資料行是不是存放在同一個或連續的一系列資料塊中。
以上兩個表的索引聚簇因子資訊為:
SQL> select t.table_name||'.'||i.index_name idx_name,
2 i.clustering_factor,t.blocks,t.num_rows
3 from user_indexes i,user_tables t
4 where i.table_name=t.table_name
5 and t.table_name in('T1','T2')
6 order by t.table_name,i.index_name;
IDX_NAME CLUSTERING_FACTOR BLOCKS NUM_ROWS
------------------------------------------------------------- ----------------- ---------- ----------
T1.T1_IDX1 152 164 9999
T2.T2_IDX1 9999 164 9999
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26451536/viewspace-760705/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- cluster factor對執行計劃的影響
- 不等號影響執行計劃的相關實驗
- 索引及排序對執行計劃的影響索引排序
- oracle cardinality對於執行計劃的影響Oracle
- not-null約束對執行計劃的影響Null
- Oracle優化案例-統計資訊對執行計劃的影響(十三)Oracle優化
- _complex_view_merging對執行計劃的影響View
- 執行緒數目對資料庫的影響執行緒資料庫
- CLUSTERING_FACTOR影響執行計劃
- 引數Optimizer_index_cost_adj 對執行計劃的影響Index
- 【CURSOR】Oracle繫結變數、執行計劃對遊標的影響Oracle變數
- 收集統計資訊中的no_invalidate選項對執行計劃的影響
- 實驗-shell執行資料庫命令.TXT資料庫
- 再說索引與Null值對於Hints及執行計劃的影響索引Null
- 驗證資料壓縮對DML的影響
- 並行查詢對於響應時間的影響實驗並行
- 按行業劃分的COVID-19疫情對B2B行業的影響【附原資料表】行業
- 表挪動儲存空間後,對之上的sql的執行計劃的影響的探究SQL
- 大資料對法律行業產生的影響大資料行業
- try catch 對程式碼執行的效能影響
- db_file_multiblock_read_count引數對block讀取和執行計劃的影響BloC
- Hint 組合執行步驟 - 分開執行, 互不影響
- 【PG執行計劃】Postgresql資料庫執行計劃統計資訊簡述SQL資料庫
- sqlprofile繫結執行計劃實驗測試SQL
- oracle實驗記錄 (predicate對cpu cost的影響)Oracle
- 在資料分佈嚴重不均的列上使用繫結變數容易錯過更好的執行計劃變數
- margin為負值對佈局的影響
- 執行計劃裡statistics資料的解釋
- 時區調整對job的執行時間的影響
- 資料庫執行計劃和資料存取方式資料庫
- 表資料的儲存對索引的影響索引
- CSS3實現動畫不會影響主執行緒,JS實現動畫會影響主執行緒CSSS3動畫執行緒JS
- 影響執行計劃之oracle sql baseline與sql profile之互動OracleSQL
- ORACLE資料庫檢視執行計劃Oracle資料庫
- 對一個執行計劃的疑問
- 磁碟排序對Oracle資料庫效能的影響排序Oracle資料庫
- Oracle資料庫關於SQL的執行計劃Oracle資料庫SQL
- 如何閱讀PG資料庫的執行計劃資料庫