Home » RDBMS Server » Performance Tuning » Help SQL Tuning (Oracle 10.2.0.1 on Suse 10)
Help SQL Tuning [message #515431] Mon, 11 July 2011 09:25 Go to next message
nqtrung
Messages: 25
Registered: April 2007
Junior Member
I have 2 tables: table_A & table_B. Table_B have CODE column that containt MOSEQ column of table_A. And I execute SQL statement:

select * from table_A A
where exists (select * from table_B B where A.moseq=substr(B.code,length(B.code)-length(A.moseq)+1,length(A.moseq)));

This statement run very slowly (45 minutes). Could you help me, pls

Thanks
Re: Help SQL Tuning [message #515432 is a reply to message #515431] Mon, 11 July 2011 09:27 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
Since NOBODY can optimize SQL just by looking at it, we need a few more details.
http://www.orafaq.com/forum/m/433888/136107/#msg_433888
Please refer to URL above & be sure to provide the details requested:
1) DDL for all tables & indexes
2) EXPLAIN PLAN
3) output from SQL_TRACE & tkprof
Re: Help SQL Tuning [message #515434 is a reply to message #515432] Mon, 11 July 2011 09:32 Go to previous messageGo to next message
cookiemonster
Messages: 13958
Registered: September 2008
Location: Rainy Manchester
Senior Member
Highly likely that this is the main problem:
where A.moseq=substr(B.code,length(B.code)-length(A.moseq)+1,length(A.moseq)));

You need to explain that linkage with sample data.
Re: Help SQL Tuning [message #515443 is a reply to message #515431] Mon, 11 July 2011 10:22 Go to previous messageGo to next message
Michel Cadot
Messages: 68716
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
I should say, if I understand your condition:
where B.code like '%'||A.moseq

but this does not solve the performances problem, just tell me if my condition is correct (it is better to explain the query with words then to let us reverse engineer it with the errors and waste of time this implies).

In addition: "what are the types of both columns?"

Regards
Michel

[Updated on: Mon, 11 July 2011 10:23]

Report message to a moderator

Re: Help SQL Tuning [message #515498 is a reply to message #515431] Mon, 11 July 2011 23:15 Go to previous messageGo to next message
nqtrung
Messages: 25
Registered: April 2007
Junior Member
This is explain plan: VDCO_MT_DATA is table_A, SMS_SEND is table_B

  • Attachment: sql.JPG
    (Size: 47.23KB, Downloaded 967 times)

[Updated on: Mon, 11 July 2011 23:19]

Report message to a moderator

Re: Help SQL Tuning [message #515500 is a reply to message #515434] Mon, 11 July 2011 23:29 Go to previous messageGo to next message
nqtrung
Messages: 25
Registered: April 2007
Junior Member
cookiemonster wrote on Mon, 11 July 2011 09:32
Highly likely that this is the main problem:
where A.moseq=substr(B.code,length(B.code)-length(A.moseq)+1,length(A.moseq)));

You need to explain that linkage with sample data.


I have this condition because B.code='abcxyz'||A.moseq but both 'abcxyz' and A.moseq column don't fix length.
Re: Help SQL Tuning [message #515515 is a reply to message #515500] Tue, 12 July 2011 01:20 Go to previous messageGo to next message
Michel Cadot
Messages: 68716
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
You didn't answer to my questions.

Regards
Michel
Re: Help SQL Tuning [message #515531 is a reply to message #515431] Tue, 12 July 2011 02:30 Go to previous messageGo to next message
hkchital
Messages: 128
Registered: September 2008
Location: Singapore
Senior Member
If A.MOSEQ and B.CODE are both VARCHAR2 does it matter ?


Hemant K Chitale
Re: Help SQL Tuning [message #515538 is a reply to message #515531] Tue, 12 July 2011 02:51 Go to previous messageGo to next message
Michel Cadot
Messages: 68716
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
If they are numbers, does that matter?

Regards
Michel
Re: Help SQL Tuning [message #515634 is a reply to message #515538] Tue, 12 July 2011 10:00 Go to previous messageGo to next message
hkchital
Messages: 128
Registered: September 2008
Location: Singapore
Senior Member
If the columns are VARCHAR2, they are *not* right-padded with blanks. He doesn't have to use substr functions to compare the two values.

And, obviously, they are not NUMBER datatypes.


Hemant K Chitale
Re: Help SQL Tuning [message #515635 is a reply to message #515634] Tue, 12 July 2011 10:19 Go to previous messageGo to next message
cookiemonster
Messages: 13958
Registered: September 2008
Location: Rainy Manchester
Senior Member
Really don't see how you can say that with the (lack of) information available.
Re: Help SQL Tuning [message #515686 is a reply to message #515635] Tue, 12 July 2011 22:35 Go to previous messageGo to next message
rleishman
Messages: 3728
Registered: October 2005
Location: Melbourne, Australia
Senior Member
This might work. Note that it uses an undocumented function REVERSE(), so you use it at your own risk knowing that Oracle may cease to support it - or change its functionality - sometime in the future.
create index table_b_code_rev on table_b(reverse(code));

select * 
from table_A A
where exists (
    select * 
    from table_B B 
    where reverse(b.code) like reverse(a.code) || '%'
);
Re: Help SQL Tuning [message #515718 is a reply to message #515686] Wed, 13 July 2011 01:06 Go to previous messageGo to next message
nqtrung
Messages: 25
Registered: April 2007
Junior Member
rleishman wrote on Tue, 12 July 2011 22:35
This might work. Note that it uses an undocumented function REVERSE(), so you use it at your own risk knowing that Oracle may cease to support it - or change its functionality - sometime in the future.
create index table_b_code_rev on table_b(reverse(code));

select * 
from table_A A
where exists (
    select * 
    from table_B B 
    where reverse(b.code) like reverse(a.code) || '%'
);


Thanks rleishman so much Smile
Re: Help SQL Tuning [message #515732 is a reply to message #515686] Wed, 13 July 2011 02:32 Go to previous message
Michel Cadot
Messages: 68716
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
rleishman wrote on Wed, 13 July 2011 05:35
This might work. Note that it uses an undocumented function REVERSE(), so you use it at your own risk knowing that Oracle may cease to support it - or change its functionality - sometime in the future.
create index table_b_code_rev on table_b(reverse(code));

select * 
from table_A A
where exists (
    select * 
    from table_B B 
    where reverse(b.code) like reverse(a.code) || '%'
);


This was what I thought but it works ONLY if the columns are strings which we still don't know as OP refuse to answer.

Regards
Michel
Previous Topic: Reg Segment advisor
Next Topic: NVL is degraing performance, any alternative
Goto Forum:
  


Current Time: Sun Nov 24 21:37:24 CST 2024