Solved

Query Review

Posted on 2011-03-01
7
191 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
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
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.

757 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now