名称如 in_deviceid || '%';
绝对应避免使用带有 % 的完整表。
你错了。当您使用LIKE
运算符 as时,如果列上有任何索引'STRING-%'
,Oracle 将执行INDEX SCAN 。随着统计数据的更新,您应该不会看到Full Table Scan。
我们来看一个测试用例:
SQL> CREATE TABLE emp_new AS SELECT ename FROM emp;
Table created.
SQL>
SQL> EXEC DBMS_STATS.gather_table_stats('LALIT', 'EMP_NEW');
PL/SQL procedure successfully completed.
SQL>
LIKE ename||'%' :没有索引:
SQL> EXPLAIN PLAN FOR
2 SELECT * FROM emp_new WHERE ename LIKE ename||'%';
Explained.
SQL>
SQL> SELECT * FROM TABLE(dbms_xplan.display);
PLAN_TABLE_OUTPUT
-----------------------------------------------------------------------------
Plan hash value: 565523140
-----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 2 | 12 | 3 (0)| 00:00:01 |
|* 1 | TABLE ACCESS FULL| EMP_NEW | 2 | 12 | 3 (0)| 00:00:01 |
-----------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
PLAN_TABLE_OUTPUT
-----------------------------------------------------------------------------
1 - filter("ENAME" LIKE "ENAME"||'%')
13 rows selected.
SQL>
因此,正如预期的那样,Oracle 进行了全表扫描。
LIKE ename||'%' : WITH 索引:
SQL> CREATE INDEX idx_nm ON emp_new(ename);
Index created.
SQL>
SQL> EXEC DBMS_STATS.gather_table_stats('LALIT', 'EMP_NEW');
PL/SQL procedure successfully completed.
SQL>
SQL> EXPLAIN PLAN FOR
2 SELECT * FROM emp_new WHERE ename LIKE ename||'%';
Explained.
SQL>
SQL> SELECT * FROM TABLE(dbms_xplan.display);
PLAN_TABLE_OUTPUT
---------------------------------------------------------------------------
Plan hash value: 848277793
---------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 2 | 12 | 1 (0)| 00:00:01 |
|* 1 | INDEX FULL SCAN | IDX_NM | 2 | 12 | 1 (0)| 00:00:01 |
---------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
PLAN_TABLE_OUTPUT
---------------------------------------------------------------------------
1 - filter("ENAME" LIKE "ENAME"||'%')
13 rows selected.
SQL>
因此,您可以清楚地看到正在使用的索引。