
select * from RESOUCES t where (t.loginid='yyyxxx' or t.account='yyyxxx') and t.status<4;SQL> select distinct account,count(*) from RESOUCES group by account;ACCOUNT COUNT(*)------------------------------------------------------------ ----------34493
SQL>Execution Plan----------------------------------------------------------Plan hash value: 3664880602---------------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |---------------------------------------------------------------------------------| 0 | SELECT STATEMENT | | 1 | 404 | 650 (1)| 00:00:08 ||* 1 | TABLE ACCESS FULL| RESOUCES | 1 | 404 | 650 (1)| 00:00:08 |---------------------------------------------------------------------------------Predicate Information (identified by operation id):---------------------------------------------------1 - filter("STATUS"<4 AND ("LOGINID"='yyyxxx' OR"ACCOUNT"='yyyxxx'))Statistics----------------------------------------------------------1 recursive calls0 db block gets2392 consistent gets0 physical reads0 redo size9744 bytes sent via SQL*Net to client524 bytes received via SQL*Net from client2 SQL*Net roundtrips to/from client0 sorts (memory)0 sorts (disk)1 rows processed
SQL>Execution Plan----------------------------------------------------------Plan hash value: 33080047------------------------------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |------------------------------------------------------------------------------------------------| 0 | SELECT STATEMENT | | 1 | 404 | 1463 (1)| 00:00:18 ||* 1 | TABLE ACCESS BY INDEX ROWID| RESOUCES | 1 | 404 | 1463 (1)| 00:00:18 ||* 2 | INDEX RANGE SCAN | IDX_RES_STATUS | 9250 | | 20 (0)| 00:00:01 |------------------------------------------------------------------------------------------------Predicate Information (identified by operation id):---------------------------------------------------1 - filter("T"."LOGINID"='yyyxxx' OR "T"."ACCOUNT"='yyyxxx')2 - access("T"."STATUS"<4)Statistics----------------------------------------------------------1 recursive calls0 db block gets7604 consistent gets0 physical reads0 redo size9744 bytes sent via SQL*Net to client524 bytes received via SQL*Net from client2 SQL*Net roundtrips to/from client0 sorts (memory)0 sorts (disk)1 rows processed
强制使用status字段的索引,性能比全表扫描更差。
尝试优化二(字段 account上建索引,优化器自动使用该索引)
SQL>Execution Plan----------------------------------------------------------Plan hash value: 3933866588------------------------------------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |------------------------------------------------------------------------------------------------------| 0 | SELECT STATEMENT | | 1 | 404 | 3 (0)| 00:00:01 ||* 1 | TABLE ACCESS BY INDEX ROWID | RESOUCES | 1 | 404 | 3 (0)| 00:00:01 || 2 | BITMAP CONVERSION TO ROWIDS | | | | | || 3 | BITMAP OR | | | | | || 4 | BITMAP CONVERSION FROM ROWIDS| | | | | ||* 5 | INDEX RANGE SCAN | IDX_HMRES_LOGID | | | 1 (0)| 00:00:01 || 6 | BITMAP CONVERSION FROM ROWIDS| | | | | ||* 7 | INDEX RANGE SCAN | IDX_HMRES_ACCOUNT | | | 1 (0)| 00:00:01 |------------------------------------------------------------------------------------------------------Predicate Information (identified by operation id):---------------------------------------------------1 - filter("T"."STATUS"<4)5 - access("T"."LOGINID"='yyyxxx')7 - access("T"."ACCOUNT"='yyyxxx')Statistics----------------------------------------------------------1 recursive calls0 db block gets5 consistent gets0 physical reads0 redo size9744 bytes sent via SQL*Net to client524 bytes received via SQL*Net from client2 SQL*Net roundtrips to/from client0 sorts (memory)0 sorts (disk)1 rows processed
用上account字段上的索引后,性能明显大幅提升。
select count(id) from RESOUCES t where t.passwordlock=1 and t.status in (0, 1, 2, 3);SQL> select distinct PASSWORDLOCK,count(*) from RESOUCES group by PASSWORDLOCK;PASSWORDLOCK COUNT(*)------------ ----------34493
SQL>Execution Plan----------------------------------------------------------Plan hash value: 3664880602---------------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |---------------------------------------------------------------------------------| 0 | SELECT STATEMENT | | | | 650 (100)| || 1 | TABLE ACCESS FULL| RESOURCES | 1 | 21 | 650 (1)| 00:00:08 |---------------------------------------------------------------------------------Predicate Information (identified by operation id):---------------------------------------------------1 - filter("T"."STATUS"=0 OR "T"."STATUS"=1 OR "T"."STATUS"=2 OR "T"."STATUS"=3 AND "T"."PASSWORDLOCK"=1)Statistics----------------------------------------------------------1 recursive calls0 db block gets2390 consistent gets0 physical reads0 redo size9744 bytes sent via SQL*Net to client524 bytes received via SQL*Net from client2 SQL*Net roundtrips to/from client0 sorts (memory)0 sorts (disk)1 rows processed
SQL>Execution Plan----------------------------------------------------------Plan hash value: 1465991502--------------------------------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |--------------------------------------------------------------------------------------------------| 0 | SELECT STATEMENT | | 1 | 16 | 1460 (1)| 00:00:18 || 1 | SORT AGGREGATE | | 1 | 16 | | || 2 | INLIST ITERATOR | | | | | ||* 3 | TABLE ACCESS BY INDEX ROWID| RESOUCES | 1 | 16 | 1460 (1)| 00:00:18 ||* 4 | INDEX RANGE SCAN | IDX_RES_STATUS | 9253 | | 17 (0)| 00:00:01 |--------------------------------------------------------------------------------------------------Predicate Information (identified by operation id):---------------------------------------------------3 - filter("T"."PASSWORDLOCK"=1)4 - access("T"."STATUS"=0 OR "T"."STATUS"=1 OR "T"."STATUS"=2 OR "T"."STATUS"=3)Statistics----------------------------------------------------------1 recursive calls0 db block gets7607 consistent gets0 physical reads0 redo size526 bytes sent via SQL*Net to client524 bytes received via SQL*Net from client2 SQL*Net roundtrips to/from client0 sorts (memory)0 sorts (disk)1 rows processed
SQL>Execution Plan----------------------------------------------------------Plan hash value: 2756549272------------------------------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |------------------------------------------------------------------------------------------------| 0 | SELECT STATEMENT | | 1 | 16 | 1 (0)| 00:00:01 || 1 | SORT AGGREGATE | | 1 | 16 | | ||* 2 | TABLE ACCESS BY INDEX ROWID| RESOUCES | 1 | 16 | 1 (0)| 00:00:01 ||* 3 | INDEX RANGE SCAN | IDX_RES_PASSL | 1 | | 1 (0)| 00:00:01 |------------------------------------------------------------------------------------------------Predicate Information (identified by operation id):---------------------------------------------------2 - filter("T"."STATUS"=0 OR "T"."STATUS"=1 OR "T"."STATUS"=2 OR "T"."STATUS"=3)3 - access("T"."PASSWORDLOCK"=1)Statistics----------------------------------------------------------1 recursive calls0 db block gets1 consistent gets0 physical reads0 redo size526 bytes sent via SQL*Net to client524 bytes received via SQL*Net from client2 SQL*Net roundtrips to/from client0 sorts (memory)0 sorts (disk)1 rows processed
select count(id) from resources t where t.passwordlock is not null;
SQL>Execution Plan----------------------------------------------------------Plan hash value: 942951535------------------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |------------------------------------------------------------------------------------| 0 | SELECT STATEMENT | | 1 | 13 | 0 (0)| 00:00:01 || 1 | SORT AGGREGATE | | 1 | 13 | | ||* 2 | INDEX FULL SCAN| IDX_RES_PASSL | 1 | 13 | 0 (0)| 00:00:01 |------------------------------------------------------------------------------------Predicate Information (identified by operation id):---------------------------------------------------2 - filter("T"."PASSWORDLOCK" IS NOT NULL)Statistics----------------------------------------------------------1 recursive calls0 db block gets1 consistent gets0 physical reads0 redo size526 bytes sent via SQL*Net to client524 bytes received via SQL*Net from client2 SQL*Net roundtrips to/from client0 sorts (memory)0 sorts (disk)1 rows processed
文章转载自skylines,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




