Solved

Query Review

Posted on 2011-03-01
7
194 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.

770 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