create table T1(
ID   NUMBER not null,
NAME VARCHAR2(30) not null
);
create index D on T1 (name);
alter table T1 add constraint PK_T1_ID primary key (ID) ;
因为先建了索引(非unique),再加了主键,就算是用ID查询,也是使用 INDEX RANGE SCAN的。
下面先测试的就是索引为指定unique的情况
----------------------------------------------
测试逻辑读
orphean@ORCL> select * from t1 where id = '15238';
ID NAME
---------- ------------------------------
15238 /3fae2b85_BasicOptionPaneUI1
执行计划
----------------------------------------------------------
Plan hash value: 190799060
-----------------------------------------------------------------------------------------
| Id | Operation                   | Name      | Rows | Bytes | Cost (%CPU)| Time     |
-----------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT            |           |     1 |    28 |     2   (0)| 00:00:01 |
|   1 | TABLE ACCESS BY INDEX ROWID| T1        |     1 |    28 |     2   (0)| 00:00:01 |
|* 2 |   INDEX RANGE SCAN          | IDX_T1_ID |     1 |       |     1   (0)| 00:00:01 |
-----------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("ID"=15238)
统计信息
----------------------------------------------------------
0 recursive calls
0 db block gets
4 consistent gets
          0 physical reads
0 redo size
491 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
orphean@ORCL>
orphean@ORCL> select name from t1 where id = '15238';
NAME
------------------------------
/3fae2b85_BasicOptionPaneUI1
执行计划
----------------------------------------------------------
Plan hash value: 190799060
-----------------------------------------------------------------------------------------
| Id | Operation                   | Name      | Rows | Bytes | Cost (%CPU)| Time     |
-----------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT            |           |     1 |    28 |     2   (0)| 00:00:01 |
|   1 | TABLE ACCESS BY INDEX ROWID| T1        |     1 |    28 |     2   (0)| 00:00:01 |
|* 2 |   INDEX RANGE SCAN          | IDX_T1_ID |     1 |       |     1   (0)| 00:00:01 |
-----------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("ID"=15238)
统计信息
----------------------------------------------------------
0 recursive calls
0 db block gets
4 consistent gets
          0 physical reads
0 redo size
439 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
-- 可以看到查询通过索引获取,因为需要回表,索引逻辑读是4
orphean@ORCL>
orphean@ORCL> select id from t1 where id = '15238';
ID
----------
15238
执行计划
----------------------------------------------------------
Plan hash value: 3653646128
------------------------------------------------------------------------------
| Id | Operation        | Name      | Rows | Bytes | Cost (%CPU)| Time     |
------------------------------------------------------------------------------
|   0 | SELECT STATEMENT |           |     1 |     5 |     1   (0)| 00:00:01 |
|* 1 | INDEX RANGE SCAN| IDX_T1_ID |     1 |     5 |     1   (0)| 00:00:01 |
------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - access("ID"=15238)
统计信息
----------------------------------------------------------
0 recursive calls
0 db block gets
 3 consistent gets
          0 physical reads
0 redo size
404 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
orphean@ORCL>
orphean@ORCL> select count(*) from t1 where id = '15238';
COUNT(*)
----------
1
执行计划
----------------------------------------------------------
Plan hash value: 3689344262
-------------------------------------------------------------------------------
| Id | Operation         | Name      | Rows | Bytes | Cost (%CPU)| Time     |
-------------------------------------------------------------------------------
|   0 | SELECT STATEMENT |           |     1 |     5 |     1   (0)| 00:00:01 |
|   1 | SORT AGGREGATE   |           |     1 |     5 |            |          |
|* 2 |   INDEX RANGE SCAN| IDX_T1_ID |     1 |     5 |     1   (0)| 00:00:01 |
-------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("ID"=15238)
统计信息
----------------------------------------------------------
0 recursive calls
0 db block gets
2 consistent gets
          0 physical reads
0 redo size
408 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
################
主键索引unique指定
alter table T1
add constraint PK_T1_ID primary key (ID)
using index
-----------------------
orphean@ORCL> select * from t1 where id = '15238';
ID NAME
---------- ------------------------------
15238 /3fae2b85_BasicOptionPaneUI1
执行计划
----------------------------------------------------------
Plan hash value: 3736029472
----------------------------------------------------------------------------------------
| Id | Operation                   | Name     | Rows | Bytes | Cost (%CPU)| Time     |
----------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT            |          |     1 |    28 |     2   (0)| 00:00:01 |
|   1 | TABLE ACCESS BY INDEX ROWID| T1       |     1 |    28 |     2   (0)| 00:00:01 |
|* 2 |   INDEX UNIQUE SCAN         | PK_T1_ID |     1 |       |     1   (0)| 00:00:01 |
----------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("ID"=15238)
统计信息
----------------------------------------------------------
0 recursive calls
0 db block gets
3 consistent gets
0 physical reads
0 redo size
395 bytes sent via SQL*Net to client
374 bytes received via SQL*Net from client
1 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
orphean@ORCL>
orphean@ORCL> select name from t1 where id = '15237';
NAME
------------------------------
javax/swing/text/PasswordView
执行计划
----------------------------------------------------------
Plan hash value: 3736029472
----------------------------------------------------------------------------------------
| Id | Operation                   | Name     | Rows | Bytes | Cost (%CPU)| Time     |
----------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT            |          |     1 |    28 |     2   (0)| 00:00:01 |
|   1 | TABLE ACCESS BY INDEX ROWID| T1       |     1 |    28 |     2   (0)| 00:00:01 |
|* 2 |   INDEX UNIQUE SCAN         | PK_T1_ID |     1 |       |     1   (0)| 00:00:01 |
----------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("ID"=15237)
统计信息
----------------------------------------------------------
0 recursive calls
0 db block gets
3 consistent gets
0 physical reads
0 redo size
344 bytes sent via SQL*Net to client
374 bytes received via SQL*Net from client
1 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
orphean@ORCL>
orphean@ORCL> select id from t1 where id = '15237';
ID
----------
15237
执行计划
----------------------------------------------------------
Plan hash value: 2381392525
------------------------------------------------------------------------------
| Id | Operation         | Name     | Rows | Bytes | Cost (%CPU)| Time     |
------------------------------------------------------------------------------
|   0 | SELECT STATEMENT |          |     1 |     5 |     1   (0)| 00:00:01 |
|* 1 | INDEX UNIQUE SCAN| PK_T1_ID |     1 |     5 |     1   (0)| 00:00:01 |
------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - access("ID"=15237)
统计信息
----------------------------------------------------------
0 recursive calls
0 db block gets
2 consistent gets
0 physical reads
0 redo size
404 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
orphean@ORCL>
orphean@ORCL> select count(*) from t1 where id = '15237';
COUNT(*)
----------
1
执行计划
----------------------------------------------------------
Plan hash value: 3140672401
-------------------------------------------------------------------------------
| Id | Operation          | Name     | Rows | Bytes | Cost (%CPU)| Time     |
-------------------------------------------------------------------------------
|   0 | SELECT STATEMENT   |          |     1 |     5 |     1   (0)| 00:00:01 |
|   1 | SORT AGGREGATE    |          |     1 |     5 |            |          |
|* 2 |   INDEX UNIQUE SCAN| PK_T1_ID |     1 |     5 |     1   (0)| 00:00:01 |
-------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("ID"=15237)
统计信息
----------------------------------------------------------
0 recursive calls
0 db block gets
2 consistent gets
0 physical reads
0 redo size
408 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
--EOF
This work is licensed under a CC A-S 4.0 International License.