Solved

Query Review

Posted on 2011-03-01
7
197 Views
Last Modified: 2012-05-11
Do w have better query than this

   SELECT a.top_id,   COUNT (b.po_id) published_replies, MAX (b.date_posted) new_rep_date FROM    tb1 a   LEFT OUTER JOIN    tb2 b   ON a.top_id = b.top_id AND b.workflow = 'A'  GROUP BY a.top_id;

Execution Plan
----------------------------------------------------------
Plan hash value: 3224356369

--------------------------------------------------------------------------------
---------------

| Id  | Operation                      | Name         | Rows  | Bytes | Cost (%CPU)| Time     |

--------------------------------------------------------------------------------
---------------

|   0 | SELECT STATEMENT               |              |     4 |    96 |     5  (20)| 00:00:01 |

|   1 |  SORT GROUP BY NOSORT          |              |     4 |    96 |     5  (20)| 00:00:01 |

|   2 |   MERGE JOIN OUTER             |              |   105 |  2520 |     5  (20)| 00:00:01 |

|   3 |    INDEX FULL SCAN             | PK_TOP     |   105 |   420 |     1  (0)| 00:00:01 |

|*  4 |    SORT JOIN                   |              |     5 |   100 |     4  (25)| 00:00:01 |

|   5 |     TABLE ACCESS BY INDEX ROWID| tb2        |     5 |   100 |      3 (0)| 00:00:01 |

|*  6 |      INDEX RANGE SCAN          | IDX_WORKFLOW |     5 |       |     1 (0)| 00:00:01 |

--------------------------------------------------------------------------------
---------------
0
Comment
Question by:vadicherla
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 18

Expert Comment

by:sventhan
ID: 35008970
This would be better

 SELECT a.top_id,   COUNT (b.po_id) published_replies, MAX (b.date_posted) new_rep_date FROM    tb1 a   Inner JOIN    tb2 b   ON a.top_id = b.top_id AND b.workflow = 'A'  GROUP BY a.top_id;

or if you really need Left Outer Join then I do not see anything wrong with your SQL.
0
 
LVL 12

Expert Comment

by:Paul_Harris_Fusion
ID: 35009027
it is a pretty simple query so the options for refactoring are pretty limited.

Assuming you need the outer join,  I think your approach is probably pretty optimal.  

If you could live without the outer join,  then you could live without the join at all but I guess this is unlikely.
0
 
LVL 12

Expert Comment

by:Paul_Harris_Fusion
ID: 35009049
if you do not need the outer join then:

Select top_id, count(po_id) as published_replies,  MAX(date_posted) as new_rep_date
from tb1
where b.workflow = 'A
group by top_id
;
0
The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

 

Author Comment

by:vadicherla
ID: 35009051
inner join is excluding  the records which  does not have value(0)
0
 
LVL 12

Assisted Solution

by:Paul_Harris_Fusion
Paul_Harris_Fusion earned 100 total points
ID: 35009054
Sorry typo:

Select top_id, count(po_id) as published_replies,  MAX(date_posted) as new_rep_date
from tb2
where b.workflow = 'A
group by top_id
;
0
 
LVL 12

Expert Comment

by:Paul_Harris_Fusion
ID: 35009432
Yes - leaving out the inner join would have exactly the result you describe.    I think yoiu already have the best approach to the query.

If performance is an issue, your only other approach would be to experiment with indexing the columns you are filtering on in the join statement.
0
 
LVL 1

Accepted Solution

by:
sunil_rangineni earned 400 total points
ID: 35015923
Looking at your query, i feel using the concept of corelated subqueries would give better performnace it this instance.
------------------------------
select a.top_id,
       (select count(po_id) from tb2 where top_id = tb1.top_id and workflow = 'A') as cnt_poid,
       (select MAX (date_posted) from tb2 where top_id = tb1.top_id and workflow = 'A') as max_date
from tb1;

Indexing of top_id in both the tables will fasten the query.
0

Featured Post

Is Your DevOps Pipeline Leaking?

Is your CI/CD pipeline a hodge-podge of randomly connected tools? You’ve likely got a tool to fix one problem & then a different tool to fix another, resulting in a cluster of tools with overlapping functionality. Learn how to optimize your pipeline with Gartner's recommendations

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question