Oracle数据库效率技巧:避免错误的索引(1)(3)
我们可以通过如下的技巧避免使用idx_b,而使用idx_a。
where a=? and b=? and c=? group by b||'' --如果b是字符类型
where a=? and b=? and c=? group by b+0 --如果b是数字类型
通过这样简单的改变,往往可以是查询时间提交很多倍
当然,我们也可以使用no_index提示,相信很多人没有用过,也是一个不错的方法:
SQL> select /*+ no_index(mytest,idx_b) */ max(d) from mytest where a=50 and b=50 and c=50 group by b;
Execution Plan
----------------------------------------------------------
Plan hash value: 422688974
--------------------------------------------------------------------------------
-------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Tim
e |
--------------------------------------------------------------------------------
-------
| 0 | SELECT STATEMENT | | 1 | 16 | 9 (0)| 00:
00:01 |
| 1 | SORT GROUP BY NOSORT | | 1 | 16 | 9 (0)| 00:
00:01 |
| 2 | TABLE ACCESS BY INDEX ROWID| MYTEST | 1 | 16 | 9 (0)| 00:
00:01 |
|* 3 | INDEX RANGE SCAN | IDX_A | 500 | | 1 (0)| 00:
00:01 |
--------------------------------------------------------------------------------
-------
Predicate Information (identified by operation id):
---------------------------------------------------
3 - access("A"=50 AND "B"=50 AND "C"=50)
Statistics
----------------------------------------------------------
1 recursive calls
0 db block gets
3 consistent gets
0 physical reads
0 redo size
513 bytes sent via SQL*Net to client
492 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
上文中主要是以代码的形式为大家讲解的,看起来可能是不太容易理解,大家要深入其中去学习,这个技巧是非常实用的,希望大家能够从中收获。
- 上一篇:Oracle无法启动的处理过程
- 下一篇:如何处理Oracle中杀不掉的锁



