I have table called result
:
CREATE TABLE result
(id_graduate NUMBER NOT NULL,
id_sml NUMBER NOT NULL,
last_update DATE NOT NULL,
id_kt NUMBER NOT NULL,
value_kt NUMBER NOT NULL,
id_res NUMBER NOT NULL)
/
ALTER TABLE result
ADD CONSTRAINT pk_id_res PRIMARY KEY (id_res)
And an index:
CREATE UNIQUE INDEX x_pk_id_res ON result
(
id_res ASC
)
STORAGE (
INITIAL 65536
MINEXTENTS 1
MAXEXTENTS 2147483645
)
NOPARALLEL
LOGGING
/
When I run SQL query #1, Oracle uses index X_PK_ID_RES
select ss.*
from V_JOURNAL ss, result r
where TO_NUMBER(ss.ID_SML||ss.ID_KT||ss.ID_GRADUATE)=r.id_res
But, when I run SQL query #2, Oracle does not use index X_PK_ID_RES
. Instead it uses an ACCESS FULL TABLE
access path.
select ss.*,r.*
from V_JOURNAL ss, result r
where TO_NUMBER(ss.ID_SML||ss.ID_KT||ss.ID_GRADUATE)=r.id_res
So, I added a new column id_res2
:
ALTER TABLE RESULT ADD ( ID_RES2 NUMBER );
And I created a new index:
CREATE UNIQUE INDEX x_id_res2 ON result
(
id_res2 ASC
)
STORAGE (
INITIAL 65536
MINEXTENTS 1
MAXEXTENTS 2147483645
)
NOPARALLEL
LOGGING
When I run SQL query #3, Oracle uses index X_ID_RES2
.
select ss.*
from V_JOURNAL ss, result r
where TO_NUMBER(ss.ID_SML||ss.ID_KT||ss.ID_GRADUATE)=r.id_res2
And, when I run SQL query #4, Oracle uses index X_ID_RES2
too.
select ss.*,r.*
from V_JOURNAL ss, result r
where TO_NUMBER(ss.ID_SML||ss.ID_KT||ss.ID_GRADUATE)=r.id_res2
Why did Oracle use an index in SQL #4, but did not use one in SQL #2?
result.id_res
? You may be better off creating a functional index onTO_NUMBER(ss.ID_SML||ss.ID_KT||ss.ID_GRADUATE)
id_res
, notid_res2
... but it's not clear if it has the PK or UI against it (since they can't both be there against the same column as written). I mentioned on the SO version that it might have switched from a full index scan to a full table scan because it's selectingr.*
in query 2; but awaiting the execution plans to see why query 4 didn't do the same... I'm guessing stats, or becauseid_res2
is nullable. Maybe.exec dbms_stats.gather_table_stats(user, 'RESULT', cascade=>true);
will gather table and index stats.