有的时候,使用错误的索引会导致oracle数据库的效率明显下降,通过一些方法或者是技巧可以有效的避免这个问题:
这个例子中,如果我想使用idx_a而不是idx_b.
SQL> create table test 2 (a int,b int,c int,d int); Table created. SQL> begin 2 for i in 1..50000 3 loop 4 insert into mytest values(i,i,i,i); 5 end loop; 6 commit; 7 end; 8 / PL/SQL procedure successfully completed. SQL> create index idx_a on mytest(a,b,c); Index created. SQL> create index idx_b on mytest(b); Index created. |
如表mytest,有字段a,b,c,d,在a,b,c上建立联合索引idx_a(a,b,c),在b上单独建立了一个索引idx_b(b)。
在正常情况下,where a=? and b=? and c=?会用到索引idx_a,where b=?会用到索引idx_b
比如:
SQL> analyze table mytest compute statistics; Table analyzed. SQL> select num_Rows from user_tables where table_name='MYTEST'; NUM_ROWS ---------- 50000 SQL> select distinct_keys from user_indexes where index_name='IDX_A'; DISTINCT_KEYS ------------- 50000 SQL> set autotrace traceonly SQL> select d from mytest 2 where a=10 and b=10 and c=10; Execution Plan ---------------------------------------------------------- Plan hash value: 1542625214 -------------------------------------------------------------------------------- ------ | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | -------------------------------------------------------------------------------- ------ | 0 | SELECT STATEMENT | | 1 | 16 | 2 (0)| 00:0 0:01 | | 1 | TABLE access BY INDEX ROWID| MYTEST | 1 | 16 | 2 (0)| 00:0 0:01 | |* 2 | INDEX RANGE SCAN | IDX_A | 1 | | 1 (0)| 00:0 0:01 | -------------------------------------------------------------------------------- ------ Predicate Information (identified by operation id): --------------------------------------------------- 2 - access("A"=10 AND "B"=10 AND "C"=10) Statistics ---------------------------------------------------------- 1 recursive calls 0 db block gets 4 consistent gets 0 physical reads 0 redo size 508 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 SQL> select d from mytest 2 where b=500; Execution Plan ---------------------------------------------------------- Plan hash value: 530004086 -------------------------------------------------------------------------------- ------ | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | -------------------------------------------------------------------------------- ------ | 0 | SELECT STATEMENT | | 1 | 8 | 2 (0)| 00:0 0:01 | | 1 | TABLE ACCESS BY INDEX ROWID| MYTEST | 1 | 8 | 2 (0)| 00:0 0:01 | |* 2 | INDEX RANGE SCAN | IDX_B | 1 | | 1 (0)| 00:0 0:01 | -------------------------------------------------------------------------------- ------ Predicate Information (identified by operation id): --------------------------------------------------- 2 - access("B"=500) Statistics ---------------------------------------------------------- 1 recursive calls 0 db block gets 4 consistent gets 0 physical reads 0 redo size 508 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 |
#p#副标题#e#
但是在这样一个条件下:where a=? and b=? and c=? group by b会用到哪个索引呢?在索引的分析数
关键词标签:Oracle
相关阅读
热门文章 Oracle中使用alter table来增加,删除,修改列oracle中使用SQL语句修改字段类型-oracle修使用低权限Oracle数据库账户得到管理员权限Oracle对user的访问控制
人气排行 ORACLE SQL 判断字符串是否为数字的语句Oracle中使用alter table来增加,删除,修改列的语法ORACLE和SQL语法区别归纳(1)oracle grant 授权语句如何加速Oracle大批量数据处理Oracle删除表的几种方法ORACLE修改IP地址后如何能够使用Oracle 10g创建表空间和用户并指定权限
查看所有0条评论>>