IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

Performance issue while using SELECT DISTINCT


IBM Mainframe Forums -> DB2
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
skrishnavijay

New User


Joined: 26 Sep 2007
Posts: 25
Location: chennai

PostPosted: Fri Aug 01, 2008 9:12 pm
Reply with quote

Hello,

I have an performance issue with the following query

SELECT COUNT (DISTINCT APP_ID)

INTO :WS-CNT

FROM AO_APPL A, AN_APPL B

WHERE

A.APP_ID <> :HS-APPL-ID AND
A.TIN_NO = :HS-TIN-NO AND
B.APP_ID = A.APP_ID AND
A.AP_TMP_ST > :HS-TIME-STAMP

can I have any other way to represent the above query?
DISTINCT is causing for the more cpu utilization time.

Also please explain me the order in which DB2 executes this stament. According to my understanding, DB2 joins the tables first and then process the queries.

Thanks in advance,
Krishna
Back to top
View user's profile Send private message
Suresh Ponnusamy

Active User


Joined: 22 Feb 2008
Posts: 107
Location: New York

PostPosted: Fri Aug 01, 2008 11:11 pm
Reply with quote

Hi

1) It is (Rewriting of the Query) depending upon your Tables Primary Keys and data on the Two Tables. If you could provide Keys and sample data, some one can help to some extend. It would be better if you could contact your DBA, they will provide better solution.

2) Yes. That is true. First DB2 joins both the tables depending on your WHERE clause, then it processes the query.
Back to top
View user's profile Send private message
priyesh.agrawal

Senior Member


Joined: 28 Mar 2005
Posts: 1448
Location: Chicago, IL

PostPosted: Fri Aug 01, 2008 11:13 pm
Reply with quote

Krishna,

From application programming point of view I don't see anything wrong with your query, other than a minor syntax error. You would need to qualify APP_ID in DISTINCT clause. For performance issue, you may want to contact the system DBA group and have them take a look on table structures.

And, I think you are right, it'll join the tables first.
Back to top
View user's profile Send private message
skrishnavijay

New User


Joined: 26 Sep 2007
Posts: 25
Location: chennai

PostPosted: Sun Aug 03, 2008 3:36 pm
Reply with quote

Thanks a lot for your suggestion.

Regarding table keys and others i will get back.

So whatever the order we specify in the WHERE clause, DB2 joins all the tables mentioned and process the queries in its own manner.

Please correct me if i am wrong.

Thanks,
Krishna
Back to top
View user's profile Send private message
ashimer

Active Member


Joined: 13 Feb 2004
Posts: 551
Location: Bangalore

PostPosted: Mon Aug 04, 2008 2:11 pm
Reply with quote

The order does not really matter ... DB2 is smart enough to rearrange it to choose the optimal access path .... here the join is done first and the result set is sorted in a sort file to remove the duplicates .. so using distinct will definitely require an extra sorting ....
Back to top
View user's profile Send private message
bhushan.shete

New User


Joined: 14 Nov 2006
Posts: 17

PostPosted: Sat Dec 20, 2008 1:58 am
Reply with quote

Hi
ashimer wrote:
the result set is sorted in a sort file to remove the duplicates ..

Here on which keys(field/column) records are sorted? is it that columns mentioned in select clause are used for sorting?

Regards
Bhushan
Back to top
View user's profile Send private message
dick scherrer

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19244
Location: Inside the Matrix

PostPosted: Sat Dec 20, 2008 2:46 am
Reply with quote

Hello,

The sort would be used to islolate the DISTINCT values.
Back to top
View user's profile Send private message
bhushan.shete

New User


Joined: 14 Nov 2006
Posts: 17

PostPosted: Mon Dec 22, 2008 6:54 pm
Reply with quote

hi dick

I did lot of combination with distinct, finally come to conclusion that when we use distinct in select, columns in select clause are only considered for sorting(i.e. to eliminate duplicates). Kindly confirm this.

Earlier I assumed it as first column after writing distinct is considered to get distinct value, which was of course a wrong assumption.

thanks
bhushan
Back to top
View user's profile Send private message
dick scherrer

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19244
Location: Inside the Matrix

PostPosted: Mon Dec 22, 2008 10:02 pm
Reply with quote

Hello,

Quote:
Kindly confirm this
Yes, for this case that is true.

For other SELECTs, there may be other reasons for sorting.
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> DB2

 


Similar Topics
Topic Forum Replies
No new posts SFTP Issue - destination file record ... All Other Mainframe Topics 2
No new posts Dynamically pass table name to a sele... DB2 2
No new posts exploiting Z16 performance PL/I & Assembler 2
No new posts Issue after ISPF copy to Linklist Lib... TSO/ISPF 1
No new posts SELECT from data change table DB2 5
Search our Forums:

Back to Top