begin
dbms_stats.gather_table_stats(user, 'tq84_tab');
end;
/
Show the execution plan for a string comparison (num = '42'). The index is used:
explain plan for select xyz from tq84_tab where num = '42';
select * from table(dbms_xplan.display(format=>'basic predicate'));
--
-- ---------------------------------------------------
-- | Id | Operation | Name |
-- ---------------------------------------------------
-- | 0 | SELECT STATEMENT | |
-- | 1 | TABLE ACCESS BY INDEX ROWID| TQ84_TAB |
-- |* 2 | INDEX UNIQUE SCAN | TQ84_TAB_IX |
-- ---------------------------------------------------
--
-- Predicate Information (identified by operation id):
-- ---------------------------------------------------
--
-- 2 - access("NUM"='42')
Almost the same thing, but the comparison uses a number (num = 42). The index is not used. The reason for not using the index is that behind the scenes, the query planner reformulates the predicate to to_number(num)=42. (Note also how the predicate information changed from access(…) to filter(…)):
explain plan for select xyz from tq84_tab where num = 42;
select * from table(dbms_xplan.display(format=>'basic predicate'));
--
-- --------------------------------------
-- | Id | Operation | Name |
-- --------------------------------------
-- | 0 | SELECT STATEMENT | |
-- |* 1 | TABLE ACCESS FULL| TQ84_TAB |
-- --------------------------------------
--
-- Predicate Information (identified by operation id):
-- ---------------------------------------------------
--
-- 1 - filter(TO_NUMBER("NUM")=42)