create table tq84_tab (
id integer not null,
val number(5,1) not null,
txt varchar2(10) not null
);
The table is filled with some random data. In every third row, the value of txt ends in XXX:
begin
dbms_random.seed(42);
end;
/
insert into tq84_tab
select
level,
dbms_random.value(1, 9999),
dbms_random.string('u', 7) ||
case mod(level, 3)
when 0 then 'XXX'
else dbms_random.string('u', 3)
end
from
dual connect by level <= 10000;
Gathering the statistics for the table
begin
dbms_stats.gather_table_stats(user, 'tq84_tab');
end;
/
The statistics are not sufficient to estimate the number of rows with substr(txt, -3) = 'XXX' correctly:
explain plan for
select count(*)
from
tq84_tab
where
substr(txt, -3) = 'XXX'
;
In order to assist the optimizer with better statistics, we create extended statistics.
select
dbms_stats.create_extended_stats(
user,
'tq84_tab',
'(substr(txt, -3))' -- Extensions must be put between parantheses!
) extended_stat_name
from
dual;