oracle like不用索引,Oracle优化——LIKE与索引(以%开头的LIKE会不走索引或走索引全扫描)...

这样会走索引范围扫描,因为这个表达式有前导性。

SH@ prod> set autotrace on

SH@ prod> select max(cust_credit_limit) , count(*) from customers_ne where cust_last_name like 'Vaugh%' ;

MAX(CUST_CREDIT_LIMIT) COUNT(*)

---------------------- ----------

11000 81

Execution Plan

----------------------------------------------------------

Plan hash value: 3473995898

---------------------------------------------------------------------------------------------

| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |

---------------------------------------------------------------------------------------------

| 0 | SELECT STATEMENT | | 1 | 16 | 13 (0)| 00:00:01 |

| 1 | SORT AGGREGATE | | 1 | 16 | | |

| 2 | TABLE ACCESS BY INDEX ROWID| CUSTOMERS_NE | 61 | 976 | 13 (0)| 00:00:01 |

|* 3 | INDEX RANGE SCAN | LAST_IDX1 | 61 | | 2 (0)| 00:00:01 |

---------------------------------------------------------------------------------------------

Predicate Information (identified by operation id):

---------------------------------------------------

3 - access("CUST_LAST_NAME" LIKE 'Vaugh%')

filter("CUST_LAST_NAME" LIKE 'Vaugh%')

Statistics

----------------------------------------------------------

0 recursive calls

0 db block gets

14 consistent gets

0 physical reads

0 redo size

614 bytes sent via SQL*Net to client

523 bytes received via SQL*Net from client

2 SQL*Net roundtrips to/from client

0 sorts (memory)

0 sorts (disk)

1 rows processed

这样不会走索引,因为这个表达式没有前导性,无法找到索引的开始点。

SH@ prod> select max(cust_credit_limit) , count(*) from customers_ne where cust_last_name like '%aughn' ;

MAX(CUST_CREDIT_LIMIT) COUNT(*)

---------------------- ----------

11000 81

Execution Plan

----------------------------------------------------------

Plan hash value: 3963802310

-----------------------------------------------------------------------------------

| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |

-----------------------------------------------------------------------------------

| 0 | SELECT STATEMENT | | 1 | 16 | 405 (1)| 00:00:05 |

| 1 | SORT AGGREGATE | | 1 | 16 | | |

|* 2 | TABLE ACCESS FULL| CUSTOMERS_NE | 2775 | 44400 | 405 (1)| 00:00:05 |

-----------------------------------------------------------------------------------

Predicate Information (identified by operation id):

---------------------------------------------------

2 - filter("CUST_LAST_NAME" LIKE '%aughn')

Statistics

----------------------------------------------------------

8 recursive calls

0 db block gets

1460 consistent gets

0 physical reads

0 redo size

614 bytes sent via SQL*Net to client

523 bytes received via SQL*Net from client

2 SQL*Net roundtrips to/from client

0 sorts (memory)

0 sorts (disk)

1 rows processed

强制使用索引,走的时索引全扫描,还远不如全表扫描。

SH@ prod> select /*+ index(customers_ne) */ max(cust_credit_limit) , count(*) from customers_ne where cust_last_name like '%aughn' ;

MAX(CUST_CREDIT_LIMIT) COUNT(*)

---------------------- ----------

11000 81

Execution Plan

----------------------------------------------------------

Plan hash value: 359032907

---------------------------------------------------------------------------------------------

| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |

---------------------------------------------------------------------------------------------

| 0 | SELECT STATEMENT | | 1 | 16 | 636 (1)| 00:00:08 |

| 1 | SORT AGGREGATE | | 1 | 16 | | |

| 2 | TABLE ACCESS BY INDEX ROWID| CUSTOMERS_NE | 2775 | 44400 | 636 (1)| 00:00:08 |

|* 3 | INDEX FULL SCAN | LAST_IDX1 | 2775 | | 143 (1)| 00:00:02 |

---------------------------------------------------------------------------------------------

Predicate Information (identified by operation id):

---------------------------------------------------

3 - filter("CUST_LAST_NAME" LIKE '%aughn')

Statistics

----------------------------------------------------------

8 recursive calls

0 db block gets

155 consistent gets

139 physical reads

0 redo size

614 bytes sent via SQL*Net to client

523 bytes received via SQL*Net from client

2 SQL*Net roundtrips to/from client

0 sorts (memory)

0 sorts (disk)

1 rows processed

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值