drop table t;
create table t(n number, client_id varchar2(30) not null);
create or replace trigger t_iuc for insert or update on t
compound trigger
cli varchar2(30);
before statement is
begin
cli := SYS_CONTEXT('userenv', 'CLIENT_IDENTIFIER');
end before statement;
before each row is
begin
:new.client_id := cli;
end before each row;
end;
/
exec dbms_session.set_identifier('rafu')
insert into t(n) select level from dual connect by level < 4;
select * from t;
1 rafu
2 rafu
3 rafu
2012-01-10
Using client identifier to populate a column
Earlier I wrote about missing dbms_session.get_client_identifier. Here is a compound trigger example using session client identifier to populate a column in a table.
2012-01-04
Constraint name generated
Generated constraint names makes development, testing and production environment error messages vary. And those reported SYS_C006206630 could be more informative. To identify those generated constraint names *_constraints views has the information available at generated column. The constraint search_condition is a long typed column and thou hard to use. Working with long columns document gives tools to convert the search column to varchar2. This query reports generated named constraints in users schema.
Give names to the reported constraints to your modeling tool. NOT NULL checks are not first in the list to be named. NOT NULL error messages are informative with the generated names also. But naming also those make the different (DEV,TST,PRD) schemas a bit more similar. SQL Developer Data modeler 3.1 EA3 has the possibility to name those also at relational model. Formerly it was only possible at physical model. If you feel like too much work modeling those at least change the reported names.
WITH xml AS (
SELECT DBMS_XMLGEN.GETXMLTYPE(
'select co.constraint_name
, co.table_name
, cc.column_name
, co.constraint_type
, co.r_constraint_name
, co.search_condition
from user_constraints co
left outer join user_cons_columns cc
on co.constraint_name = cc.constraint_name
where co.generated = ''GENERATED NAME''')
AS xml
FROM dual
), concol as (
SELECT extractValue(xs.object_value, '/ROW/TABLE_NAME') AS table_name
, extractValue(xs.object_value, '/ROW/COLUMN_NAME') AS column_name
, extractValue(xs.object_value, '/ROW/CONSTRAINT_TYPE') AS constraint_type
, extractValue(xs.object_value, '/ROW/CONSTRAINT_NAME') AS constraint_name
, extractValue(xs.object_value, '/ROW/R_CONSTRAINT_NAME') AS r_constraint_name
, extractValue(xs.object_value, '/ROW/SEARCH_CONDITION') AS search_condition
FROM xml x
, TABLE(XMLSEQUENCE(EXTRACT(x.xml, '/ROWSET/ROW'))) xs
)
select cc.*
from concol cc
order by table_name, column_name,constraint_type
;
Give names to the reported constraints to your modeling tool. NOT NULL checks are not first in the list to be named. NOT NULL error messages are informative with the generated names also. But naming also those make the different (DEV,TST,PRD) schemas a bit more similar. SQL Developer Data modeler 3.1 EA3 has the possibility to name those also at relational model. Formerly it was only possible at physical model. If you feel like too much work modeling those at least change the reported names.
ALTER TABLE test RENAME CONSTRAINT SYS_C006206630 TO test_pk;
2011-12-28
unindex 11.2 cross schema
Foreign keys might point to another schema. Here is an unindex query for such situation. My earlier queries unindex and unindex 11.2 were built on top of user_* views. Here the queries are using all_* views. I am not using dba_* views as all_* views are usable for wider audience than just dba privileged.
Reminder: you probably do not need all of those, but they might explain you some poor execution times or TM lock waits.
select case when i.index_name is not null
then 'OK'
else '****'
end ok
, c.owner
, c.table_name
, c.constraint_name
, c.cols
, i.index_name
from (
select a.owner
, a.table_name
, a.constraint_name
, listagg(b.column_name, ' ' )
within group (order by column_name) cols
from all_constraints a, all_cons_columns b
where a.owner = b.owner
and a.constraint_name = b.constraint_name
and a.constraint_type = 'R'
group by a.owner,a.table_name, a.constraint_name
) c
left outer join
(
select index_owner
, table_name
, index_name
, cr
, listagg(column_name, ' ' )
within group (order by column_name) cols
from (
select index_owner
, table_name
, index_name
, column_position
, column_name
, connect_by_root(column_name) cr
from all_ind_columns
connect by prior column_position-1 = column_position
and prior index_name = index_name
and prior index_owner = index_owner
)
group by index_owner, table_name, index_name, cr
) i on c.owner = i.index_owner and c.cols = i.cols and c.table_name = i.table_name
order by ok,owner,table_name,constraint_name;
2011-12-20
The index could be unique
drop table uq; create table uq(pk int constraint uq_pk primary key, col int, col2 int); create index uq_col_idx on uq(col,pk);Index uq_col_idx is not unique but it might be defined as such. It contains all columns in a unique primary key. To find such candidates is just another relational division question.
with co as (
select con.constraint_name
, con.table_name
, cco.column_name
, count(*) over (partition by con.constraint_name) ccc
from user_constraints con
inner join user_cons_columns cco
on cco.constraint_name=con.constraint_name
where con.constraint_type in ('P','U')
and con.deferrable = 'NOT DEFERRABLE'
), ix as (
select idx.table_name,idx.index_name,ico.column_name,idx.uniqueness
from user_indexes idx
inner join user_ind_columns ico
on idx.index_name = ico.index_name
where exists (
select 0 from user_tab_columns col
inner join user_ind_columns coi
on coi.table_name = col.table_name
and coi.column_name = col.column_name
where idx.index_name = coi.index_name
and col.nullable = 'N'
)
), ixco as (
select ix.table_name
, ix.index_name
, ix.column_name
, co.constraint_name
, co.ccc
, count(*) over (partition by co.constraint_name,ix.index_name) ixc
, ix.uniqueness
from co, ix
where co.table_name=ix.table_name
and co.column_name=ix.column_name
)
select distinct table_name,index_name could_be_unique,constraint_name based_on
from ixco
where uniqueness = 'NONUNIQUE'
and ccc=ixc
order by table_name,index_name
;
TABLE_NAME COULD_BE_UNIQUE BASED_ON
------------------------------ ------------------------------ ------------------------------
UQ UQ_COL_IDX UQ_PK
drop index uq_col_idx; create unique index uq_col_idx on uq(col,pk);
2011-12-14
A table vanishing from a plan
Christian Antognini published his presentation slides Challenges and Chances of the 11g Query Optimizer. Worth reading to get a nice overview.
The 11g new feature "join elimination" is mentioned there. There is a similar behavior even in 10gR2 optimizer. A table mentioned in a query text vanishes from the query plan. It is not a join but a correlated sub query with a aggregate.
select 1
from dual mai
where exists (
select max(1)
from dual inn
where mai.dummy=inn.dummy
)
;
---------------------------------
| Id | Operation | Name |
---------------------------------
| 0 | SELECT STATEMENT | |
| 1 | FAST DUAL | |
---------------------------------
The sub query and the "inn" table is not seen in the plan. There is no need as the aggregate returns a row. This is not the case in my earlier post about not exists null. There the sub query is not correlated and it is using not exists.
The not exists correlated makes similar elimination. But in addition adds a filter(NULL IS NOT NULL) just before entering even the first table.
select 1
from dual mai
where not exists (
select max(1)
from dual inn
where mai.dummy=inn.dummy
)
;
---------------------------------
| Id | Operation | Name |
---------------------------------
| 0 | SELECT STATEMENT | |
|* 1 | FILTER | |
| 2 | FAST DUAL | |
---------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter(NULL IS NOT NULL)
One could ask, why would somebody write such a query...
2011-11-25
A Question
I attended the Jonathan Lewis seminar three weeks ago and wrote just after that about information overload. The things learned there have helped me understanding issues I have to deal with the Oracle optimizer. The amount of information gained need some time to melt into mind. One issue talked there was a question.
The original written question was "How many choises does Oracle have in this example?" With addition there was mentioned "for all versions".
drop table t1; create table t1( n1 number , n2 number , n3 number , constraint t1p primary key(n1) , constraint t1u unique (n2)); select count(*) from t1;I answered wrongly mentioning also that it depends on the version. As so often my mind started to think howcome I answered such an answer. Maybe I was thinking a question: How many different execution plan possibilities does Oracle have in this example? Unfortunately I am not attending UKOUG to throw this to the OakTable Challenge. Feel free to use "Give me at least one that was not in the list of the Jonathan's answer and how to get there." I will be coming back to the issue after 7th of December.
2011-11-23
Filtering outer join to hash join plan
I gave a SQL presentation a couple of years ago.
There I mentioned the possibility to restrict an outer join when using ANSI joins.
Every now and then seen such an join condition. Maybe even written myself.
This week such a query has been bothering me with a long running nested loops access path. This was a simpler query than the one with outer join exists predicate I have mentioned earlier.
While dealing with it I saw a quite funny looking predicate.
drop table aa;
drop table bb;
create table aa as
select rownum id
, nullif(mod(rownum,3),1) val
from dual connect by level < 12;
create table bb as
select rownum id
, nullif(mod(rownum,3),0)+10 val
from dual connect by level < 9;
alter table aa modify id not null;
alter table bb modify id not null;
select aa.* from aa;
ID VAL
---------- ----------
1
2 2
3 0
4
5 2
6 0
7
8 2
9 0
10
11 2
select bb.* from bb;
ID VAL
---------- ----------
1 11
2 12
3
4 11
5 12
6
7 11
8 12
Desired results:
ID VAL ID VAL
---------- ---------- ---------- ----------
1
2 2 2 12
3 0
4
5 2 5 12
6 0
7
8 2 8 12
9 0
10
11 2
A normal outer join uses hash join plan. Adding the predicate aa.val = 2 to the join condition I get the result I want.
select *
from aa
left outer join bb
on aa.id=bb.id
;
-----------------------------------
| Id | Operation | Name |
-----------------------------------
| 0 | SELECT STATEMENT | |
|* 1 | HASH JOIN OUTER | |
| 2 | TABLE ACCESS FULL| AA |
| 3 | TABLE ACCESS FULL| BB |
-----------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - access("AA"."ID"="BB"."ID")
select *
from aa
left outer join bb
on aa.id=bb.id and aa.val = 2
;
-------------------------------------
| Id | Operation | Name |
-------------------------------------
| 0 | SELECT STATEMENT | |
| 1 | NESTED LOOPS OUTER | |
| 2 | TABLE ACCESS FULL | AA |
| 3 | VIEW | |
|* 4 | FILTER | |
|* 5 | TABLE ACCESS FULL| BB |
-------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
4 - filter("AA"."VAL"=2)
5 - filter("AA"."ID"="BB"."ID")
But the 11.1.0.7 optimizer goes to a nested loops path.
My problem is that the table BB is kind of big.
Together with plan row 4 filter is not filtering out so many rows the execution is full table scanning the table bb too many times.
Throwing the same query to 11.2.0.3 optimizer I get a hash join access path. Nice. That was something I was hoping for. But look at the access predicate.
-----------------------------------
| Id | Operation | Name |
-----------------------------------
| 0 | SELECT STATEMENT | |
|* 1 | HASH JOIN OUTER | |
| 2 | TABLE ACCESS FULL| AA |
| 3 | TABLE ACCESS FULL| BB |
-----------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - access("AA"."ID"="BB"."ID" AND "AA"."VAL"=CASE WHEN ("BB"."ID"
IS NOT NULL) THEN 2 ELSE 2 END )
Obviously the outer join (+) are missing next to BB.ID. You need to read it from the word OUTER next to the HASH JOIN.
Sqldeveloper is able to visualize the predicate differently.
AA.ID=BB.ID(+) AND AA.VAL=CASE WHEN (BB.ID(+) IS NOT NULL) THEN 2 ELSE 2 ENDBut that case statement seems kind of weird. "If something then constant otherwise the same constant". Should that case statement not be possible to be optimized to be a simple constant 2? I rewrote my query to use the old style join. And with 11.2.0.3 the plan and results stay the same. But going back to 11.1.0.7 things change.
select aa.*,bb.*
from aa, bb
where aa.id=bb.id(+)
and aa.val = case when (bb.id(+) is not null) then 2 else 2 end
;
-----------------------------------
| Id | Operation | Name |
-----------------------------------
| 0 | SELECT STATEMENT | |
|* 1 | HASH JOIN OUTER | |
|* 2 | TABLE ACCESS FULL| AA |
| 3 | TABLE ACCESS FULL| BB |
-----------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - access("AA"."ID"="BB"."ID")
2 - filter("AA"."VAL"=2)
The case statement is optimized as a simple constant and used as a filter staright to the table AA.
Query results change compared to 11.2.0.3 results.
Actually the thing I wanted out is without the else part in the predicate.
select aa.*,bb.*
from aa, bb
where aa.id=bb.id(+)
and aa.val = case when (bb.id(+) is not null) then 2 end
;
-----------------------------------
| Id | Operation | Name |
-----------------------------------
| 0 | SELECT STATEMENT | |
|* 1 | HASH JOIN OUTER | |
| 2 | TABLE ACCESS FULL| AA |
| 3 | TABLE ACCESS FULL| BB |
-----------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - access("AA"."ID"="BB"."ID" AND "AA"."VAL"=CASE WHEN ("BB"."ID"
IS NOT NULL) THEN 2 END )
The results are what I was requesting.
This may be rewoten back to ANSI join that is understood also by 11.1.0.7 optimizer and the execution goes to a hash join path.
select aa.*,bb.*
from aa
left outer join bb
on aa.id=bb.id
and aa.val = case when (bb.id is not null) then 2 end
;
I am satisfied with the results. Well throw another problem to the optimizers. How about nulls.
Change the "and aa.val =" to "and aa.val is null" and even 11.2.0.3 is not able to go to a hash join plan. Here is an problematic nested loops access path query and a dirty trick on a way to a hash path.
select aa.*,bb.*
from aa
left outer join bb
on aa.id=bb.id
and aa.val is null
;
select *
from aa
left outer join bb
on aa.id=bb.id
and coalesce(nullif(aa.val,bb.id),aa.val) is null
;
Subscribe to:
Posts (Atom)
About Me
- Rafu
- I am Timo Raitalaakso. I have been working since 2001 at Solita Oy as a Senior Database Specialist. My main focus is on projects involving Oracle database. Oracle ACE alumni 2012-2018. In this Rafu on db blog I write some interesting issues that evolves from my interaction with databases. Mainly Oracle.