WITH Clause no longer working. "exceeded call limit on IO usage" - oracle

I have some code that previously ran fine with no issues. But now when I run I receive the
ORA-02395: exceeded call limit on IO usage
02395. 00000 - "exceeded call limit on IO usage"
error. Can anyone explain to me why my code is throwing this error. I have no access to increase any user privileges. I've tried running this code and limiting the amount of data in the with clause and it ran fine. So I'm guessing some modifications need to happen there.
WITH NEW_REP_DATA AS (
select period,manager rep,comp,sum(a) "GT99",sum(b) "90TO99",sum(c) "80TO89",sum(d) "70TO79",sum(e) "LT70",sum(f) "NA" from (
select period
,rep_code
,manager
--,nvl(a,0)+nvl(b,0)+nvl(c,0)+nvl(d,0)+nvl(e,0)+nvl(f,0) cnt
,comp,
nvl(a,0) a,nvl(b,0) b, nvl(c,0) c, nvl(d,0) d,nvl(e,0) e,nvl(f,0) f
from
(select period,rep_code,manager, comp,max(case when bucket='>99' then cnt end) a,
max(case when bucket='90TO99' then cnt end) b,
max(case when bucket='80TO89' then cnt end) c,
max(case when bucket= '70TO79' then cnt end) d,
max(case when bucket='LT70' then cnt end) e,
max(case when bucket='NA' then cnt end) f
from (
select period,rep_code,manager, comp,bucket,
--count(unique rep_code)
count( distinct rep_code) cnt
--cnt
from(
select * from (select
unique period
,MANAGER
,"PayeeID" rep_code
,comp
,cytd cytd_a
,cytp cytd_p,
nvl(case when cytp > 0 then
case when round((cytd/cytp),3) > .99 then '>99'
when round((cytd/cytp),3) between .891 and .99 then '90TO99'
when round((cytd/cytp),3) between .791 and .89 then '80TO89'
when round((cytd/cytp),3) between .7 and .79 then '70TO79'
when round((cytd/cytp),3) < .7 then 'LT70'
end
when cytp = 0 and cytd > 0 then '>99'
else 'NA'
end,'NA') as bucket
from (
select aaa.period
,aaa."PayeeID"
,aaa."Reports_to" MANAGER
,aaa."Component" comp,
aaa."Current_YTD_Actual" cytd,
aaa."Current_YTD_Plan" cytp
from nbr_var_data aaa
where aaa."Comp_Plan_Name" not in ('MISC_COMP','GM_2017')
AND "Comp_Plan_Name" not in ('MISC_COMP')
AND "Comp_Plan_Name" not LIKE '%GM%'
and aaa.period = (select max(aaa.period) from Nbr_Var_Data)
))
)
where 1=1
group by period, rep_code, comp, bucket, manager )
group by period, rep_code, comp, manager )) group by period, manager, comp)
SELECT DISTINCT
dc.rep PID
,dc.period
,ee.PAYEE_NAM
,dc.comp
,Dc."GT99"
,Dc."90TO99"
,Dc."80TO89"
,Dc."70TO79"
,Dc."LT70"
,Dc."NA"
,Ee.Parent_Payee_Id REPORT_TO_PAYEE_ID
,Ee.Parent_Payee_Nam REPORT_TO_NAME
FROM (SELECT
--period ,
--empl_id ,
gg.payee_id ,
gg.payee_nam ,
--lvl ,
ff.parent_payee_id ,
ff.PARENT_PAYEE_NAM
--parent_lvl ,
--mnth_disp
FROM (SELECT DISTINCT
dd.period
,dd.PARENT_PAYEE_ID
,ee.PAYEE_NAM PARENT_PAYEE_NAM
from (
SELECT DISTINCT
PERIOD,
PARENT_PAYEE_ID
FROM FI_CHANNEL_HIER) dd
left join FI_CHANNEL_HIER ee
on dd.PARENT_PAYEE_ID = ee.PAYEE_ID
WHERE DD.PERIOD = (select max(DD.PERIOD) from FI_CHANNEL_HIER )) ff
LEFT JOIN FI_CHANNEL_HIER gg
ON ff.PARENT_PAYEE_ID = gg.PARENT_PAYEE_ID) ee,
NEW_REP_DATA dc
WHERE EE.PAYEE_ID = DC.Rep
;

