After executing a join over two tables, whith 4 million rows each and indexes in join columns, I realized that 4 minutes it's too much time waiting for results. So I decided to watch for the execution plan :
select columns from tableA join TableB on tableA.id = tableB.id
Execution Plan----------------------------------------------------------
0 SELECT STATEMENT Optimizer=RULE
1 0 FILTER
2 1 SORT (GROUP BY)
3 2 TABLE ACCESS (BY INDEX ROWID) OF 'TABLEA'
4 3 NESTED LOOPS
5 4 TABLE ACCESS (FULL) OF 'TABLEB'
6 4 INDEX (RANGE SCAN) OF 'TABLEA_U1' (UNIQUE)
All seems to be correct, after all I'm using Rule Base Optimizer. In order to reduce execution time I force the execution of an hash join instead of a Nested Loops.
Execution time droped to about 2 minutes and execution plan looks like this :
Execution Plan----------------------------------------------------------
0 SELECT STATEMENT Optimizer=RULE (Cost=624 Card=40000 Bytes=2 800000)
1 0 FILTER
2 1 SORT (GROUP BY) (Cost=624 Card=40000 Bytes=2800000)
3 2 HASH JOIN (Cost=172 Card=40000 Bytes=2800000)
4 3 TABLE ACCESS (FULL) OF 'TABLEA' (Cost=35 Card=2000 Bytes=62000)
5 3 TABLE ACCESS (FULL) OF 'TABLEB' (Cost=136 Card=2000 Bytes=78000)
And all I've to do is change my query to :
select /*+ USE_HASH (tableA tableB ) */ columns from tableA join TableB on tableA.id = tableB.id
Why is this happening? why RBO this choose this plan? The answer is simple, RBO doesn't consider hash joins has a valid execution path, so you have to force it.
This specific join is used when a large amount of data needs to be joined or when a large fraction of the the table needs to be joined, however this is a very memory expensive operation and need to be carefully analised.
Showing posts with label PL/SQL. Show all posts
Showing posts with label PL/SQL. Show all posts
Wednesday, January 7, 2009
PL/SQL Tip - 1
Query v$parameter view for BD parameters checking.
In this example I'm looking for the selected optimizer mode.
select value from v$parameter where name = 'optimizer_mode';
Enjoy.
In this example I'm looking for the selected optimizer mode.
select value from v$parameter where name = 'optimizer_mode';
Enjoy.
Labels:
PL/SQL
PL/SQL : Show locked objects
Today, after trying unsuccessfully to drop my temporary tables, and subsequent fails due to object locks, I tried to find who's locking my table.
Not an easy task, so I dig a little and found this amazing script that solved my problem. If you have the same problem, read here --> http://www.orafaq.com/scripts/performance/lockall.txt how to list all locked objects.
Not an easy task, so I dig a little and found this amazing script that solved my problem. If you have the same problem, read here --> http://www.orafaq.com/scripts/performance/lockall.txt how to list all locked objects.
Labels:
PL/SQL
PL/SQL : How to update using select
This "problem" came across me when I needed to update an entire temporary table column. One option is to loop all table rows, boring and time consumer task. After some background searchs I've adopted this method:
UPDATE TABLEA SET
(TABLEA.COLUMNA, TABLEA.COLUMNB) =
(SELECT TABLEB.COLUMNA, TABLEB.COLUMNB FROM TABLEB WHERE TABLEB.ID = TABLEA.ID)
I've basically do one inner select and join the two tables in the inner select's WHERE clause.
UPDATE TABLEA SET
(TABLEA.COLUMNA, TABLEA.COLUMNB) =
(SELECT TABLEB.COLUMNA, TABLEB.COLUMNB FROM TABLEB WHERE TABLEB.ID = TABLEA.ID)
I've basically do one inner select and join the two tables in the inner select's WHERE clause.
Labels:
PL/SQL
PL/SQL : Scripting for table drop
I came from MS SQLServer and I miss a lot of nice functionalities, like for instance, If exists.
Why am I talking about this? Simple, I want to run a script that "only" drops a few tables that I'don't no if they already exists.
Easy to say, hard to find. So here's a script for doing that, without returning any errors.
set echo off;
set heading off;
spool run.sql;
select 'drop table 'table_name';' from dba_tables where UPPER(table_name) in ('TABLEA', 'TABLEB', 'TABLEC');
spool off;
@run.sql;
Enjoy.
Why am I talking about this? Simple, I want to run a script that "only" drops a few tables that I'don't no if they already exists.
Easy to say, hard to find. So here's a script for doing that, without returning any errors.
set echo off;
set heading off;
spool run.sql;
select 'drop table 'table_name';' from dba_tables where UPPER(table_name) in ('TABLEA', 'TABLEB', 'TABLEC');
spool off;
@run.sql;
Enjoy.
Labels:
PL/SQL
PL/SQL : Check script for object owners
In real life development environments, you develope a script logged as UserX, and someone in Support Team will install it in production environment logged as UserZ.
So far, nothing unusual happens. But, if we are talking about oracle scripts, there's something that you have to deal with, Schemas, If you didn't reference all you objects with full name, SchemaX.TableA for instance, when UserZ tries to install it, he will not find TableA in is schema.
In simple scripts, with few lines, it's easy to manually check for errors, but try to do it in a scripy with thousand of lines...
So I've developed this procedure to "parse" my code looking for this kind of errors
FUNCTION CHECK_OWNER_IN_SQL(
p_list varchar2,
p_del varchar2
) return boolean
is
l_idx pls_integer;
l_list varchar2(32767) := p_list;
l_value varchar2(32767);
keyword varchar2(10000);
existsObject integer;
result boolean;
begin
result := false;
loop
l_idx := instr(l_list,p_del);
if l_idx > 0 then
keyword := substr(l_list,1,l_idx-1);
select count(*) into existsObject from user_objects where lower(object_name) = lower(keyword);
if existsObject > 0 then
DBMS_OUTPUT.Put_Line( 'Possible error in ' keyword);
result := true;
end if;
l_list := substr(l_list,l_idx+length(p_del));
else
keyword := l_list;
select count(*) into existsObject from user_objects where lower(object_name) = lower(keyword);
if existsObject > 0 then
DBMS_OUTPUT.Put_Line( 'Possible error in ' keyword );
result := true;
end if;
exit;
end if;
end loop;
return result;
end;
--Possible values for p_type are :
-- DATABASE LINK,FUNCTION,INDEX,PACKAGE,PACKAGE BODY,PROCEDURE,SEQUENCE,SYNONYM,TABLE,TRIGGER,TYPE,VIEW
PROCEDURE CHECK_OWNER(
p_owner IN varchar2,
p_type IN varchar2,
p_name IN varchar2,
p_sql IN varchar2 := NULL
)
IS
cursor lines
is
Select text from all_source where lower(owner) = lower(p_owner) and lower(type) = lower(p_type) and lower(name) = lower(p_name) order by line;
sqltext varchar2(32000);
result boolean;
linha integer;
begin
if p_sql is not null then
result := siebel.check_owner_in_sql(p_sql , ' ');
else
linha := 1;
for line in lines loop
sqltext := TRIM(line.text);
IF substr(sqltext,1,2) <> '--' then
if siebel.check_owner_in_sql(sqltext , ' ') then
DBMS_OUTPUT.Put_Line( 'Linha ' linha ': ' sqltext);
end if;
end if;
linha := linha + 1;
end loop;
if linha = 1 then
DBMS_OUTPUT.Put_Line( 'Não foi encontrado o objecto a validar');
end if;
end if;
end;
After create this two procedures, you have only to execute the last one, and watch for possible warnings in your output window.
So far, nothing unusual happens. But, if we are talking about oracle scripts, there's something that you have to deal with, Schemas, If you didn't reference all you objects with full name, SchemaX.TableA for instance, when UserZ tries to install it, he will not find TableA in is schema.
In simple scripts, with few lines, it's easy to manually check for errors, but try to do it in a scripy with thousand of lines...
So I've developed this procedure to "parse" my code looking for this kind of errors
FUNCTION CHECK_OWNER_IN_SQL(
p_list varchar2,
p_del varchar2
) return boolean
is
l_idx pls_integer;
l_list varchar2(32767) := p_list;
l_value varchar2(32767);
keyword varchar2(10000);
existsObject integer;
result boolean;
begin
result := false;
loop
l_idx := instr(l_list,p_del);
if l_idx > 0 then
keyword := substr(l_list,1,l_idx-1);
select count(*) into existsObject from user_objects where lower(object_name) = lower(keyword);
if existsObject > 0 then
DBMS_OUTPUT.Put_Line( 'Possible error in ' keyword);
result := true;
end if;
l_list := substr(l_list,l_idx+length(p_del));
else
keyword := l_list;
select count(*) into existsObject from user_objects where lower(object_name) = lower(keyword);
if existsObject > 0 then
DBMS_OUTPUT.Put_Line( 'Possible error in ' keyword );
result := true;
end if;
exit;
end if;
end loop;
return result;
end;
--Possible values for p_type are :
-- DATABASE LINK,FUNCTION,INDEX,PACKAGE,PACKAGE BODY,PROCEDURE,SEQUENCE,SYNONYM,TABLE,TRIGGER,TYPE,VIEW
PROCEDURE CHECK_OWNER(
p_owner IN varchar2,
p_type IN varchar2,
p_name IN varchar2,
p_sql IN varchar2 := NULL
)
IS
cursor lines
is
Select text from all_source where lower(owner) = lower(p_owner) and lower(type) = lower(p_type) and lower(name) = lower(p_name) order by line;
sqltext varchar2(32000);
result boolean;
linha integer;
begin
if p_sql is not null then
result := siebel.check_owner_in_sql(p_sql , ' ');
else
linha := 1;
for line in lines loop
sqltext := TRIM(line.text);
IF substr(sqltext,1,2) <> '--' then
if siebel.check_owner_in_sql(sqltext , ' ') then
DBMS_OUTPUT.Put_Line( 'Linha ' linha ': ' sqltext);
end if;
end if;
linha := linha + 1;
end loop;
if linha = 1 then
DBMS_OUTPUT.Put_Line( 'Não foi encontrado o objecto a validar');
end if;
end if;
end;
After create this two procedures, you have only to execute the last one, and watch for possible warnings in your output window.
Labels:
PL/SQL
WHy MINUS does NOT EXISTs in our queries?
Read this great (and old) article --> http://www.oraclemagician.com/mag/magic8.pdf about MINUS vs NOT EXISTS operators.
There really isn’t just one right way to design queries. In some cases, you really are better off using Not Exists. In many cases, however, you should use the set operator MINUS. Once you understand the principles, you can easily choose the best method for your particular case.
Labels:
PL/SQL
Subscribe to:
Posts (Atom)
Archive
-
▼
2009
(21)
-
▼
January
(21)
- How to deploy files in a remote server?
- Oracle : Reduce your parsing times
- Oracle : SQLCODE and SQLERRM in Inserts
- SQL * Plus Tip - 1
- Oracle : Reducing join execution time
- PL/SQL Tip - 1
- PL/SQL : Show locked objects
- PL/SQL : How to update using select
- EIM : Shell execution order
- PL/SQL : Scripting for table drop
- EIM : DUP_RECORD_EXISTS after Update
- Localcooling . com
- How to relate a contact whit their accounts
- PL/SQL : Check script for object owners
- SQL Navigator : Code Templates
- S_POSTN_CON : Description
- Joins : Please talk ANSI SQL with me.
- WHy MINUS does NOT EXISTs in our queries?
- Get last file version
- Debugging in http://localhost:8082
- Error 452: Match column X not found in Y
-
▼
January
(21)
Translation by Google
Subscription Free
Categories
- ANSI SQL
- Back to Basics
- Best Practices
- Case Study
- cfg File
- Check In -Check Out
- Configuration
- Copy
- Debug
- Ecology
- EIM
- eScript
- Files
- Firewall
- Harvest
- How - To
- I Wish I Knew
- List of Values
- Logging
- Mapping
- Oracle
- PL/SQL
- Remote Server
- Siebel
- Siebel CRM
- Siebel Logs
- Siebel Tools
- Signals
- SQL Plus
- SQLNavigator
- Tunning
- Web Services
- Workflows