MVSFORUMS.com Forum Index MVSFORUMS.com
A Community of and for MVS Professionals
 
 FAQFAQ   SearchSearch   Quick Manuals   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Optimization question - Multiple acces of the same table

 
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database
View previous topic :: View next topic  
Author Message
THRIVIKRAM
Beginner


Joined: 03 Oct 2005
Posts: 70
Topics: 34

PostPosted: Fri Dec 03, 2010 10:11 am    Post subject: Optimization question - Multiple acces of the same table Reply with quote

Hi,

Iam trying to optimize this query:
Query:
Code:

SELECT DISTINCT A.SMT_DTN_CNY_CD DTNCNY                         
               ,A.DTN_SLC_NR DTNSLIC                           
               ,A.SVC_TYP_CD SVCTYPE                           
               ,COUNT(B.SMT_NR) SMTLETTER                       
               ,COUNT(C.SMT_NR) SMTDOC                         
               ,COUNT(D.SMT_NR) SMTNONDOC                       
               ,COUNT(A.SMT_NR) SMTTOTAL                       
FROM TABLEA A                                         
   LEFT OUTER JOIN TABLEA B ON A.SMT_NR = B.SMT_NR     
                               AND B.SMT_TYP_CD = '01'         
   LEFT OUTER JOIN TABLEA C ON A.SMT_NR = C.SMT_NR     
                               AND C.SMT_TYP_CD = '02'         
   LEFT OUTER JOIN TABLEA D ON A.SMT_NR = D.SMT_NR     
                               AND D.SMT_TYP_CD = '03'         
WHERE A.USR_NR = 'APP1BWT'                                     
      AND A.ORG_SRT_DT = '2010-11-08'                             
      AND A.ORG_SRT_TYP_CD LIKE 'T%'                               
      AND A.ORG_SLC_NR ='01029'                                   
    GROUP BY A.SMT_DTN_CNY_CD, A.DTN_SLC_NR, A.SVC_TYP_CD         

What is it trying to do?
SMT_TYP_CD field indicates if the Shipment is of type Letter or Document or Non Document. This query is embedded in a report program that will be executed thousands of time in a day. The TABLEA contains approx 12M rows. I think scanning this table three times is causing the delay. Is there any way we can access the table once to get all the SMT_TYP_CD
Back to top
View user's profile Send private message Send e-mail
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12378
Topics: 75
Location: San Jose

PostPosted: Fri Dec 03, 2010 12:52 pm    Post subject: Reply with quote

THRIVIKRAM,

If I were you , I would avoid the Joins and get all the 3 tables as a single table and then use that result table to join

Untested sql

Code:

SELECT DISTINCT A.SMT_DTN_CNY_CD DTNCNY       
               ,A.DTN_SLC_NR     DTNSLIC       
               ,A.SVC_TYP_CD     SVCTYPE       
               ,COUNT(Z.B_IND)   SMTLETTER     
               ,COUNT(Z.C_IND)   SMTDOC       
               ,COUNT(Z.C_IND)   SMTNONDOC     
               ,COUNT(A.SMT_NR)  SMTTOTAL     
                                               
  FROM TABLEA  A
      ,(SELECT SMT_NR     SMT_NR                               
              ,CHAR('B')  B_IND                                 
              ,CHAR(' ')  C_IND                                 
              ,CHAR(' ')  D_IND                                 
          FROM TABLEB                                       
         WHERE SMT_TYP_CD  = '01'                               
         UNION ALL                                             
        SELECT SMT_NR     SMT_NR                               
              ,CHAR(' ')  B_IND                                 
              ,CHAR('C')  C_IND                                 
              ,CHAR(' ')  D_IND                                 
          FROM TABLEC                                       
         WHERE SMT_TYP_CD  = '02'                               
         UNION ALL                                             
        SELECT SMT_NR     SMT_NR                               
              ,CHAR(' ')  B_IND                                 
              ,CHAR(' ')  C_IND                                 
              ,CHAR('D')  D_IND                                 
          FROM TABLED                                       
         WHERE SMT_TYP_CD  = '03') Z                           
                                                               
  WHERE A.SMT_NR     = Z.SMT_NR                                 
    AND A.ORG_SRT_DT = '2010-11-08'                             
    AND A.ORG_SLC_NR ='01029'                                   
    AND A.ORG_SRT_TYP_CD LIKE 'T%'                             
  GROUP BY A.SMT_DTN_CNY_CD, A.DTN_SLC_NR, A.SVC_TYP_CD         
 ;
Back to top
View user's profile Send private message Send e-mail Visit poster's website
THRIVIKRAM
Beginner


Joined: 03 Oct 2005
Posts: 70
Topics: 34

PostPosted: Fri Dec 03, 2010 2:28 pm    Post subject: Reply with quote

Thanks Kolusu for looking into this.

Quote:

get all the 3 tables as a single table and then use that result table to join


1) There is only one table involved here. TABLEA
2) If I use the approach you suggested, I will be selecting the same table four times. This table has 12M rows in it.