ORA-02395 happens when your query exceeds the LOGICAL_READS_PER_CALL threshold set in your user profile. That's why the issue goes away when you restricted the amount of data returned by your subquery.
Two possible explanations for why this did not use to happen:
The tables contain more data than they used to be.
Your DBA has implemented a new user profile (or made the existing one stricter).
The obvious solution is to negotiate with the DBA.
Otherwise you will need to refactor your query to reduce the number of blocks it scans. Doing requires an understanding of your data model, your business logic and your data characteristics (volume, distribution, usage).
Clearly that requires knowledge of and access to your system, so it's not something that we can help with.

WITH NEW_REP_DATA AS (
select period,manager rep,comp,sum(a) "GT99",sum(b) "90TO99",sum(c) "80TO89",sum(d) "70TO79",sum(e) "LT70",sum(f) "NA" from (
select period
,rep_code
,manager
--,nvl(a,0)+nvl(b,0)+nvl(c,0)+nvl(d,0)+nvl(e,0)+nvl(f,0) cnt
,comp,
nvl(a,0) a,nvl(b,0) b, nvl(c,0) c, nvl(d,0) d,nvl(e,0) e,nvl(f,0) f
from
(select period,rep_code,manager, comp,max(case when bucket='>99' then cnt end) a,
max(case when bucket='90TO99' then cnt end) b,
max(case when bucket='80TO89' then cnt end) c,
max(case when bucket= '70TO79' then cnt end) d,
max(case when bucket='LT70' then cnt end) e,
max(case when bucket='NA' then cnt end) f
from (
select period,rep_code,manager, comp,bucket,
--count(unique rep_code)
count( distinct rep_code) cnt
--cnt
from(
select * from (select
unique period
,MANAGER
,"PayeeID" rep_code
,comp
,cytd cytd_a
,cytp cytd_p,
nvl(case when cytp > 0 then
case when round((cytd/cytp),3) > .99 then '>99'
when round((cytd/cytp),3) between .891 and .99 then '90TO99'
when round((cytd/cytp),3) between .791 and .89 then '80TO89'
when round((cytd/cytp),3) between .7 and .79 then '70TO79'
when round((cytd/cytp),3) < .7 then 'LT70'
end
when cytp = 0 and cytd > 0 then '>99'
else 'NA'
end,'NA') as bucket
from (
select aaa.period
,aaa."PayeeID"
,aaa."Reports_to" MANAGER
,aaa."Component" comp,
aaa."Current_YTD_Actual" cytd,
aaa."Current_YTD_Plan" cytp
from nbr_var_data aaa
where aaa."Comp_Plan_Name" not in ('MISC_COMP','GM_2017')
AND "Comp_Plan_Name" not in ('MISC_COMP')
AND "Comp_Plan_Name" not LIKE '%GM%'
and aaa.period = (select max(aaa.period) from Nbr_Var_Data)
))
)
where 1=1
group by period, rep_code, comp, bucket, manager )
group by period, rep_code, comp, manager )) group by period, manager, comp)
SELECT DISTINCT
dc.rep PID
,dc.period
,ee.PAYEE_NAM
,dc.comp
,Dc."GT99"
,Dc."90TO99"
,Dc."80TO89"
,Dc."70TO79"
,Dc."LT70"
,Dc."NA"
,Ee.Parent_Payee_Id REPORT_TO_PAYEE_ID
,Ee.Parent_Payee_Nam REPORT_TO_NAME
FROM (SELECT
--period ,
--empl_id ,
gg.payee_id ,
gg.payee_nam ,
--lvl ,
ff.parent_payee_id ,
ff.PARENT_PAYEE_NAM
--parent_lvl ,
--mnth_disp
FROM (SELECT DISTINCT
dd.period
,dd.PARENT_PAYEE_ID
,ee.PAYEE_NAM PARENT_PAYEE_NAM
from (
SELECT DISTINCT
PERIOD,
PARENT_PAYEE_ID
FROM FI_CHANNEL_HIER) dd
left join FI_CHANNEL_HIER ee
on dd.PARENT_PAYEE_ID = ee.PAYEE_ID
WHERE DD.PERIOD = (select max(DD.PERIOD) from FI_CHANNEL_HIER )) ff
LEFT JOIN FI_CHANNEL_HIER gg
ON ff.PARENT_PAYEE_ID = gg.PARENT_PAYEE_ID) ee,
NEW_REP_DATA dc
WHERE EE.PAYEE_ID = DC.Rep
AND dc.period = (select max(period) from NEW_REP_DATA)
;

