非唯一索引不為人所知的一個細微特徵
如果表包含非唯一索引,在對錶進行批量delete+insert的時候,在delete完成後,必須先commit,再insert,或者是session在完成非唯一索引建立後,進行commit,否則非唯一索引需要重建。唯一索引沒有以上特性。
實驗一:非唯一索引建立後未commit (delete+insert)
SQL>create table test1 (id number, name varchar2(20));
SQL>insert into test1 select rownum, 'TEST1'
from dual connect by level <=1000000;
SQL>commit;
SQL>create index test1_idx on test1(id);
SQL>analyze index test1_idx validate structure;
SQL>select blocks, lf_rows, lf_blks, del_lf_rows from index_stats;
BLOCKS LF_ROWS LF_BLKS DEL_LF_ROWS
-------------- ---------------- ------------------ -------------------
2304 1000000 2226 0
將表資料刪除,然後重新insert新的資料
SQL>truncate table test1 (或者 delete test1)
SQL>insert into test1 select rownum, 'LongTop'
from dual connect by level <= 1000000;
SQL>commit;
SQL>analyze index test1_idx validate structure;
SQL>select blocks, lf_blks, del_lf_rows from index_stats;
BLOCKS LF_BLKS DEL_LF_ROWS
-------------------- ---------- -----------------
4736 4520 1000000
索引的儲存空間增加一倍,索引刪除空間沒有被重用,需要對索引進行重建。
實驗二:非唯一索引 建立後未commit(delete+commit+insert)
SQL>create table test1 (id number, name varchar2(20));
SQL>insert into test1 select rownum, 'TEST1'
from dual connect by level <=1000000;
SQL>commit;
SQL>create index test1_idx on test1(id);
SQL>analyze index test1_idx validate structure;
SQL>select blocks, lf_rows, lf_blks, del_lf_rows from index_stats;
BLOCKS LF_ROWS LF_BLKS DEL_LF_ROWS
-------------- ---------------- ------------------ -------------------
2304 1000000 2226 0
將表資料刪除,然後commit,再重新insert新的資料
SQL> truncate table test1
SQL>commit;
SQL>insert into test1 select rownum, 'LongTop'
from dual connect by level <= 1000000;
SQL>commit;
SQL>analyze index test1_idx validate structure;
SQL>select blocks, lf_blks, del_lf_rows from index_stats;
BLOCKS LF_BLKS DEL_LF_ROWS
-------------- ---------------- ------------------ -------------------
2048 1999 0
發現blocks的數量比原先的更少。
實驗三、非唯一索引建立後commit (delete+insert)
SQL>create table test1 (id number, name varchar2(20));
SQL>insert into test1 select rownum, 'TEST1'
from dual connect by level <=1000000;
SQL>commit;
SQL>create index test1_idx on test1(id);
SQL>commit;
SQL>analyze index test1_idx validate structure;
SQL>select blocks, lf_rows, lf_blks, del_lf_rows from index_stats;
BLOCKS LF_ROWS LF_BLKS DEL_LF_ROWS
-------------- ---------------- ------------------ -------------------
2304 1000000 2226 0
將表資料刪除,然後重新insert新的資料
SQL>truncate table test1 (或者 delete test1)
SQL>insert into test1 select rownum, 'LongTop'
from dual connect by level <= 1000000;
SQL>commit;
SQL>analyze index test1_idx validate structure;
SQL>select blocks, lf_blks, del_lf_rows from index_stats;
BLOCKS LF_BLKS DEL_LF_ROWS
-------------------- ---------- -----------------
2048 1999 0
實驗四、唯一索引(delete+insert)
SQL>create table test2 (id number, name varchar2(20));
SQL>insert into test2 select rownum, 'TEST2'
from dual connect by level <=1000000;
SQL>commit;
SQL>create unique index test2_idx on test2(id);
SQL>analyze index test2_idx validate structure;
SQL>select blocks, lf_rows, lf_blks, del_lf_rows from index_stats;
BLOCKS LF_ROWS LF_BLKS DEL_LF_ROWS
-------------- ---------------- ------------------ -------------------
2176 1000000 2088 0
將表資料刪除,然後重新insert新的資料
SQL> delete test2;
SQL>insert into test2 select rownum, 'LongTop'
from dual connect by level <= 1000000;
SQL>commit;
SQL>analyze index test2_idx validate structure;
SQL>select blocks, lf_blks, del_lf_rows from index_stats;
BLOCKS LF_BLKS DEL_LF_ROWS
-------------------- ---------- -----------------
2176 2088 0
實驗一:非唯一索引建立後未commit (delete+insert)
SQL>create table test1 (id number, name varchar2(20));
SQL>insert into test1 select rownum, 'TEST1'
from dual connect by level <=1000000;
SQL>commit;
SQL>create index test1_idx on test1(id);
SQL>analyze index test1_idx validate structure;
SQL>select blocks, lf_rows, lf_blks, del_lf_rows from index_stats;
BLOCKS LF_ROWS LF_BLKS DEL_LF_ROWS
-------------- ---------------- ------------------ -------------------
2304 1000000 2226 0
將表資料刪除,然後重新insert新的資料
SQL>truncate table test1 (或者 delete test1)
SQL>insert into test1 select rownum, 'LongTop'
from dual connect by level <= 1000000;
SQL>commit;
SQL>analyze index test1_idx validate structure;
SQL>select blocks, lf_blks, del_lf_rows from index_stats;
BLOCKS LF_BLKS DEL_LF_ROWS
-------------------- ---------- -----------------
4736 4520 1000000
索引的儲存空間增加一倍,索引刪除空間沒有被重用,需要對索引進行重建。
實驗二:非唯一索引 建立後未commit(delete+commit+insert)
SQL>create table test1 (id number, name varchar2(20));
SQL>insert into test1 select rownum, 'TEST1'
from dual connect by level <=1000000;
SQL>commit;
SQL>create index test1_idx on test1(id);
SQL>analyze index test1_idx validate structure;
SQL>select blocks, lf_rows, lf_blks, del_lf_rows from index_stats;
BLOCKS LF_ROWS LF_BLKS DEL_LF_ROWS
-------------- ---------------- ------------------ -------------------
2304 1000000 2226 0
將表資料刪除,然後commit,再重新insert新的資料
SQL> truncate table test1
SQL>commit;
SQL>insert into test1 select rownum, 'LongTop'
from dual connect by level <= 1000000;
SQL>commit;
SQL>analyze index test1_idx validate structure;
SQL>select blocks, lf_blks, del_lf_rows from index_stats;
BLOCKS LF_BLKS DEL_LF_ROWS
-------------- ---------------- ------------------ -------------------
2048 1999 0
發現blocks的數量比原先的更少。
實驗三、非唯一索引建立後commit (delete+insert)
SQL>create table test1 (id number, name varchar2(20));
SQL>insert into test1 select rownum, 'TEST1'
from dual connect by level <=1000000;
SQL>commit;
SQL>create index test1_idx on test1(id);
SQL>commit;
SQL>analyze index test1_idx validate structure;
SQL>select blocks, lf_rows, lf_blks, del_lf_rows from index_stats;
BLOCKS LF_ROWS LF_BLKS DEL_LF_ROWS
-------------- ---------------- ------------------ -------------------
2304 1000000 2226 0
將表資料刪除,然後重新insert新的資料
SQL>truncate table test1 (或者 delete test1)
SQL>insert into test1 select rownum, 'LongTop'
from dual connect by level <= 1000000;
SQL>commit;
SQL>analyze index test1_idx validate structure;
SQL>select blocks, lf_blks, del_lf_rows from index_stats;
BLOCKS LF_BLKS DEL_LF_ROWS
-------------------- ---------- -----------------
2048 1999 0
實驗四、唯一索引(delete+insert)
SQL>create table test2 (id number, name varchar2(20));
SQL>insert into test2 select rownum, 'TEST2'
from dual connect by level <=1000000;
SQL>commit;
SQL>create unique index test2_idx on test2(id);
SQL>analyze index test2_idx validate structure;
SQL>select blocks, lf_rows, lf_blks, del_lf_rows from index_stats;
BLOCKS LF_ROWS LF_BLKS DEL_LF_ROWS
-------------- ---------------- ------------------ -------------------
2176 1000000 2088 0
將表資料刪除,然後重新insert新的資料
SQL> delete test2;
SQL>insert into test2 select rownum, 'LongTop'
from dual connect by level <= 1000000;
SQL>commit;
SQL>analyze index test2_idx validate structure;
SQL>select blocks, lf_blks, del_lf_rows from index_stats;
BLOCKS LF_BLKS DEL_LF_ROWS
-------------------- ---------- -----------------
2176 2088 0
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/354732/viewspace-605655/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 唯一索引和非唯一索引ROWID儲存的區別索引
- 關於唯一性索引造成堵塞和非唯一性索引造成堵塞的區別索引
- mysql的唯一索引不會利用change bufferMySql索引
- 唯一索引索引
- 轉載-treedump索引讀取索引儲存的資料值--非唯一性索引索引
- 主鍵(PK)與非空唯一索引(Unique Index)的區別索引Index
- MongoDB之索引(唯一索引)MongoDB索引
- MySQL <唯一索引>MySql索引
- 唯一索引,可以在索引列插入多個null嗎索引Null
- MySQL 唯一索引和普通索引MySql索引
- Oracle主鍵、唯一鍵與唯一索引的區別Oracle索引
- 唯一性約束和唯一性索引的區別索引
- 一個查詢不走索引的例子索引
- 【Mongo】mongos shard 唯一索引的問題Go索引
- 主鍵和唯一索引的區別索引
- 唯一索引操作可能產生的鎖索引
- mysql唯一索引是什麼MySql索引
- 資料庫聚簇索引——not null條件對唯一鍵索引成為聚簇索引的影響資料庫索引Null
- MySQL 唯一索引範圍查詢鎖下一個記錄的理解MySql索引
- [MySQL]為什麼非聚簇索引不儲存資料位置MySql索引
- Oracle主鍵約束、唯一鍵約束、唯一索引的區別(轉)Oracle索引
- 唯一index和非唯一index中leaf node裡rowid的一點區別。Index
- 資料庫的聚集索引和非聚集索引 很好的詳細說明資料庫索引
- MySQL實戰45講——普通索引和唯一索引MySql索引
- Eclipse 不為人所知的另一面 - 企業管理軟體領域 ABAP 程式語言開發利器Eclipse
- 資料庫表的唯一索引問題資料庫索引
- MYSQL中的普通索引,主健,唯一,全文索引區別MySql索引
- 8個不為人知的Docker 知識Docker
- npm和package.json那些不為常人所知的小祕密NPMPackageJSON
- 揭曉Model 3電動車5大“不為人知”的細節
- 【INDEX】Oracle中主鍵、唯一約束與唯一索引之區別IndexOracle索引
- 唯一性索引優化實踐索引優化
- vue一些不為人知的用法Vue
- 明明加了唯一索引,為什麼還是產生了重複資料?索引
- 明明加了唯一索引,為什麼還是產生重複資料?索引
- 超讚!有哪些很少為人所知,但是很有意思的網站?網站
- 你一無所知的CSSCSS
- 如何選擇普通索引和唯一索引《死磕MySQL系列 五》索引MySql