附件
前言
sql是否可以使用索引,与很多因素有关,比如WHERE条件列的选择度,表及列的统计信息,可能还包括列是否为空,本文仅讨论索引是否为空,与索引是否使用的一些关系测试结论
测试明细
SQL> create table t_null_nonnull(a int,b int);
Table created.
SQL> create table t_null_nonnull(a int,b int);
Table created.
SQL> desc t_null_nonnull;
Name Null? Type
----------------------------------------- -------- ----------------------------
A NUMBER(38)
B NUMBER(38)
SQL> insert into t_null_nonnull select level,level from dual connect by level<=10000;
10000 rows created.
SQL> commit;
Commit complete.
SQL> select a from t_null_nonnull;
10000 rows selected.
Execution Plan
----------------------------------------------------------
Plan hash value: 1444422154
------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 10000 | 126K| 7 (0)| 00:00:01 |
| 1 | TABLE ACCESS FULL| T_NULL_NONNULL | 10000 | 126K| 7 (0)| 00:00:01 |
------------------------------------------------------------------------------------
Note
-----
- dynamic sampling used for this statement (level=2)
Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
689 consistent gets
0 physical reads
0 redo size
174298 bytes sent via SQL*Net to client
7845 bytes received via SQL*Net from client
668 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
10000 rows processed
SQL>
SQL> create index idx_t_nonnull on t_null_nonnull(a);
Index created.
SQL> select a from t_null_nonnull;
10000 rows selected.
Execution Plan
----------------------------------------------------------
Plan hash value: 1444422154
------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 10000 | 126K| 7 (0)| 00:00:01 |
| 1 | TABLE ACCESS FULL| T_NULL_NONNULL | 10000 | 126K| 7 (0)| 00:00:01 |
------------------------------------------------------------------------------------
Note
-----
- dynamic sampling used for this statement (level=2)
Statistics
----------------------------------------------------------
5 recursive calls
0 db block gets
689 consistent gets
0 physical reads
0 redo size
174298 bytes sent via SQL*Net to client
7845 bytes received via SQL*Net from client
668 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
10000 rows processed
SQL>
SQL> select /*+ index(t_null_nonnull idx_t_nonnull) */ a from t_null_nonnull;
10000 rows selected.
Execution Plan
----------------------------------------------------------
Plan hash value: 1444422154
------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 10000 | 126K| 7 (0)| 00:00:01 |
| 1 | TABLE ACCESS FULL| T_NULL_NONNULL | 10000 | 126K| 7 (0)| 00:00:01 |
------------------------------------------------------------------------------------
Note
-----
- dynamic sampling used for this statement (level=2)
Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
689 consistent gets
0 physical reads
0 redo size
174298 bytes sent via SQL*Net to client
7845 bytes received via SQL*Net from client
668 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
10000 rows processed
SQL>
SQL> alter table t_null_nonnull modify a not null;
Table altered.
SQL> set linesize 30
SQL> desc t_null_nonnull;
Name Null? Type
----------------- -------- ------------
A NOT NULL NUMBER(38)
B NUMBER(38)
SQL> select a from t_null_nonnull;
10000 rows selected.
Execution Plan
----------------------------------------------------------
Plan hash value: 1444422154
------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 10000 | 126K| 7 (0)| 00:00:01 |
| 1 | TABLE ACCESS FULL| T_NULL_NONNULL | 10000 | 126K| 7 (0)| 00:00:01 |
------------------------------------------------------------------------------------
Note
-----
- dynamic sampling used for this statement (level=2)
Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
689 consistent gets
0 physical reads
0 redo size
174298 bytes sent via SQL*Net to client
7845 bytes received via SQL*Net from client
668 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
10000 rows processed
SQL>
SQL> select /*+ index(t_null_nonnull idx_t_nonnull) */ a from t_null_nonnull;
10000 rows selected.
Execution Plan
----------------------------------------------------------
Plan hash value: 4141558108
----------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 10000 | 126K| 27 (0)| 00:00:01 |
| 1 | INDEX FULL SCAN | IDX_T_NONNULL | 10000 | 126K| 27 (0)| 00:00:01 |
----------------------------------------------------------------------------------
Note
-----
- dynamic sampling used for this statement (level=2)
Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
687 consistent gets
0 physical reads
0 redo size
174298 bytes sent via SQL*Net to client
7845 bytes received via SQL*Net from client
668 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
10000 rows processed
SQL>
SQL> set autot off
SQL> truncate table t_null_nonnull;
Table truncated.
SQL> insert into t_null_nonnull select level,level from dual connect by level<=10000;
10000 rows created.
SQL> commit;
Commit complete.
SQL> select a from t_null_nonnull;
10000 rows selected.
Execution Plan
----------------------------------------------------------
Plan hash value: 315757037
--------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 10000 | 126K| 7 (0)| 00:00:01 |
| 1 | INDEX FAST FULL SCAN| IDX_T_NONNULL | 10000 | 126K| 7 (0)| 00:00:01 |
--------------------------------------------------------------------------------------
Note
-----
- dynamic sampling used for this statement (level=2)
Statistics
----------------------------------------------------------
4 recursive calls
0 db block gets
715 consistent gets
0 physical reads
0 redo size
174298 bytes sent via SQL*Net to client
7845 bytes received via SQL*Net from client
668 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
10000 rows processed
SQL>
SQL> alter table t_null_nonnull modify a null;
Table altered.
SQL> select a from t_null_nonnull;
10000 rows selected.
Execution Plan
----------------------------------------------------------
Plan hash value: 1444422154
------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 10000 | 126K| 7 (0)| 00:00:01 |
| 1 | TABLE ACCESS FULL| T_NULL_NONNULL | 10000 | 126K| 7 (0)| 00:00:01 |
------------------------------------------------------------------------------------
Note
-----
- dynamic sampling used for this statement (level=2)
Statistics
----------------------------------------------------------
187 recursive calls
0 db block gets
739 consistent gets
0 physical reads
0 redo size
174298 bytes sent via SQL*Net to client
7845 bytes received via SQL*Net from client
668 SQL*Net roundtrips to/from client
6 sorts (memory)
0 sorts (disk)
10000 rows processed
SQL>
SQL> select a from t_null_nonnull where a is not null;
10000 rows selected.
Execution Plan
----------------------------------------------------------
Plan hash value: 315757037
--------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 10000 | 126K| 7 (0)| 00:00:01 |
|* 1 | INDEX FAST FULL SCAN| IDX_T_NONNULL | 10000 | 126K| 7 (0)| 00:00:01 |
--------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("A" IS NOT NULL)
Note
-----
- dynamic sampling used for this statement (level=2)
Statistics
----------------------------------------------------------
5 recursive calls
0 db block gets
715 consistent gets
0 physical reads
0 redo size
174298 bytes sent via SQL*Net to client
7845 bytes received via SQL*Net from client
668 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
10000 rows processed
SQL>
个人简介
项目经验:
贵州移动crm及客服数据库性能优化项目
贵州移动crm及客服务数据库sql审核项目
深圳穆迪软件有限公司数据库性能优化项目
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/9240380/viewspace-1694512/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/9240380/viewspace-1694512/