Thanks!!
Back to top
View user's profile Send private message Send e-mail
THRIVIKRAM
Beginner


Joined: 03 Oct 2005
Posts: 70
Topics: 34

PostPosted: Fri Dec 03, 2010 2:41 pm    Post subject: Reply with quote

Also, the counts doesnt match. Here is the O/P of the suggested query:
+-------------------------------------------------------------------------------
| DTNCNY | DTNSLIC | SVCTYPE | SMTLETTER | SMTDOC | SMTNONDOC
+-------------------------------------------------------------------------------
| AU | 08691 | 21 | 1 | 1 | 1
| AU | 08695 | 05 | 1 | 1 | 1
| AU | 08695 | 28 | 1 | 1 | 1
| AU | 09639 | 01 | 1 | 1 | 1
| AU | 09639 | 05 | 1 | 1 | 1
+------------------------------------------------------------------------------
-----------------+
| SMTTOTAL |
-----------------+
| 1 |
| 1 |
| 1 |
| 1 |
| 1 |
-----------------+

Basically, the query is trying to get the Total counts, Letter Counts, Doc Counts, Non Doc counts for a particular Country, Slic and Service type combination.

Thanks!!
Back to top
View user's profile Send private message Send e-mail
papadi
Supermod


Joined: 20 Oct 2009
Posts: 594
Topics: 1

PostPosted: Fri Dec 03, 2010 2:43 pm    Post subject: Reply with quote

Quote:

I will be selecting the same table four times. This table has 12M rows in it.

Unless the query does not use an index to directly access the quailfying rows, only the qualifying rows will be accessed. . . Not the 12M. . .

I don't understand why you repeated the left outer join 3 times. . . Would something like this not work:
Code:
  AND (B.SMT_TYP_CD = '01' OR '02' OR '03')


I'm not where i can test this . . . So it isn't tested Rolling Eyes

How long does this run now? What would be acceptable?
_________________
All the best,

di
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12378
Topics: 75
Location: San Jose

PostPosted: Fri Dec 03, 2010 2:52 pm    Post subject: Reply with quote

THRIVIKRAM wrote:
1) There is only one table involved here. TABLEA
Thanks!!


THRIVIKRAM,

Ok I am now confused. If it is just 1 table why do you have to use an LEFT OUTER joins? Why can't you do it with just a case statement? All you wanted is a count. Try this untested sql.

Code:

SELECT DISTINCT SMT_DTN_CNY_CD                        DTNCNY           
               ,DTN_SLC_NR                            DTNSLIC         
               ,SVC_TYP_CD                            SVCTYPE         
               ,SUM(CASE WHEN SMT_TYP_CD = '01'                       
                         THEN INT(1) ELSE INT(0) END) SMTLETTER       
               ,SUM(CASE WHEN SMT_TYP_CD = '02'                       
                         THEN INT(1) ELSE INT(0) END) SMTDOC           
               ,SUM(CASE WHEN SMT_TYP_CD = '03'                       
                         THEN INT(1) ELSE INT(0) END) SMTNONDOC       
               ,COUNT(SMT_NR)                         SMTTOTAL         
  FROM TABLE
 WHERE ORG_SRT_DT = '2010-11-08'                                       
   AND ORG_SLC_NR ='01029'                                             
   AND ORG_SRT_TYP_CD LIKE 'T%'                                       
   AND SMT_TYP_CD IN ('01','02','03')                                 
 GROUP BY SMT_DTN_CNY_CD                                               
         ,DTN_SLC_NR                                                   
         ,SVC_TYP_CD                                                   
 ;                                                                     
Back to top
View user's profile Send private message Send e-mail Visit poster's website
THRIVIKRAM
Beginner


Joined: 03 Oct 2005
Posts: 70
Topics: 34

PostPosted: Mon Dec 06, 2010 12:48 am    Post subject: Reply with quote

Thanks Kolusu for the reply.

In my initial query, TABLEA A in one region and TABLEA B,C,D are in other region. Initial query has a different names for TABLEA B,C,D which are Data Proped from TABLEA A. (Sorry, If Iam confusing here) I dont want my question to be too complex and so gave that all my tables correspond to a single table. In this thought process, this SUM function skipped my mind.
Iam going to use your SQL to suit my needs. Thanks once again.

Thanks!!
Back to top
View user's profile Send private message Send e-mail
jim haire
Beginner


Joined: 30 Dec 2002
Posts: 140
Topics: 40

PostPosted: Mon Dec 06, 2010 3:29 pm    Post subject: Reply with quote

Not sure if it would have too much impact, but I think you can remove the DISTINCT clause since you are grouping counts by A.SMT_DTN_CNY_CD,A.DTN_SLC_NR,A.SVC_TYP_CD.

You would always be assured of only getting 1 row for that combination of values with the counts. Distinct causes a sort to be performed, but removing it in this case probably doesn't matter as the GROUP BY would more than likely cause a sort to be performed anyway. Still, the DISTINCT is not needed and can be taken out.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


MVSFORUMS
Powered by phpBB © 2001, 2005 phpBB Group