Related

Re-writing a join query

I have a question concerning Hive. Let me explain to you the scenario :
I am using a Hive action on Oozie; I have a query which is doing
succesive LEFT JOIN on different tables;
Total number of rows to be inserted is about 35 million;
First, the job was crashing due to lack of memory, so I set "set hive.auto.convert.join=false" the query was perfectly executed but it took 4 hours to be done;
I tried to rewrite the order of LEFT JOINs putting large tables at the end, but same result, about 4 hours to be executed;
Here is what the query look like:
INSERT OVERWRITE TABLE final_table
SELECT
T1.Id,
T1.some_field_name,
T1.another_filed_name,
T2.also_another_filed_name,
FROM table1 T1
LEFT JOIN table2 T2 ON ( T2.Id = T1.Id ) -- T2 is the smallest table
LEFT JOIN table3 T3 ON ( T3.Id = T1.Id )
LEFT JOIN table4 T4 ON ( T4.Id = T1.Id ) -- T4 is the biggest table
So, knowing the structure of the query is there a way to rewrite it so that I can avoid too many JOINs ?
Thanks in advance
PS: Even vectorization gave me the same timing
Too long for a comment, will be deleted later.
(1) Your current query won't compile.
(2) You are not selecting anything from T3 and T4, which makes no sense.
(3) Changing the order of tables is not likely to have any impact with cost based optimizer.
(4) Basically I would suggest to collect statistics on the tables, specifically on the id columns, but in your case I got a feeling that id is not unique in more than 1 table.
Add to your post the result of the following query:
select *
, case when cnt_1 = 0 then 1 else cnt_1 end
* case when cnt_2 = 0 then 1 else cnt_2 end
* case when cnt_3 = 0 then 1 else cnt_3 end
* case when cnt_4 = 0 then 1 else cnt_4 end as product
from (select id
,count(case when tab = 1 then 1 end) as cnt_1
,count(case when tab = 2 then 1 end) as cnt_2
,count(case when tab = 3 then 1 end) as cnt_3
,count(case when tab = 4 then 1 end) as cnt_4
from ( select 1 as tab,id from table1
union all select 2 as tab,id from table2
union all select 3 as tab,id from table3
union all select 4 as tab,id from table4
) t
group by id
having greatest (cnt_1,cnt_2,cnt_3,cnt_4) >= 10
) t
order by product desc
limit 10
;

Ora-00932 - expected NUMBER got -

