Bitmap 索引 vs. B-tree 索引:如何选择以及何时使用?——4-5
——理解適當(dāng)使用每個(gè)索引對(duì)性能的影響
Bitmap 索引 vs. B-tree 索引:如何選擇以及何時(shí)使用?——1-5
Bitmap 索引 vs. B-tree 索引:如何選擇以及何時(shí)使用?——2-5
Bitmap 索引 vs. B-tree 索引:如何選擇以及何時(shí)使用?——3-5
Bitmap 索引 vs. B-tree 索引:如何選擇以及何時(shí)使用?——4-5
Bitmap 索引 vs. B-tree 索引:如何選擇以及何時(shí)使用?——5-5
?
本文內(nèi)容
- 步驟 6(添加一個(gè) GENDER 列)
步驟 6(添加一個(gè) GENDER 列)
在一個(gè)低基數(shù)列執(zhí)行測(cè)試前,向表添加一個(gè) GENDER 列,并基于某個(gè)條件用 M、F、null 值來(lái)更新。
SQL> alter table test_normal add GENDER varchar2(1); ? 表已更改。 ? SQL>用下面匿名過(guò)程更新 GENDER 列的值,之后看看列值如何分布:
DECLARE v_empno test_normal.empno%type; CURSOR c IS SELECT empno FROM test_normal; BEGIN OPEN c; LOOP FETCH c INTO v_empno; exit when c%notfound; IF dbms_random.random MOD 3 = 1 THEN UPDATE test_normal set gender = 'M' WHERE empno = v_empno; ELSIF dbms_random.random mod 3 = 2 THEN UPDATE test_normal set gender = 'F' WHERE empno = v_empno; ELSIF dbms_random.random mod 3 = 0 THEN UPDATE test_normal set gender = null WHERE empno = v_empno; END IF; commit; END LOOP; CLOSE c; END; SQL> select GENDER, count(*) from test_normal group by GENDER; ? GENDER COUNT(*) ---------- ---------- 385961 M 320767 F 293272 ? SQL>該列上的 Bitmap 索引大小約在 620KB 左右,如下所示:
SQL> create bitmap index normal_GENDER_bmx on test_normal(GENDER); ? 索引已創(chuàng)建。 ? SQL> select substr(segment_name,1,30) segment_name, bytes/1024/1024 "Size in MB" 2 from user_segments 3 where segment_name in ('TEST_NORMAL','NORMAL_GENDER_BMX'); ? SEGMENT_NAME Size in MB ------------------------------------------------------------ ---------- NORMAL_GENDER_BMX .625 TEST_NORMAL 50 ? SQL>相比,該列上的 B-tree 索引為 9MB,比 Bitmap 索引大很多。
SQL> drop index normal_gender_bmx; ? 索引已刪除。 ? SQL> create index normal_gender_idx on test_normal(gender); ? 索引已創(chuàng)建。 ? SQL> select substr(segment_name,1,30) segment_name, bytes/1024/1024 "Size in MB" ? 2 from user_segments 3 where segment_name in ('TEST_NORMAL','NORMAL_GENDER_IDX'); ? SEGMENT_NAME Size in MB ------------------------------------------------------------ ---------- NORMAL_GENDER_IDX 9 TEST_NORMAL 50 ? SQL>現(xiàn)在,若執(zhí)行一個(gè)等值謂詞查詢,那么優(yōu)化器將不使用索引,Bitmap 索引或 B-tree 索引。而是執(zhí)行一個(gè)全表掃描。
SQL> select * from test_normal where GENDER is null; ? 已選擇385961行。 ? ? 執(zhí)行計(jì)劃 ---------------------------------------------------------- Plan hash value: 512490529 ? --------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time| --------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 1000K| 37M| 1725 (2)| 00:00:21| |* 1 | TABLE ACCESS FULL| TEST_NORMAL | 1000K| 37M| 1725 (2)| 00:00:21| --------------------------------------------------------------------------------- ? Predicate Information (identified by operation id): --------------------------------------------------- 1 - filter("GENDER" IS NULL) ? ? 統(tǒng)計(jì)信息 ---------------------------------------------------------- 0 recursive calls 0 db block gets 31825 consistent gets 0 physical reads 0 redo size 22065645 bytes sent via SQL*Net to client 283550 bytes received via SQL*Net from client 25732 SQL*Net roundtrips to/from client 0 sorts (memory) 0 sorts (disk) 385961 rows processed ? SQL> SQL> select * from test_normal where GENDER='M'; ? 已選擇320767行。 ? ? 執(zhí)行計(jì)劃 ---------------------------------------------------------- Plan hash value: 654360527 ? ------------------------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | ------------------------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 1 | 39 | 2 (0)| 00:00:01 | | 1 | TABLE ACCESS BY INDEX ROWID| TEST_NORMAL | 1 | 39 | 2 (0)| 00:00:01 | |* 2 | INDEX RANGE SCAN | NORMAL_GENDER_IDX | 1 | | 2 (0)| 00:00:01 | ------------------------------------------------------------------------------------------------- ? Predicate Information (identified by operation id): --------------------------------------------------- 2 - access("GENDER"='M') ? ? 統(tǒng)計(jì)信息 ---------------------------------------------------------- 0 recursive calls 0 db block gets 49138 consistent gets 0 physical reads 0 redo size 19044637 bytes sent via SQL*Net to client 235744 bytes received via SQL*Net from client 21386 SQL*Net roundtrips to/from client 0 sorts (memory) 0 sorts (disk) 320767 rows processed ? SQL> SQL> select * from test_normal where GENDER='F'; ? 已選擇293272行。 ? ? 執(zhí)行計(jì)劃 ---------------------------------------------------------- Plan hash value: 654360527 ? ------------------------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | ------------------------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 1 | 39 | 2 (0)| 00:00:01 | | 1 | TABLE ACCESS BY INDEX ROWID| TEST_NORMAL | 1 | 39 | 2 (0)| 00:00:01 | |* 2 | INDEX RANGE SCAN | NORMAL_GENDER_IDX | 1 | | 2 (0)| 00:00:01 | ------------------------------------------------------------------------------------------------- ? Predicate Information (identified by operation id): --------------------------------------------------- 2 - access("GENDER"='F') ? ? 統(tǒng)計(jì)信息 ---------------------------------------------------------- 0 recursive calls 0 db block gets 45430 consistent gets 0 physical reads 0 redo size 17412131 bytes sent via SQL*Net to client 215581 bytes received via SQL*Net from client 19553 SQL*Net roundtrips to/from client 0 sorts (memory) 0 sorts (disk) 293272 rows processed ? SQL>
表 8 Bitmap 索引在 TEST_NORMAL 表 EMPNO、SAL 和 GENDER 列的統(tǒng)計(jì)信息?
| ? | EMPNO | SAL | GENDER |
| 基數(shù)程度 | ? 高 | ? 一般 | ? 低 |
| size | ? 28MB | ? 4MB | ? 625KB |
基數(shù)越低,Bitmap 索引越小。
表 9 B-tree 索引在 TEST_NORMAL 表 EMPNO、SAL 和 GENDER 列的統(tǒng)計(jì)信息
| ? | EMPNO | SAL | GENDER |
| 基數(shù)程度 | ? 高 | ? 一般 | ? 低 |
| size | ? 18MB | ? 17MB | ? 9MB |
總結(jié)
以上是生活随笔為你收集整理的Bitmap 索引 vs. B-tree 索引:如何选择以及何时使用?——4-5的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Oracle 双机热备 镜像 纯软 实战
- 下一篇: X皮书之shell 常用代码