一个字段设置为Null/Not Null,究竟有无考量因素呢? 请看下面的例 子:
sql > create table test_tab as select * from user_objects;
sql > create unique index test_tab_id on test_tab(object_id);
sql > set autotrace traceonly explain;
sql > select object_id from test_tab;
Execution Plan
0 SELECT STATEMENT Optimizer = ALL_ROWS(Cost = 3 Card =9 Bytes=117)
1 0 TABLE ACCESS(FULL) OF 'TEST_TAB'(TABLE) (Cost = 3 Card=9 Bytes=117)
sql > analyze table test_tab compute statistics for table for all indexed columns for all indexes;
sql> select object_id from test_tab;
Execution Plan
0 SELECT STATEMENT Optimizer= ALL_ROWS(Cost=3 Card =9 Bytes=36)
1 0 TABLE ACCESS(FULL) OF 'TEST_TAB'(TABLE)(Cost=3 Card = 9 Bytes=36)
sql > alter table test_tab modify(object_id not null);
sql > select object_id from test_tab;
Execution Plan
0 SELECT STATEMENT Optimizer=ALL_ROWS(Cost=1 Card=9 Bytes = 36)
1 0 INDEX(FULL_SCAN) of "TEST_TAB_ID'(INDEX(UNIQUE)) (Cost=1 Card=9 Bytes = 36)
也就是说,如果列为NULL,将导致 索引扫描不会使用,而使用全表扫描,因此如果表没有primary key(primary key 一定是not null),而在某个列上建了唯一索引,如果可能,应该将该 列设置为not null,加大索引使用的可能性,以改善性能。
44
5