I have been running the below query without issue:
with Nums (NN) as
(
select 0 as NN
from dual
union all
select NN+1 -- (1)
from Nums
where NN < 30
)
select null as errormsg, trunc(sysdate)-NN as the_date, count(id) as the_count
from Nums
left join
(
SELECT c1.id, trunc(c1.c_date) as c_date
FROM table1 c1
where c1.c_date > trunc(sysdate) - 30
UNION
SELECT c2.id, trunc(c2.c_date)
FROM table2 c2
where c2.c_date > trunc(sysdate) -30
) x1
on x1.c_date = trunc(sysdate)-Nums.NN
group by trunc(sysdate)-Nums.NN
However, when I try to pop this in a proc for SSRS use:
procedure pr_do_the_thing (RefCur out sys_refcursor)
is
oops varchar2(100);
begin
open RefCur for
-- see above query --
;
end pr_do_the_thing;
I get
Error(): PL/SQL: ORA-00932: inconsistent datatypes: expected NUMBER got -
Any thoughts? Like I said above, as a query, there is no issue. As a proc, the error appears at note (1) int eh query.
This seems to be bug 18139621 (see MOS Doc ID 2003626.1). There is a patch available, but if this is the only place you encounter this, it might be simpler to switch to a hierarchical query:
with Nums (NN) as
(
select level - 1
from dual
connect by level <= 31
)
...
You could also calculate the dates inside the CTE (which also fails with a recursive CTE):
with Dates (DD) as
(
select trunc(sysdate) - level + 1
from dual
connect by level <= 31
)
select null as errormsg, DD as the_date, count(id) as the_count
from Dates
left join
(
SELECT c1.id, trunc(c1.c_date) as c_date
FROM table1 c1
where c1.c_date > trunc(sysdate) - 30
UNION
SELECT c2.id, trunc(c2.c_date)
FROM table2 c2
where c2.c_date > trunc(sysdate) -30
) x1
on x1.c_date = DD
group by DD;
I'd probably organise it slightly differently, so the subquery doesn't limit the date range directly:
with dates (dd) as
(
select trunc(sysdate) - level + 1
from dual
connect by level <= 31
)
select errormsg, the_date, count(id) as the_count
from (
select null as errormsg, d.dd as the_date, c1.id
from dates d
left join table1 c1 on c1.c_date >= d.dd and c1.c_date < d.dd + 1
union all
select null as errormsg, d.dd as the_date, c2.id
from dates d
left join table2 c2 on c2.c_date >= d.dd and c2.c_date < d.dd + 1
)
group by errormsg, the_date;
but as always with these things, check the performance of each approach...
Also notice that I've switched from union to union all. If an ID could appear more than once on the same day, in the same table or across both tables, then the counts will be different - you need to decide whether you want to count them once or as many times as they appear. That applies to your original query too.

Oracle/SQL Query issue

