SQL效率 - MySQL案例/子选择

时间:2012-10-03 12:15:28

标签: mysql sql

以下代码运行时间过长。我知道这是代码:

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  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;

亲切的问候

3 个答案:

答案 0 :(得分:1)

MySQL 5.0 IFNULL

在第一个选择不为空的情况下,使用IFNULL表达式会删除一个额外的选择...

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)
)

答案 1 :(得分:0)

我会为你的方向/ calldestination / calltype / callhandling事件添加一个子查询。

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

答案 2 :(得分:0)

我的解决方案是,我不知道它是否真的带来了性能......

使用REGEX进行搜索,并在检索相应的数字后执行替换显示。

另一方面,您可以节省一个替换并使用LTRIM instat。 首先替换&#39;(&#39;然后&#39;)&#39;,然后&#39; + 44&#39;并且最后使用LTRIM来摆脱空间。