17.讀書筆記收穫不止oracle之 索引儲存列值
下面來看下索引的第二個特點,索引儲存列值及rowid的特性。
sql>create table t as select * from dba_objects;
create index idx1_object_id on t (object_id);
select count(*) from t;
count(*)
表的情況和索引的情況的差別在於表把整行的記錄依次放進block形成data的block,而索引是把所在列的記錄排序後依次放進block裡面形成index_block。在沒有索引的情況下,data block中可以統計出表記錄數,index block也可以的。
不過index block裡存放的值是表特定的索引列,容納空間要比存放整行也就是所有列的data block要少得多。用索引一定會高效。
sql>set autotrace on
sql> set linesize 1000
set timing on
select count(*) from t;
count(*)
elapsed: 00:00:00.26
execution plan
plan hash value: 2966233522
| id | operation | name | rows | cost (%cpu)| time |
| 0| select statement | | 1 | 429 (1)| 00:00:01 |
| 1| sort aggregate | | 1 | | |
| 2| table access full| t | 91717 | 429 (1)| 00:00:01 |
statistics
33 recursive calls
0 dbblock gets
1564 consistent gets
1539 physical reads
0 redosize
544 bytes sent via sql*net to client
551 bytes received via sql*net from client
2 sql*net roundtrips to/from client
3 sorts (memory)
0 sorts (disk)
1 rowsprocessed
發現還是使用了全表掃瞄,這是因為索引列有空的記錄,不能準確統計表記錄數。
繼續查詢:
sql> select count(*) from t where object_id is not null;
count(*)
elapsed: 00:00:00.05
execution plan
plan hash value: 1296839119
| id | operation | name | rows | bytes | cost (%cpu)| time |
| 0| select statement | | 1 | 5 | 57 (0)| 00:00:01 |
| 1| sort aggregate | | 1 | 5 | | |
|* 2| index fast full scan| idx1_object_id| 91717 | 447k| 57 (0)| 00:00:01 |
predicate information (identified byoperation id):
2- filter("object_id" is not null)
statistics
1 recursive calls
0 dbblock gets
211 consistent gets
203 physical reads
0 redosize
544 bytes sent via sql*net to client
551 bytes received via sql*net from client
2 sql*net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rowsprocessed
將此列為非空後,走的是索引。
也可以修改object_id列的屬性,修改為不允許為空
sql> alter table t modify object_id not null;
繼續查詢:
select count(*) from t ;
count(*)
elapsed: 00:00:00.01
execution plan
plan hash value: 1296839119
| id | operation | name | rows | cost (%cpu)| time |
| 0| select statement | | 1 | 57 (0)| 00:00:01 |
| 1| sort aggregate | | 1 | | |
| 2| index fast full scan| idx1_object_id | 91717| 57 (0)| 00:00:01 |
statistics
8 recursive calls
0 dbblock gets
224 consistent gets
0 physical reads
0 redosize
544 bytes sent via sql*net to client
551 bytes received via sql*net from client
2 sql*net roundtrips to/from client
2 sorts (memory)
0 sorts (disk)
1 rowsprocessed
也是走索引了。
此外還可以修改該列為主鍵也可以同樣實現走索引。
sql> drop table t purge;
sql> alter table t add constraintpk1_object_id primary key (object_id);
sql> set autotrace on
sql> set linesize 1000
sql> set timing on
sql> select count(*) from t;
count(*)
elapsed: 00:00:00.06
execution plan
plan hash value: 1604907147
| id | operation | name | rows | cost (%cpu)| time |
| 0| select statement | | 1 | 53 (0)| 00:00:01 |
| 1| sort aggregate | | 1 | | |
| 2| index fast full scan| pk1_object_id | 91717| 53 (0)|00:00:01 |
statistics
12 recursive calls
0 dbblock gets
213 consistent gets
191 physical reads
0 redosize
544 bytes sent via sql*net to client
551 bytes received via sql*net from client
2 sql*net roundtrips to/from client
2 sorts (memory)
0 sorts (disk)
1 rowsprocessed
在主鍵上建的索引,然後在使用count(*)之後也是使用走索引。
《收穫,不止oralce》讀書筆記(3)
資料庫 database 由若干表空間 tablespace 組成,表空間由若干段 segment 組成,段 segment 由若干區 extend 組成,區 extend 又是由oracle的最小單元塊 block 組成。extend是oracle資料庫分配空間的最小單位。segment對應著表 ...
《收穫,不止oralce》讀書筆記(6)
表連線 巢狀迴圈 nested loops join 參與連線的兩張表有驅動表和被驅動表之分。類似於巢狀的兩層for迴圈。訪問被驅動表的次數等於驅動表的返回記錄數。如果在被驅動表的連線條件列上加上索引,可以顯著提高查詢效率。適合返回少量記錄的場合,被驅動表最好有索引 雜湊連線 hash join 同...
1 讀書筆記收穫不止Oracle之開篇
1.讀書筆記收穫不止oracle之開篇 偶然看到梁敬彬和梁敬弘合著的 收穫,不止oracle 心中滿懷欣喜,從題目上看不光是講oracle知識,應該會參雜一些技術之外或技術之上的東西。而且也有很多大師們的推薦,既然如此覺得可以好好學習一下,吸取其中精華 有很多oracle資料庫方面的技術大師們 馮培...