Quote:
|
and found out that the first subquery (select count(campaignde6_.id)...) is the one causing the long execution time
|
Of course it slows down the statement, because it will be executed for
every row that is returned from the main query.
Why do you have a GROUP BY at the end of the statement, if the statement itself does not use any aggregate function? Is that used to get a DISTINCT result?
I think (I might be mistaken!) you can replace the sub-select in the SELECT list with an analytical function. As the same count is again used in the WHERE part, I think you can get rid of the second sub-select as well by putting the whole thing into a derived table:
Code:
SELECT col_0_0_,
col_1_0_,
col_2_0_,
campaign_count
FROM (
select
customer2_.id as col_0_0_,
customerse4_.id as col_1_0_,
campaign1_.id as col_2_0_,
count(campaign1_.id) over (partition by campaign1_.id, customer2_.id) as campaign_count,
campaign1_.frequencyCapping
from
CAMPAIGNDELIVERY campaignde0_
inner join
CAMPAIGN campaign1_
on campaignde0_.campaign_id=campaign1_.id
left outer join
CAMPAIGN_CUSTOMERSELECTION customerse3_
on campaign1_.id=customerse3_.campaign_id
left outer join
CUSTOMERSELECTION customerse4_
on customerse3_.customerSelection_id=customerse4_.id
left outer join
CUSTOMERSELECTIONASSIGNMENT customerse5_
on customerse4_.id=customerse5_.customerSelection_id
inner join
CUSTOMER customer2_
on campaignde0_.customer_id=customer2_.id
where
(
customer2_.id=customerse5_.customer_id
or customerse5_.customer_id is null
)
and (campaign1_.id in (775))
and (campaignde0_.feedback<>0 or campaign1_.frequencyCapping > 0)
campaignde0_.mandator_id=368
) t
where frequencyCapping <= campaign_count
Not sure if I got the syntax 100% correct, but you might get what I'm aiming at.
I'm also not sure if I understood the query 100% correct, so there might be a logical problem in there as well. Especially the analytical function that does the counting, might count the wrong rows.
The statement also looks like it's traversing a hierarchical table.
If that is the case, might also consider using CONNECT BY instead of mutliple outer joins. For one thing it makes the satement a lot easier to read and it might also improve performance. But as you said that the "base" statement is fast enough, that might not be necessary for performance reasons.
Edit: just realized this is not the Oracle forum

So forget the CONNECT BY clause (which is Oracle specific).
The windowing function is ANSI standard though.