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 > Better way to write query for improving performance?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-04-11, 06:00
ashu000 ashu000 is offline
Registered User
 
Join Date: Apr 2011
Posts: 28
Better way to write query for improving performance?

Hi All,

I have written a query which is working fine but taking long time to execute, it is actually matching a lot of data. If there is some other way of writing my query, then please suggest.

Scenario :

I have 3 tables - Country, Stores and Mapping. Country table is having column state and indicator. User can select any state and either include it or exclude it, which will update the indicator to Include or Exclude. The stores table is having column store number and column indicator. Mapping table is having mapping between the state and the stores.

When user includes or excludes any of the state, it updates the Country table indicator corresponding to the state, which is not a problem.
Now, I need to update the Store table where first I have to check the stores one by one that they belong to which State from Mapping table and if the found state is the same selected by the user, then update store indicator in the Store table.
Because number of records are huge, so it is affecting the performance.

For updating the Store table, I am writing the following query:
UPDATE STORE SET INDICATOR = :INDICATOR WHERE STATE IN (SELECT STORE_NUMBER FROM MAPPING WHERE STATE IN (: STATE)).

The ": STATE" value I am passing through my java program and it will go like ('IL', 'AL').

I need help to make this query better if possible.

Thanks a lot for any help and suggestions.

Last edited by ashu000; 06-04-11 at 06:20.
Reply With Quote
  #2 (permalink)  
Old 06-04-11, 07:12
tonkuma tonkuma is online now
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
Remove(or ignore) Mapping table.
Then add state column to Store table.
If you want to see the indicator, join Country table and see indicator in Country table.
Reply With Quote
  #3 (permalink)  
Old 06-04-11, 09:29
ashu000 ashu000 is offline
Registered User
 
Join Date: Apr 2011
Posts: 28
Hi Tonkuma,

Thanks for your reply.

Actually there are other dependencies and it is not allowed to change the table. So with the same structure I have to go.

I think what I am doing is the only way.

Thanks a lot once again for your replies.
Reply With Quote
  #4 (permalink)  
Old 06-04-11, 18:23
Peter.Vanroose Peter.Vanroose is offline
Registered User
 
Join Date: Sep 2004
Location: Belgium
Posts: 1,079
Quote:
Originally Posted by ashu000 View Post
UPDATE STORE SET INDICATOR = :INDICATOR WHERE STATE IN (SELECT STORE_NUMBER FROM MAPPING WHERE STATE IN (: STATE))
Maybe try a correlated subquery:
Code:
UPDATE STORE s SET INDICATOR = :INDICATOR WHERE EXISTS
(SELECT 1 FROM MAPPING WHERE s.STATE=STORE_NUMBER and STATE IN (: STATE))
__________________
--_Peter Vanroose,
__IBM Certified Database Administrator, DB2 9 for z/OS
__IBM Certified Application Developer
__ABIS Training and Consulting
__http://www.abis.be/
Reply With Quote
Reply

Tags
db2, select, update

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