Oracle主鍵與複合主鍵的效能分析
Oracle主鍵與複合主鍵的效能分析
總結:
1、主鍵和複合主鍵,查詢效能相同(索引高度相同,恰當的運用索引)。
2、 主鍵和複合主鍵,(update,insert)效能不同 (因為複合主鍵會用更多的塊來建立索引,所以update,insert效能低)
實驗思路:
1、 建立實驗表,及主鍵,聯合2個主鍵,聯合3個主鍵
2、 檢視索引的結構
3、檢視條件相同的,執行計劃(來確定主鍵和複合主鍵的效率)
一、 建立實驗表;test1為單主鍵為1個column,test2為聯合主鍵2個columns,test3為聯合主鍵3個columns
SQL> create table test1(a number,b number,c number,primary key(a));
Table created.
SQL> create table test2(a number,b number,c number,primary key(a,b));
Table created.
SQL> create table test3(a number,b number,c number,primary key(a,b,c));
Table created.
二、 檢視索引的結構
1、先檢視一下建立的表對應的索引
SQL> select index_name,table_name from user_indexes;
INDEX_NAME TABLE_NAME
------------------------------ ------------------------------
SYS_C005198 TEST1
SYS_C005199 TEST2
SYS_C005200 TEST3
2、寫個儲存過程來給實驗表插入資料
begin
for i in 1..10000 loop
insert into test1 values(i,i+1,i+2);
commit;
end loop;
end;
Test1
SQL> analyze index SYS_C005198 validate structure;
Index analyzed.
SQL> select HEIGHT,BLOCKS,BR_BLKS,LF_BLKS,LF_ROWS,DEL_LF_ROWS from index_stats ;
HEIGHT BLOCKS BR_BLKS LF_BLKS LF_ROWS DEL_LF_ROWS
---------- ---------- ---------- ---------- ---------- -----------
2 24 1 18 10000 0
Test2
SQL> analyze index SYS_C005199 validate structure;
Index analyzed.
SQL> select HEIGHT,BLOCKS,BR_BLKS,LF_BLKS,LF_ROWS,DEL_LF_ROWS from index_stats ;
HEIGHT BLOCKS BR_BLKS LF_BLKS LF_ROWS DEL_LF_ROWS
---------- ---------- ---------- ---------- ---------- -----------
2 32 1 23 10000 0
Test3
SQL> analyze index SYS_C005200 validate structure;
Index analyzed.
SQL> select HEIGHT,BLOCKS,BR_BLKS,LF_BLKS,LF_ROWS,DEL_LF_ROWS from index_stats ;
HEIGHT BLOCKS BR_BLKS LF_BLKS LF_ROWS DEL_LF_ROWS
---------- ---------- ---------- ---------- ---------- -----------
2 40 1 28 10000 0
總結:根據B-TREE索引的結構特點。說明主鍵和聯合主鍵, 同樣的資料聯合主鍵需要更多的資源來維護索引。(聯合主鍵索引因為用了更多的塊,所以update,insert 會比主鍵索引慢一些。至於查詢下面研究)
三、 檢視相同情況下,主鍵的效率。
1、 語句都讓其走INDEX UNIQUE SCAN ,看看效率:
Test1
SQL> select a from test1 where a=5555;
A
----------
5555
Execution Plan
----------------------------------------------------------
Plan hash value: 2716871853
--------------------------------------------------------------------------------
-
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time
|
--------------------------------------------------------------------------------
-
| 0 | SELECT STATEMENT | | 1 | 13 | 1 (0)| 00:00:01
|
|* 1 | INDEX UNIQUE SCAN| SYS_C005198 | 1 | 13 | 1 (0)| 00:00:01
|
--------------------------------------------------------------------------------
-
Predicate Information (identified by operation id):
---------------------------------------------------
1 - access("A"=5555)
Statistics
----------------------------------------------------------
1 recursive calls
0 db block gets
2 consistent gets
0 physical reads
0 redo size
405 bytes sent via SQL*Net to client
385 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
Test2
SQL> select a,b from test2 where a=5555 and b=5556;
A B
---------- ----------
5555 5556
Execution Plan
----------------------------------------------------------
Plan hash value: 3210951477
--------------------------------------------------------------------------------
-
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time
|
--------------------------------------------------------------------------------
-
| 0 | SELECT STATEMENT | | 1 | 26 | 1 (0)| 00:00:01
|
|* 1 | INDEX UNIQUE SCAN| SYS_C005199 | 1 | 26 | 1 (0)| 00:00:01
|
--------------------------------------------------------------------------------
-
Predicate Information (identified by operation id):
---------------------------------------------------
1 - access("A"=5555 AND "B"=5556)
Statistics
----------------------------------------------------------
1 recursive calls
0 db block gets
2 consistent gets
0 physical reads
0 redo size
460 bytes sent via SQL*Net to client
385 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
Test3
SQL> select a,b,c from test3 where a=5555 and b=5556 and c=5557;
A B C
---------- ---------- ----------
5555 5556 5557
Execution Plan
----------------------------------------------------------
Plan hash value: 1852305570
--------------------------------------------------------------------------------
-
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time
|
--------------------------------------------------------------------------------
-
| 0 | SELECT STATEMENT | | 1 | 39 | 1 (0)| 00:00:01
|
|* 1 | INDEX UNIQUE SCAN| SYS_C005200 | 1 | 39 | 1 (0)| 00:00:01
|
--------------------------------------------------------------------------------
-
Predicate Information (identified by operation id):
---------------------------------------------------
1 - access("A"=5555 AND "B"=5556 AND "C"=5557)
Statistics
----------------------------------------------------------
1 recursive calls
0 db block gets
2 consistent gets
0 physical reads
0 redo size
515 bytes sent via SQL*Net to client
385 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
分析:通過執行SQL走INDEX UNIQUE SCAN索引的情況,分析執行計劃得到的結果是主鍵和聯合主鍵效能相同:
(我們關注的:
1 recursive calls
0 db block gets
2 consistent gets
0 physical reads
0 redo size
消耗一致和COST消耗一致。)
總結:主鍵和聯合主鍵,應用B-tree索引的情況下,如果我們的索引高度相同,且正確的應用索引。這樣的情況下我們查詢效能是相同的。
歡迎大家給與糾正錯誤,共同提升!
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26442936/viewspace-734960/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 主鍵、自增主鍵、主鍵索引、唯一索引概念區別與效能區別索引
- 主鍵與主鍵索引的關係索引
- oracle 聯合主鍵學習Oracle
- Oracle主鍵Oracle
- hibernate複合主鍵查詢問題
- 在已存在的表結構上新增主鍵、外來鍵、聯合主鍵、聯合索引的例子索引
- Oracle主鍵、唯一鍵與唯一索引的區別Oracle索引
- 主鍵為聯合主鍵時,索引B+樹結構索引
- ORACLE批量刪除無主鍵重複資料Oracle
- Oracle並行新增主鍵Oracle並行
- 注意:Oracle中的聯合主鍵查詢問題(轉)Oracle
- Oracle根據主鍵查詢外來鍵Oracle
- [精]mysql聯合主鍵應用MySql
- oracle新增主鍵的四種方法:Oracle
- Oracle 建立主鍵自增表Oracle
- myBatis插入oracle獲取主鍵MyBatisOracle
- ORACLE設定遞增主鍵Oracle
- MySQL 缺少主鍵的表的效能下降的原因MySql
- Oracle刪除主鍵保留索引的方法Oracle索引
- 修改主鍵的SQLSQL
- MySQL主鍵的理解MySql
- MySQL的主從複製與MySQL的主主複製MySql
- oracle增加主鍵也不會了Oracle
- mybatis oracle資料庫批次插入資料,忽略主鍵重複MyBatisOracle資料庫
- MySQL主從複製與主主複製MySql
- java主鍵生成Java
- 主鍵分類
- 父表修改與外來鍵的關係(主鍵DML與外來鍵的關係)
- Oracle主鍵選擇對插入的影響Oracle
- SQL的主鍵和外來鍵約束SQL
- 新的主鍵和外來鍵的語法
- 分散式主鍵生成分散式
- postgresql自增主鍵SQL
- Hibernate主鍵策略
- MySQL 處理插入過程中的主鍵唯一鍵重複值辦法MySql
- indexedDB 索引與primarykey主鍵區別Index索引
- 關鍵詞與主題分面公式公式
- 資料表設計之主鍵自增、UUID或聯合主鍵UI