index有時候可能會比表大!
之前沒有想過這個問題,第一次看到這樣的說法是在piner的面試題中。當時覺得有點意思,這幾天細讀doc,上面也提到了這樣的說法:
Consider an index on a table on which a lot of DML has been performed. Because of the DML, the size of the index can increase to the point where each block is only 50% full, or even less. If the index refers to most of the columns in the table, then the index could actually be larger than the table.
[@more@]doc:
Re-creating Indexes
You might want to re-create an index to compact it and minimize fragmented space, or to change the index's storage characteristics. When creating a new index that is a subset of an existing index or when rebuilding an existing index with new storage characteristics, Oracle might use the existing index instead of the base table to improve the performance of the index build.
Note: To avoid calling |
However, there are cases where it can be beneficial to use the base table instead of the existing index. Consider an index on a table on which a lot of DML has been performed. Because of the DML, the size of the index can increase to the point where each block is only 50% full, or even less. If the index refers to most of the columns in the table, then the index could actually be larger than the table. In this case, it is faster to use the base table rather than the index to re-create the index.
Use the ALTER
INDEX
... REBUILD
statement to reorganize or compact an existing index or to change its storage characteristics. The REBUILD
statement uses the existing index as the basis for the new one. All index storage statements are supported, such as STORAGE
(for extent allocation), TABLESPACE
(to move the index to a new tablespace), and INITRANS
(to change the initial number of entries).
Usually, ALTER
INDEX
... REBUILD
is faster than dropping and re-creating an index, because this statement uses the fast full scan feature. It reads all the index blocks using multiblock I/O, then discards the branch blocks. A further advantage of this approach is that the old index is still available for queries while the rebuild is in progress
--===================================
SQL> create table t(id ,name) as select object_id , object_name from dba_objects
;
表已建立。
SQL> desc t
名稱 是否為空? 型別
----------------------------------------- -------- ----------------------------
ID NUMBER
NAME VARCHAR2(128)
SQL> create index idx_t on t(id , name);
索引已建立。
SQL> analyze table t compute statistics;
表已分析。
SQL> select blocks from user_tables where table_name='T';
BLOCKS
----------
70
SQL> select leaf_blocks from user_indexes where index_name='IDX_T';
LEAF_BLOCKS
-----------
77
已經看出了index比表大,為什麼這樣?這裡主要是t只有id和name,而index也包含了id和name,但是別忘了index還包含了rowid!
SQL> delete from t where id<=5000;
已刪除4928行。
SQL> commit;
提交完成。
SQL> alter table t move ;
表已更改。
SQL> analyze table t compute statistics;
analyze table t compute statistics
*
第 1 行出現錯誤:
ORA-01502: 索引 'XYS.IDX_T' 或這類索引的分割槽處於不可用狀態
SQL> alter index idx_t rebuild;
索引已更改。
SQL> analyze table t compute statistics for all indexes;
表已分析。
SQL> select blocks from user_tables where table_name='T';
BLOCKS
----------
70
SQL> select leaf_blocks from user_indexes where index_name='IDX_T';
LEAF_BLOCKS
-----------
55
SQL> insert into t select object_id , object_name from dba_objects;
已建立15810行。
SQL> commit;
提交完成。
SQL> analyze table t compute statistics for all indexes;
表已分析。
SQL> select blocks from user_tables where table_name='T';
BLOCKS
----------
70
SQL> select leaf_blocks from user_indexes where index_name='IDX_T';
LEAF_BLOCKS
-----------
148
SQL>
透過執行dml操作,這次效果更加明顯,index顯然比表t大!
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/19602/viewspace-1002902/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- (大表小技巧)有時候 2 小時的 SQL 操作,可能只要 1 分鐘SQL
- MySQL什麼時候會使用內部臨時表?MySql
- Laravel 日誌有時候有許可權有時候沒有許可權?Laravel
- selenium 在 docker 上 截圖有時候正常,有時候圖片全黑-黑屏Docker
- Scrum不是一顆銀彈,有時可能會浪費大量時間 - RemoHJansenScrumREM
- 當你感到絕望時,可能是最接近成功的時候
- OneThink什麼時候會有基於thinkphp5開發的啊?PHP
- shiro 什麼時候會進入 doGetAuthorizationInfo() ?
- 雲遊戲平臺取代次時代主機?可能還沒到時候遊戲
- mysql建立表的時候對欄位和表新增COMMENTMySql
- 測試開發的技術是否在混沌探索時期,這時候抓住機會就可能一飛沖天
- 我在設計資料庫的時候會考慮到哪些資料欄位將來可能會發生變更。資料庫
- 有的時候我覺得我不會 Markdown
- mysql什麼時候會發生file sortMySql
- 什麼時候會傳送options請求
- 身邊同事技術比你差,你會有看不起同事的時候嗎?
- SAP CRM中介軟體下載時,為什麼有時候會生成一個奇怪的BDOC容器
- MySQL建立表的時候建立聯合索引的方法MySql索引
- 大資料時代,真的會有網站使用 DNT 嗎?大資料網站
- 蘋果新款MacBook Pro可能會有SD卡插槽蘋果MacSD卡
- Python會在什麼時候被其他語言取代Python
- 如果進行評論時候多思考一遍內容,是否網暴發生的可能性就會降低?
- mac安裝swoole的時候沒有許可權Mac
- golang const 宣告常量量時候,只寫名字,就會自動有值?——>[批量賦值][列舉]Golang賦值
- iview 表單提交資料的時候驗證問題View
- hive orc表'orc.create.index'='true'與'orc.create.index'='false'HiveIndexFalse
- “浪費時間”有時候也沒錯,比如在《小森生活》裡
- 請問什麼時候物件分配會不在 TLAB 內分配物件
- 你是什麼時候”突然”學會程式設計的程式設計
- 當我談自律的時候,我會談什麼(一)
- 檢視mysql哪張表比較大MySql
- 丁磊:那時候我們除了會寫軟體 什麼也不會做
- 154的cpu有時候還是挺高,先觀察
- mysql left join的時候又表是多條記錄的話,會出現冗餘的情況MySql
- table/index/LOBINDEX遷移表空間Index
- Java是瀕臨死亡還是比以往任何時候都更有活力? - RogerJava
- Android大變天,是時候和ButterKnife說再見了!Android
- 對DevOps的九大誤解,是時候糾正了!dev
- 太可怕!下賽季國安可能會讓球迷用VR看比賽VR