I am sort of a newbie to oracle/sql. I am trying to pull from the same column different values and add them with some other information. The other information is not the issue it is trying to count and add here the problem comes in.
I am connecting to an oracle database. Here is what i have
SELECT
EV.PUBLIC_DESCRIPTION,
EV.EVENT_DATE,
ES.PRICE,
BT.BUYER_TYPE_CODE,
PCA.ADDR1,
PCA.ADDR2,
PCA.CITY,
PCA.POSTAL_CODE,
PCE.EMAIL,
PC.FORMATTED_NAME,
PCP.PHONE_NUMBER,
PCP.SECONDARY,
SUM(COUNT(CASE WHEN BT.BUYER_TYPE_CODE = 'GRADLT' THEN 1 ELSE 0 END) + COUNT(CASE WHEN BT.BUYER_TYPE_CODE = 'GRADTE' THEN 1 ELSE 0 END) + COUNT(CASE WHEN BT.BUYER_TYPE_CODE = 'GRSTND' THEN 1 ELSE 0 END) + COUNT(CASE WHEN BT.BUYER_TYPE_CODE = 'GSTDTE' THEN 1 ELSE 0 END) + COUNT(CASE WHEN BT.BUYER_TYPE_CODE = 'GROUDI' THEN 1 ELSE 0 END)) AS "Adults",
SUM(COUNT(CASE WHEN BT.BUYER_TYPE_CODE = 'GRCHILD' THEN 1 ELSE 0 END) + COUNT(CASE WHEN BT.BUYER_TYPE_CODE = 'GRCHTE' THEN 1 ELSE 0 END)) AS 'Paid Child',
SUM(COUNT(CASE WHEN BT.BUYER_TYPE_CODE = 'GRPCH' THEN 1 ELSE 0 END)) AS 'Free Child',
SUM(COUNT(CASE WHEN BT.BUYER_TYPE_CODE = 'GRCOMP' THEN 1 ELSE 0 END)) AS 'Comps'
FROM EVENT EV
INNER JOIN EVENT_SEAT ES ON EV.EVENT_ID = ES.EVENT_ID
INNER JOIN BUYER_TYPE BT ON ES.BUYER_TYPE_ID = BT.BUYER_TYPE_ID
INNER JOIN PATRON_ORDER PO ON ES.ORDER_ID = PO.ORDER_ID
INNER JOIN PATRON_ACCOUNT PA ON ES.ATTENDING_PATRON_ACCOUNT_ID = PA.PATRON_ACCOUNT_ID
INNER JOIN PATRON_CONTACT PC ON PA.PATRON_ACCOUNT_ID = PC.PATRON_ACCOUNT_ID
INNER JOIN PATRON_CONTACT_ADDRESS PCA ON PC.PATRON_ACCOUNT_ID = PCA.PATRON_ACCOUNT_ID
INNER JOIN PATRON_CONTACT_EMAIL PCE ON PCA.PATRON_ACCOUNT_ID = PCE.PATRON_ACCOUNT_ID
INNER JOIN PATRON_CONTACT_PHONE PCP ON PCE.PATRON_ACCOUNT_ID = PCP.PATRON_ACCOUNT_ID
GROUP BY EV.PUBLIC_DESCRIPTION, EV.EVENT_DATE
ORDER BY ES.TRANSACTION_ID DESC, PCP.SECONDARY DESC, PCP.PHONE_NUMBER DESC, PC.FORMATTED_NAME DESC, PCE.EMAIL DESC, PCA.POSTAL_CODE DESC, PCA.CITY DESC, PCA.ADDR2 DESC, PCA.ADDR1 DESC, BT.BUYER_TYPE_CODE DESC, ES.PRICE DESC;
any help would be greatly appreciated
You need to decide which technique you want to use, currently you are using 2 techniques and they are colliding.
For this you must know: COUNT() will increment by one for every NON-NULL value
So, to use COUNT() with a case expression do this
COUNT(CASE WHEN BT.BUYER_TYPE_CODE = 'GRSTND' THEN 1 END)
or
COUNT(CASE WHEN BT.BUYER_TYPE_CODE = 'GRSTND' THEN 1 ELSE NULL END)
OR, don't use COUNT(), use SUM() instead
SUM(CASE WHEN BT.BUYER_TYPE_CODE = 'GRSTND' THEN 1 ELSE 0 END)
To add conditions together, I suggest you use the case expression better
Instead of something like this:
, COUNT(CASE WHEN BT.BUYER_TYPE_CODE = 'GRADLT' THEN 1 END)
+ COUNT(CASE WHEN BT.BUYER_TYPE_CODE = 'GRADTE' THEN 1 END)
+ COUNT(CASE WHEN BT.BUYER_TYPE_CODE = 'GRSTND' THEN 1 END)
+ COUNT(CASE WHEN BT.BUYER_TYPE_CODE = 'GSTDTE' THEN 1 END)
+ COUNT(CASE WHEN BT.BUYER_TYPE_CODE = 'GROUDI' THEN 1 END) AS "Adults"
Use this:
COUNT(CASE WHEN BT.BUYER_TYPE_CODE IN ('GRADLT','GRADTE','GRSTND','GSTDTE','GROUDI') THEN 1 ELSE NULL END)
There is also an issue with your GROUP BY, which MUST contain ALL non-aggregating columns. I think your query should look more like this:
SELECT
EV.PUBLIC_DESCRIPTION
, EV.EVENT_DATE
, ES.PRICE
/* , BT.BUYER_TYPE_CODE */
, PCA.ADDR1
, PCA.ADDR2
, PCA.CITY
, PCA.POSTAL_CODE
, PCE.EMAIL
, PC.FORMATTED_NAME
, PCP.PHONE_NUMBER
, PCP.SECONDARY
, COUNT(CASE WHEN BT.BUYER_TYPE_CODE IN ('GRADLT', 'GRADTE', 'GRSTND', 'GSTDTE', 'GROUDI') THEN 1 ELSE NULL END) AS "Adults"
, COUNT(CASE WHEN BT.BUYER_TYPE_CODE IN ('GRCHILD', 'GRCHTE', 'GRPCH', 'GRCOMP') THEN 1 ELSE NULL END) AS "Free Child"
, COUNT(CASE WHEN BT.BUYER_TYPE_CODE = 'GRCOMP' THEN 1 ELSE NULL END) AS "Comps"
FROM EVENT EV
INNER JOIN EVENT_SEAT ES
ON EV.EVENT_ID = ES.EVENT_ID
INNER JOIN BUYER_TYPE BT
ON ES.BUYER_TYPE_ID = BT.BUYER_TYPE_ID
INNER JOIN PATRON_ORDER PO
ON ES.ORDER_ID = PO.ORDER_ID
INNER JOIN PATRON_ACCOUNT PA
ON ES.ATTENDING_PATRON_ACCOUNT_ID = PA.PATRON_ACCOUNT_ID
INNER JOIN PATRON_CONTACT PC
ON PA.PATRON_ACCOUNT_ID = PC.PATRON_ACCOUNT_ID
INNER JOIN PATRON_CONTACT_ADDRESS PCA
ON PC.PATRON_ACCOUNT_ID = PCA.PATRON_ACCOUNT_ID
INNER JOIN PATRON_CONTACT_EMAIL PCE
ON PCA.PATRON_ACCOUNT_ID = PCE.PATRON_ACCOUNT_ID
INNER JOIN PATRON_CONTACT_PHONE PCP
ON PCE.PATRON_ACCOUNT_ID = PCP.PATRON_ACCOUNT_ID
GROUP BY
EV.PUBLIC_DESCRIPTION
, EV.EVENT_DATE
, ES.PRICE
/* , BT.BUYER_TYPE_CODE */
, PCA.ADDR1
, PCA.ADDR2
, PCA.CITY
, PCA.POSTAL_CODE
, PCE.EMAIL
, PC.FORMATTED_NAME
, PCP.PHONE_NUMBER
, PCP.SECONDARY
/* check all these columns exist in the select clause
ORDER BY
ES.TRANSACTION_ID DESC
, PCP.SECONDARY DESC
, PCP.PHONE_NUMBER DESC
, PC.FORMATTED_NAME DESC
, PCE.EMAIL DESC
, PCA.POSTAL_CODE DESC
, PCA.CITY DESC
, PCA.ADDR2 DESC
, PCA.ADDR1 DESC
, BT.BUYER_TYPE_CODE DESC
, ES.PRICE DESC
*/
When you come the the final clause: ORDER BY you can ONLY reference columns that exist in the select clause. This example would FAIL
select column1 from table1 group by column1 order by fred
but this would work:
select column1 from table1 group by column1 order by column1
Your column aliases 'Paid Child', 'Free Child', 'Comps' should not be wrapped in single quotes. You should be using double quotes like you are already for "Adults".
So they should instead be:
"Paid Child"
"Free Child"
"Comps"
Or better yet, consider naming your aliases without any spaces, so you don't have to worry about wrapping the aliases in anything, like this:
paid_child
free_child
comps
Documentation on Database Object Names and Qualifiers:
Database Object Naming Rules
Every database object has a name. In a SQL statement, you represent the name of an object with a quoted identifier or a nonquoted identifier.
A quoted identifier begins and ends with double quotation marks ("). If you name a schema object using a quoted identifier, then you must use the double quotation marks whenever you refer to that object.
A nonquoted identifier is not surrounded by any punctuation.
...
Although column aliases, table aliases, usernames, and passwords are not objects or parts of objects, they must also follow these naming rules unless otherwise specified in the rules themselves.

Result set different from query than from stored procedure in Oracle

I have a sp that is giving me wrong results. That sp returns a cursor which is missing some rows. Now
When we fire the query within the sp directly in a query window, correct results come up. The only difference in this query is that instead of a cursor, the query directly outputs the result set; i.e. no cursor is opened when we fire the query directly.
When we recompile the sp, correct results come up for some time, and then it is back to missing rows after some time.
Have not been able to make much progress on this. Any pointers on what to look for, how to make progress etc. are much appreciated. I can paste the sp in here if needed, and probably the sp can be rewritten in some other way to make the problem go away, but I am more interested in knowing root cause - what could be causing this strange problem?
SP
CREATE OR REPLACE PROCEDURE "SP_GETTAXSETTINGCHANNELINFO" (v_bid varchar2,
p_rdTaxSetting out SYS_REFCURSOR) as
vT_bid varchar2(100);
begin
vT_bid := v_bid;
Open p_rdTaxSetting for
Select taxmappingid, CHANNELNAME,PROPERTYNAME, PROPERTYID, TAXTYPE, ISPARTIALLYINCLUSIVE, PARTIALLYINCLUSIVEVALUE
FROM (
WITH ChannelData AS (
SELECT ONDEMAND_SELECTED_SOURCES AS OnDemandChannels,
SCHEDULED_SELECTED_SOURCES AS ScheduledChannels
FROM SUB_ORDER S
INNER JOIN CONTACT C ON S.Supplier_ID = C.Contact_ID
WHERE C.BID = vT_bid
AND S.STATUS='Complete' and current_ind = 1
),
Property_Data As (
SELECT
SC.PROPERTY_NUM AS PropertyID
,SC.Company_Name as PropertyName
,SC.competitor_number as PropertyOrder
FROM SUB_ORDER S
INNER JOIN CONTACT C ON S.Supplier_ID = C.Contact_ID
INNER JOIN SUB_ORDER_COMPETITOR SC ON S.Order_Id = SC.Order_ID
WHERE C.BID = vT_bid
AND S.STATUS='Complete' and current_ind = 1
),
ODChannel as (
select regexp_substr (OnDemandChannels, '[^,]+', 1, rn) as Channel
from (Select OnDemandChannels From ChannelData)
cross join
(select rownum rn
from (select max (length (regexp_replace (OnDemandChannels, '[^,]+'))) + 1 max_value
from (Select OnDemandChannels From ChannelData))
connect by level <= max_value)
where regexp_substr (OnDemandChannels, '[^,]+', 1, rn) is not null
),
SCDChannel As (
select regexp_substr (ScheduledChannels, '[^,]+', 1, rn) as Channel
from (Select ScheduledChannels From ChannelData)
cross join
(select rownum rn
from (select max (length (regexp_replace (ScheduledChannels, '[^,]+'))) + 1 max_value
from (Select ScheduledChannels From ChannelData))
connect by level <= max_value)
where regexp_substr (ScheduledChannels, '[^,]+', 1, rn) is not null
),
ChaData1 As (
Select
CASE lower(Channel)
WHEN 'galileo' then 'GDS'
else Channel
end AS Channel
from ODChannel
),
ChaData2 As (
Select CASE lower(Channel)
WHEN 'galileo' then 'GDS'
else Channel
end AS Channel FROM SCDChannel
),
PropData As (
Select Distinct PropertyID,PropertyName from Property_Data
order by PropertyID
),
AllChannel AS (
Select Channel From ChaData1
union
Select Channel From ChaData2
),
Prop_Cha_Data As (
Select Distinct Channel,PropertyID,PropertyName from AllChannel,Property_Data
order by Channel,PropertyID
),
Tax_Channel_Prop_Exists AS ( select
CASE lower(CHANNELNAME)
WHEN 'galileo' then 'GDS'
else CHANNELNAME
end AS CHANNELNAME,
PropertyName As PROPERTYNAME, TCP.PROPERTYID As PROPERTYID,TAXTYPE,
ISPARTIALLYINCLUSIVE, PARTIALLYINCLUSIVEVALUE,taxmappingid
from TAXSETTING_CHANNEL_PROPERTY TCP
INNER JOIN AllChannel PC On TRIM (CASE lower(TCP.CHANNELNAME)
WHEN 'galileo' then 'GDS'else TCP.CHANNELNAME end) = TRIM(PC.Channel)
INNER JOIN PropData CP On TCP.PROPERTYID = CP.PropertyID
where TCP.BID=vT_bid
)
Select Distinct taxmappingid, CHANNELNAME,PROPERTYNAME, PROPERTYID, TAXTYPE, ISPARTIALLYINCLUSIVE, PARTIALLYINCLUSIVEVALUE from Tax_Channel_Prop_Exists
UNION ALL
select DISTINCT 0 As taxmappingid, PCD.Channel As CHANNELNAME,
PCD.PropertyName As PROPERTYNAME ,
PCD.propertyid As PROPERTYID,
-1 As TAXTYPE,
0 As ISPARTIALLYINCLUSIVE,
'' As PARTIALLYINCLUSIVEVALUE
FROM Prop_Cha_Data PCD
WHERE NOT EXISTS (
Select taxmappingid FROM Tax_Channel_Prop_Exists E
WHERE E.channelname = PCD.Channel AND
E.propertyid = PCD.propertyid )
)
Order by PROPERTYNAME,CHANNELNAME ;
end SP_GetTaxSettingChannelInfo;

CROSS APPLY too slow for running total - TSQL

Please see my code below as it is running too slowly with the CROSS APPLY.
How can I remove the CROSS APPLY and add something else that will run faster?
Please note I am using SQL Server 2008 R2.
;WITH MyCTE AS
(
SELECT
R.NetWinCURRENCYValue AS NetWin
,dD.[Date] AS TheDay
FROM
dimPlayer AS P
JOIN
dbo.factRevenue AS R ON P.playerKey = R.playerKey
JOIN
dbo.vw_Date AS dD ON Dd.dateKey = R.dateKey
WHERE
P.CustomerID = 12345)
SELECT
A.TheDay AS [Date]
,ISNULL(A.NetWin, 0) AS NetWin
,rt.runningTotal AS CumulativeNetWin
FROM MyCTE AS A
CROSS APPLY (SELECT SUM(NetWin) AS runningTotal
FROM MyCTE WHERE TheDay <= A.TheDay) AS rt
ORDER BY A.TheDay
CREATE TABLE #temp (NetWin money, TheDay datetime)
insert into #temp
SELECT
R.NetWinCURRENCYValue AS NetWin
,dD.[Date] AS TheDay
FROM
dimPlayer AS P
JOIN
dbo.factRevenue AS R ON P.playerKey = R.playerKey
JOIN
dbo.vw_Date AS dD ON Dd.dateKey = R.dateKey
WHERE
P.CustomerID = 12345;
SELECT
A.TheDay AS [Date]
,ISNULL(A.NetWin, 0) AS NetWin
,SUM(B.NetWin) AS CumulativeNetWin
FROM #temp AS A
JOIN #temp AS B
ON A.TheDay >= B.TheDay
GROUP BY A.TheDay, ISNULL(A.NetWin, 0);
Here https://stackoverflow.com/a/13744550/613130 it's suggested to use recursive CTE.
;WITH MyCTE AS
(
SELECT
R.NetWinCURRENCYValue AS NetWin
,dD.[Date] AS TheDay
,ROW_NUMBER() OVER (ORDER BY dD.[Date]) AS RN
FROM dimPlayer AS P
JOIN dbo.factRevenue AS R ON P.playerKey = R.playerKey
JOIN dbo.vw_Date AS dD ON Dd.dateKey = R.dateKey
WHERE P.CustomerID = 12345
)
, MyCTERec AS
(
SELECT C.TheDay AS [Date]
,ISNULL(C.NetWin, 0) AS NetWin
,ISNULL(C.NetWin, 0) AS CumulativeNetWin
,C.RN
FROM MyCTE AS C
WHERE C.RN = 1
UNION ALL
SELECT C.TheDay AS [Date]
,ISNULL(C.NetWin, 0) AS NetWin
,P.CumulativeNetWin + ISNULL(C.NetWin, 0) AS CumulativeNetWin
,C.RN
FROM MyCTERec P
INNER JOIN MyCTE AS C ON C.RN = P.RN + 1
)
SELECT *
FROM MyCTERec
ORDER BY RN
OPTION (MAXRECURSION 0)
Note that this query will work if you have 1 record == 1 day! If you have multiple records in a day, the results will be different from the other query.
As I said here, if you want really fast calculation, put it into temporary table with sequential primary key and then calculate rolling total:
create table #Temp (
ID bigint identity(1, 1) primary key,
[Date] date,
NetWin decimal(29, 10)
)
insert into #Temp ([Date], NetWin)
select
dD.[Date],
sum(R.NetWinCURRENCYValue) as NetWin,
from dbo.dimPlayer as P
inner join dbo.factRevenue as R on P.playerKey = R.playerKey
inner join dbo.vw_Date as dD on Dd.dateKey = R.dateKey
where P.CustomerID = 12345
group by dD.[Date]
order by dD.[Date]
;with cte as (
select T.ID, T.[Date], T.NetWin, T.NetWin as CumulativeNetWin
from #Temp as T
where T.ID = 1
union all
select T.ID, T.[Date], T.NetWin, T.NetWin + C.CumulativeNetWin as CumulativeNetWin
from cte as C
inner join #Temp as T on T.ID = C.ID + 1
)
select C.[Date], C.NetWin, C.CumulativeNetWin
from cte as C
order by C.[Date]
I assume that you could have duplicates dates in the input, but don't want duplicates in the output, so I grouped data before puting it into the table.

Resources