In-depth understanding of Oracle indexes (18): functional index traps and how to avoid index contamination

Source: Internet
Author: User
(I) function index traps when using function indexes, you must re-build the function index after the function code is changed. Otherwise, Oracle will return an error result but will not be prompted for the following test: [plain] viewplaincopyprint? SPANstyleBACKGROUND-COLOR: rgb (102,102,102) hr @ ORCLdroptabletpurge; Tabledropped. hr @ ORCLcrea

(I) function index traps when using function indexes, be sure to re-create the function index after the function code is changed. Otherwise, Oracle will return an error result but will not prompt the following test: [plain] view plaincopyprint? SPANstyle = BACKGROUND-COLOR: rgb (102,102,102) hr @ ORCLdroptabletpurge; Tabledropped. hr @ ORCLcrea

(I) functional index traps

When using the function index, you must re-create the function index after the function code is changed. Otherwise, Oracle will return an error but will not prompt you.

The test is as follows:

[Plain]View plaincopyprint?

  1. Hr @ ORCL> drop table t purge;
  2. Table dropped.
  3. Hr @ ORCL> create table t (x number, y varchar2 (30 ));
  4. Table created.
  5. Hr @ ORCL> insert into t select rownum, rownum | 'A' from dual connect by rownum <1000;
  6. 999 rows created.
  7. Hr @ ORCL> ed
  8. Wrote file afiedt. buf
  9. 1 create or replace function f_david (p_value varchar2) return varchar2
  10. 2 deterministic is
  11. 3 begin
  12. 4 return p_value;
  13. 5 * end;
  14. 6
  15. 7/
  16. Function created.
  17. Hr @ ORCL> create index idx_f_david _t on t (f_david (y ));
  18. Index created.
  19. Hr @ ORCL> exec dbms_stats.gather_table_stats (ownname => 'hr', tabname => 'T', estimate_percent => 100, cascade => TRUE, no_invalidate => false );
  20. PL/SQL procedure successfully completed.
  21. Hr @ ORCL> select * from t where f_david (y) = '8a ';
  22. X Y
  23. ----------------------------------------
  24. 8 8a
  25. Hr @ ORCL> ed// What is the ed splplus command?
  26. Wrote file afiedt. buf
  27. 1 create or replace function f_david (p_value varchar2) return varchar2
  28. 2 deterministic is
  29. 3 begin
  30. 4 return p_value | 'B ';
  31. 5 * end;
  32. Hr @ ORCL>/
  33. Function created.
  34. /* At this time, the function f_david is not the one we know, but the query is still like this !!! */
  35. Hr @ ORCL> select * from t where f_david (y) = '8a ';
  36. X Y
  37. ----------------------------------------
  38. 8 8a
  39. /* The index reconstruction query has no records. This is the correct result */
  40. Hr @ ORCL> drop index idx_f_david _t;
  41. Index dropped.
  42. Hr @ ORCL> create index idx_f_david _t on t (f_david (y ));
  43. Index created.
  44. Hr @ ORCL> exec dbms_stats.gather_table_stats (ownname => 'hr', tabname => 'T', estimate_percent => 100, cascade => TRUE, no_invalidate => false );
  45. PL/SQL procedure successfully completed.
  46. Hr @ ORCL> select * from t where f_david (y) = '8a ';
  47. No rows selected

Hr @ ORCL> drop table t purge; Table dropped. hr @ ORCL> create table t (x number, y varchar2 (30); Table created. hr @ ORCL> insert into t select rownum, rownum | 'A' from dual connect by rownum <1000; 999 rows created. hr @ ORCL> edWrote file afiedt. buf 1 create or replace function f_david (p_value varchar2) return varchar2 2 deterministic is 3 begin 4 return p_value; 5 * end; 6 7/Function created. hr @ ORCL> create index idx_f_david _t on t (f_david (y); Index created. hr @ ORCL>Exec dbms_stats.gather_table_stats (ownname => 'hr', tabname => 'T', estimate_percent => 100, cascade => TRUE, no_invalidate => false ); // What does this sentence mean?PL/SQL procedure successfully completed. hr @ ORCL> select * from t where f_david (y) = '8a '; x y ---------- ------------------------------ 8 8ahr @ ORCL> edWrote file afiedt. buf 1 create or replace function f_david (p_value varchar2) return varchar2 2 deterministic is 3 begin 4 return p_value | 'B'; 5 * end; hr @ ORCL>/Function created. /* at this time, the function f_david is not the one we know, but the query is still like this !!! */Hr @ ORCL> select * from t where f_david (y) = '8a '; x y ---------- ---------------------------- 8 8a/* The index reconstruction query has no records. This is the correct result. */hr @ ORCL> drop Index idx_f_david _t; index dropped. hr @ ORCL> create index idx_f_david _t on t (f_david (y); Index created. hr @ ORCL> exec dbms_stats.gather_table_stats (ownname => 'hr', tabname => 'T', estimate_percent => 100, cascade => TRUE, no_invalidate => false ); PL/SQL procedure successfully completed. hr @ ORCL> select * from t where f_david (y) = '8a '; no rows selected


(Ii) Avoid index contamination

Here are two comments,

① Do not add a function before the field

For example:
To_char (start_time, 'yyyy. mm. dd') between '2017. 123' and '2017. 123'
And
Start_time between to_date ('192. 123', 'yyyy. mm. dd') and to_date ('192. 123', 'yyyy. mm. dd ')

It should be the second type at any time !!!

② Do not embed fields into expressions

For example:
Start_time + 7 <sysdate;
And
Start_time <sysdate-7


By David Lin
2013-06-06
Good Luck

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.