设为首页 加入收藏

TOP

SQL中的null(二)
2015-11-21 01:51:20 来源: 作者: 【 】 浏览:1
Tags:SQL null
Predicate Information (identified by operation id): --------------------------------------------------- 1 - filter("SNAME" IS NULL) Note ----- - dynamic sampling used for this statement (level=2) 17 rows selected.

如果实际情况确实需要is null查询走索引呢?可通过创建联合索引的方式来实现。

drop index n1_sname ;

create index n1_sname_ind on n1(sname,sid);

?

?

SQL> explain plan for select * from n1 where sid is not null and sname is null ;

?

SQL> select * from table(dbms_xplan.display);
Plan hash value: 3644017351
-------------------------------------------------------------------------------
| Id  | Operation	 | Name 	| Rows	| Bytes | Cost (%CPU)| Time
|
-------------------------------------------------------------------------------
|   0 | SELECT STATEMENT |		|     1 |    75 |     3   (0)| 00:00:01
|
|*  1 |  INDEX RANGE SCAN| N1_SNAME_IND |     1 |    75 |     3   (0)| 00:00:01
|
-------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
   1 - access("SNAME" IS NULL)
       filter("SID" IS NOT NULL)
Note
-----
   - dynamic sampling used for this statement (level=2)

18 rows selected.

?

可以看到创建联合索引后,SQL查询所耗费的资源明显降低。

需要注意的是我们查询最频繁最经常使用列,比如sname要放在联合索引的第一列;同时要走联合索引,需要where后面的条件出现联合索引包含的所有的字段,这也是为什么加了sid这个字段的原因。

首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇java.sql.SQLException:索引中丢.. 下一篇sql多表连接查询

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: