Reputation: 244
The below code is taking too long to run. I know it is the code:
case when (select (concat(alias,' - ',firm))
from publiccontact where replace(replace(replace(replace(businessnumber,'+44 (','0'),') ',''),'+44','0'),')','') = tax.callerid1 and businessnumber <> ''
limit 1
) is not null then
(select (concat(alias,' - ',firm))
from publiccontact where replace(replace(replace(replace(businessnumber,'+44 (','0'),') ',''),'+44','0'),')','') = tax.callerid1 and businessnumber <> ''
limit 1
) else (select (concat(alias,' - ',firm))
from publiccontact where replace(replace(replace(replace(mobilenumber,'+44 (','0'),') ',''),'+44','0'),')','') = tax.callerid1 and mobilenumber <> ''
limit 1
)
end as callername,
Could I make my case and sub selects more efficient? My code works, but is definitely not efficient.
select date(instime) as date,
Pkey as ID,
subscriber as user,
SUBSCRIBERNAME as userName,
case when (select (concat(alias,' - ',firm))
from publiccontact where replace(replace(replace(replace(businessnumber,'+44 (','0'),') ',''),'+44','0'),')','') = tax.callerid1 and businessnumber <> ''
limit 1
) is not null then
(select (concat(alias,' - ',firm))
from publiccontact where replace(replace(replace(replace(businessnumber,'+44 (','0'),') ',''),'+44','0'),')','') = tax.callerid1 and businessnumber <> ''
limit 1
) else (select (concat(alias,' - ',firm))
from publiccontact where replace(replace(replace(replace(mobilenumber,'+44 (','0'),') ',''),'+44','0'),')','') = tax.callerid1 and mobilenumber <> ''
limit 1
)
end as callername,
(select (concat(alias,' - ',firm))
from publiccontact where replace(replace(replace(replace(businessnumber,'+44 (','0'),') ',''),'+44','0'),')','') = tax.destinationnumber1 and businessnumber <> ''
limit 1
)
as destinationname,
case when direction = 1 then 'incoming' else 'outgoing' end as direction,
case when CALLDESTINATION = 1 then 'public' else 'private' end as destination,
startdate as StartDate,
starttime as StartTime,
duration as DuractionSec,
TIMETOANSWER as TimeAnswerSec,
TAXCHARGES as Charges,
coalesce(callerid1,callerid2,'') as CallerID,
coalesce(destinationnumber1,destinationnumber2,'') as DestinationNumber,
ORIGINSUBSCRIBER as UserNumber,
completed as CallCompleted,
coalesce(case when concat(DIRECTION,CALLDESTINATION,CALLTYPE,CALLHANDLING) = 2123 then 'Incoming Call Transfered External' else null end,
case when concat(DIRECTION,CALLDESTINATION,CALLTYPE,CALLHANDLING) = 1132 then 'Incoming Call Transfered External' else null end,
case when concat(DIRECTION,CALLDESTINATION,CALLTYPE,CALLHANDLING) = 1131 then 'Incoming Call Transfered Interal' else null end,
case when concat(DIRECTION,CALLDESTINATION,CALLTYPE,CALLHANDLING) = 1132 then 'Incoming Call Transfered Interal' else null end,
case when concat(DIRECTION,CALLDESTINATION,CALLTYPE,CALLHANDLING) = 1233 then 'AMC Call Answered' else null end,
case when concat(DIRECTION,CALLDESTINATION,CALLTYPE,CALLHANDLING) = 2122 then 'Incoming DDI call answered by GSM' else null end,
case when concat(DIRECTION,CALLDESTINATION,CALLTYPE,CALLHANDLING) = 1232 then 'AMCOutgoing' else null end,
case when concat(DIRECTION,CALLDESTINATION,CALLTYPE,CALLHANDLING) = 2111 then 'OutgoingCallTransferedInternally' else null end,
case when concat(DIRECTION,CALLDESTINATION,CALLTYPE,CALLHANDLING) = 2121 then 'OutgoingCallTransferedInternally' else null end,
case when concat(DIRECTION,CALLDESTINATION,CALLTYPE,CALLHANDLING) = 2123 then 'OutgoingCallTransferedtoExternal' else null end,
case when concat(DIRECTION,CALLDESTINATION,CALLTYPE,CALLHANDLING) = 1252 then 'IncomingCallPrivateNetworkCallShouldBeIgnored' else null end,
case when concat(DIRECTION,CALLDESTINATION,CALLTYPE,CALLHANDLING) = 1143 then 'IncACDCallUnaws' else null end,
case when concat(DIRECTION,CALLDESTINATION,CALLTYPE,CALLHANDLING) = 1142 then 'IncACDCallAnswered' else null end,'') as type
from taxticketitem tax;
Kind regards
Upvotes: 1
Views: 148
Reputation: 275
My solution would be, i don't know if it really brings performance plus...
Doing a search with REGEX and perform the replace for displaying after retrieving the corresponding number.
On the other side you could spare one replace and use a LTRIM
instat.
First replace '(' then ')', then '+44' and last use a LTRIM to get rid of the space.
Upvotes: 0
Reputation: 51494
I would add a subquery for your direction/calldestination/calltype/callhandling thing.
select
...,
CallDescription
from taxticketitem
left join
(
Select 2 as Direction, 1 as CallDestination, 2 as CallType, 3 as CallHandling, 'Incoming Call Transfered External' as CallDescription
union
select 1,1,3,2, 'Incoming Call Transfered External'
...
) v
on taxticketitem.Direction = v.Direction
and taxticketitem.CallDestination = v.CallDestination
and taxticketitem.CallType = v.CallType
and taxticketitem.CallHandling = v.CallHandling
Upvotes: 0
Reputation: 146
Using an IFNULL expression would cut out an additional select in the case when the first select is not null...
ifnull (
(select (concat(alias,' - ',firm))
from publiccontact where replace(replace(replace(replace(businessnumber,'+44 (','0'),') ',''),'+44','0'),')','') = tax.callerid1 and businessnumber <> ''
limit 1),
(select (concat(alias,' - ',firm))
from publiccontact where replace(replace(replace(replace(mobilenumber,'+44 (','0'),') ',''),'+44','0'),')','') = tax.callerid1 and mobilenumber <> ''
limit 1)
)
Upvotes: 1