If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > how to improve this query performance

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-20-03, 01:03
krajkumar6 krajkumar6 is offline
Registered User
 
Join Date: Sep 2003
Posts: 9
how to improve this query performance

hi,

Can u suggest a way to improve this query performance?
I have 2 tables T1,T2.
T1 has two numeric fields n1,n2 which together form the primary key.
T2 also has the fields n1,n2 which together reference the values of T1.

query:

select T1.n1,T1.n2
from T1,T2
where
char(T1.n1)||char(T1.n2) not in char(T2.n1)||char(T2.n2)

My problem is the this query takes a long time to complete as T1 has
over 5000 rows.


PS: I was told that there is a feature in db2 by which i can say
select (n1,n2) from T1,T2
where
(T1.n1,T1.n2) not in (T2.n1,T2.n2).
Can someone throw some light on that?

Thanks,
K Rajkumar
Reply With Quote
  #2 (permalink)  
Old 10-20-03, 01:22
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,198
I am not sure what your are trying to do, but it sounds like you want the rows in table1 which don’t exist in table2.

Select A.n1, A.n2
from T1 A
where not exists
(select B.n1 from T2 B
where A.n1 = B.n1 and A.n2 = B.n2)

Make sure T2 has a composite index on (n1, n2). There can be other columns in the index if needed, but n1 and n2 should be the first 2 columns of the index for best performance.
Reply With Quote
  #3 (permalink)  
Old 10-20-03, 01:23
grofaty grofaty is offline
Registered User
 
Join Date: Jan 2003
Posts: 1,570
Hi,

Try to write subselect like this:
select n1, n2 from T1 where (n1,n2) not in (select n1, n2 from T2)

I also suggest you to go through the How to improve the performance? thread already discussed in this forum.

Hope this helps,
Grofaty
Reply With Quote
  #4 (permalink)  
Old 10-20-03, 02:20
krajkumar6 krajkumar6 is offline
Registered User
 
Join Date: Sep 2003
Posts: 9
Grofaty,

I tried your query.But it is giving me a syntax error.
My db2 is Version: V5R1M0 010525
that runs on a AS/400 system.
Could that be a problem?

Reg,
K Rajkumar
Reply With Quote
  #5 (permalink)  
Old 10-20-03, 03:25
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,198
I think that my query and grofaty's is the same. But as you noted, his syntax may not be supported by your verison. They both do the same thing and should perform the same.
Reply With Quote
  #6 (permalink)  
Old 10-20-03, 06:56
grofaty grofaty is offline
Registered User
 
Join Date: Jan 2003
Posts: 1,570
Hi,

That was an interesting event! When I started to write the reply there was no replys, but when I sumited it then the Marcus_A had already posted the message. So the answers are almost the same... Funny!

As Marcus_A said, your database version does not suppot the above SQLs.

There is one more tip (I don't know if this is supported with your version of db2):

select n1, n2 from T1
except
select n1, n2 from T2

If this works, consider the Marcus_A tip about indexes.

Hope this helps,
Grofaty
Reply With Quote
  #7 (permalink)  
Old 10-22-03, 12:42
M_RAS M_RAS is offline
Registered User
 
Join Date: Sep 2003
Location: canada
Posts: 230
Quote:
Originally posted by Marcus_A
I am not sure what your are trying to do, but it sounds like you want the rows in table1 which don’t exist in table2.

Select A.n1, A.n2
from T1 A
where not exists
(select B.n1 from T2 B
where A.n1 = B.n1 and A.n2 = B.n2)

Make sure T2 has a composite index on (n1, n2). There can be other columns in the index if needed, but n1 and n2 should be the first 2 columns of the index for best performance.
It is better instead of (select B.n1) using select 1 and do not make trouble for db2 for searching B.n1 , because the result of not exist is true or false.
Thanks
Reply With Quote
  #8 (permalink)  
Old 10-22-03, 17:39
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,198
Maybe or maybe not.

DB2 has to find the column B.n1 anyway because it is part of the WHERE clause. The value is not returned to the application program because it is part of the EXISTS clause and is not included in the columns retrieved to the program in the main SELECT.

I don't think DB2 will be bothered by such an inclusion of the column in the SELECT clause of the sub-select. But that is just my opinion.
Reply With Quote
  #9 (permalink)  
Old 10-23-03, 05:48
rusiar rusiar is offline
Registered User
 
Join Date: Oct 2003
Location: York UK
Posts: 9
query can further be improved by

Select A.n1, A.n2
from T1 A
where not exists
(select 1 from T2 B
where A.n1 = B.n1 and A.n2 = B.n2)



Quote:
Originally posted by Marcus_A
I am not sure what your are trying to do, but it sounds like you want the rows in table1 which don’t exist in table2.

Select A.n1, A.n2
from T1 A
where not exists
(select B.n1 from T2 B
where A.n1 = B.n1 and A.n2 = B.n2)

Make sure T2 has a composite index on (n1, n2). There can be other columns in the index if needed, but n1 and n2 should be the first 2 columns of the index for best performance.
Reply With Quote
  #10 (permalink)  
Old 10-23-03, 05:52
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,198
That's what M_RAS said. But I am not so sure, for the reasons that I already gave.
Reply With Quote
  #11 (permalink)  
Old 10-23-03, 18:37
aloz aloz is offline
Registered User
 
Join Date: May 2003
Location: San Juan, PR
Posts: 18
Try this statement:

Select A.n1, A.n2
from T1 A left outer join T2 B
on A.n1 = B.n1 and A.n2 = B.n2
where B.n1 is null
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On