如何进行null与index的分析

30次阅读
没有评论

共计 5248 个字符,预计需要花费 14 分钟才能阅读完成。

这期内容当中丸趣 TV 小编将会给大家带来有关如何进行 null 与 index 的分析,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

今天在测试过程中遇到一问题, SQL 该走 Index 的, 没走. 加 index hint 也不行. 描述如下:

1. 建立测试表
create table t1
as
select object_id, object_name from dba_objects;

2. 在 object_name 列上建立 b -tree index
create index idx_t1_name on t1(object_name);

3. 如果我是 select object_name from t1, 按理说 CBO 应该会选择走 Index scan. 但奇怪的是结果走的 full table scan.
SQL set autotrace trace exp
SQL select object_name from t1;

Execution Plan
———————————————————-
Plan hash value: 3617692013

————————————————————————–
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
————————————————————————–
| 0 | SELECT STATEMENT | | 50934 | 3282K| 57 (2)| 00:00:01 |
| 1 | TABLE ACCESS FULL| T1 | 50934 | 3282K| 57 (2)| 00:00:01 |
————————————————————————–

Note
—–
– dynamic sampling used for this statement

[@more@]
3. 使用 index hint 想强行走 Index, 结果还是 full table scan. 我就奇怪了. hint 咋个不起做用呢? 郁闷.
SQL select /*+ index(t1, idx_t1_name) */ object_name from t1;

Execution Plan
———————————————————-
Plan hash value: 3617692013

————————————————————————–
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
————————————————————————–
| 0 | SELECT STATEMENT | | 50934 | 3282K| 57 (2)| 00:00:01 |
| 1 | TABLE ACCESS FULL| T1 | 50934 | 3282K| 57 (2)| 00:00:01 |
————————————————————————–

Note
—–
– dynamic sampling used for this statement

4. 偶然看了下表结构
SQL desc t1
Name Null? Type
—————————————– ——– —————————-
OBJECT_ID NUMBER
OBJECT_NAME VARCHAR2(128)

NULL 列引起我的注意. OBJECT_NAME 可以为 null !! 而在 oracle 中单个列上建 b -tree Index, null 是不会存进 Index 的 (复合索引可以, 只要整个 Index columns 不为 null). 那就是说如果有些行的 object_name 是 null, 那走 Index 取值不是会丢掉 object_name 为 null 的行. 那如果我让 object_name not null 呢?

SQL alter table t1 modify object_name not null;

Table altered.

SQL desc t1
Name Null? Type
—————————————– ——– —————————-
OBJECT_ID NUMBER
OBJECT_NAME NOT NULL VARCHAR2(128)

再试一试
SQL select object_name from t1;

Execution Plan
———————————————————-
Plan hash value: 3617692013

————————————————————————–
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
————————————————————————–
| 0 | SELECT STATEMENT | | 50934 | 3282K| 57 (2)| 00:00:01 |
| 1 | TABLE ACCESS FULL| T1 | 50934 | 3282K| 57 (2)| 00:00:01 |
————————————————————————–

结果还是 full table scan : (

试试用 hint
SQL select /*+ index(t1, idx_t1_name) */ object_name from t1;

Execution Plan
———————————————————- 
Plan hash value: 1352742509 

——————————————————————————–
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
——————————————————————————–
| 0 | SELECT STATEMENT | | 50934 | 3282K| 264 (1)| 00:00:04 |
| 1 | INDEX FULL SCAN | IDX_T1_NAME | 50934 | 3282K| 264 (1)| 00:00:04 |

这回 hint 起作用了. 这说明并不是 Hint 失效, 只是满足走 Index 的条件一开始没有具备. 看来 null 是个潜在杀手, 得小心防范. 
现在强走 index 是 ok 了. 但, 是什么东西会影响 CBO 的判断不走 Index 呢? 想到统计信息可能会是原因之一, 于是查看了一下.

SQL select index_name, LAST_ANALYZED from user_indexes;
INDEX_NAME LAST_ANALYZED 
—————————————————————————————— ————— 
IDX_T1_NAME 01-MAR-18

SQL select table_name, LAST_ANALYZED from user_tables;

TABLE_NAME LAST_ANALYZED 
—————————————————————————————— ————— 
T1

看到刚建的表没有做过统计. 于是 go to analyze table, 结果如下:

SQL exec dbms_stats.gather_table_stats(TEST , T1

PL/SQL procedure successfully completed.

SQL select table_name, LAST_ANALYZED from user_tables;

TABLE_NAME LAST_ANALYZED 
—————————————————————————————— ————— 
T1 01-MAR-18

再来看看执行结果有没有变化:
SQL select object_name from t1;

Execution Plan
———————————————————- 
Plan hash value: 222950081

———————————————————————————— 
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | 
———————————————————————————— 
| 0 | SELECT STATEMENT | | 49917 | 1218K| 57 (2)| 00:00:01 | 
| 1 | INDEX FAST FULL SCAN| IDX_T1_NAME | 49917 | 1218K| 57 (2)| 00:00:01 | 
————————————————————————————

这下终于走 Index 这条路老 : ) 在 Index 中, key value 是排序存放的. Index Fast full scan 它是按照 block 的存储顺序来读取数据, 并可以一次 I / O 多块读取提高效率 (参数 readdb_file_multiblock_read_count), 但返回的值是没有排序的. 而
Index full scan 会按照 Key value 顺序读取值, 返回排了序的结果. 所以, 做个 order by 会是走 Index full scan.

SQL select object_name from t1 order by object_name;

Execution Plan
———————————————————- 
Plan hash value: 1352742509

——————————————————————————– 
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | 
——————————————————————————– 
| 0 | SELECT STATEMENT | | 49917 | 1218K| 249 (1)| 00:00:03 | 
| 1 | INDEX FULL SCAN | IDX_T1_NAME | 49917 | 1218K| 249 (1)| 00:00:03 | 
——————————————————————————–

对于定义为 NULL 的列,创建位图索引可走索引

上述就是丸趣 TV 小编为大家分享的如何进行 null 与 index 的分析了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注丸趣 TV 行业资讯频道。

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-07-18发表,共计5248字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)