Reputation: 31
I have the following SQL query and personally i can't find a way to optimize this further.
Do you agree with me?
This is the query:
select
t1.queue,
t.accountcode as agent,
TIMESTAMPDIFF(SECOND, time1, t.calldate) as recall_time
from
daily_cdr t
inner join
(
select
a.callid,
a.data2 as caller,
Cast(a.time as datetime) as time1,
a.queuename as queue
from
queue_log a
inner join
queue_log b
on a.callid = b.callid
where
a.event = "ENTERQUEUE"
)
t1
on t.dst = t1.caller
where
disposition = "ANSWERED"
and billsec > 0
and t.calldate > t1.time1
and t.accountcode is not null
group by
uniqueid;
It will be executed on a 5.5.68-MariaDB instance.
I started from a query made by several subqueries.
The next is only a part of the initial query to give you an idea of how it was:
select
(
select
data2
from
queue_log
where
callid = t.callid
and event = "ENTERQUEUE") as caller,
(
select
Cast(time as datetime)
from
queue_log
where
callid = t.callid
and event = "ENTERQUEUE"
)
as time1,
(
select
queuename
from
queue_log
where
callid = t.callid
and event = "ENTERQUEUE"
)
as queue
from
(
select
callid
from
queue_log
where
event in
(
"XITEMPTY",
"EXITWITHKEY",
"EXITWITHTIMEOUT",
"FULL",
"JOINEMPTY",
"JOINUNAVAIL",
"ABANDON"
)
)
t
I think I've done a good enough job, but surely someone else could do better or has some important advice to share.
Upvotes: 0
Views: 51
Reputation: 31
As suggested by @FanoFN and @danblack, the subquery isn't necessary so the query can be done in this way:
select
t1.queuename,
t.accountcode as agent,
TIMESTAMPDIFF(SECOND, Cast(t1.time as datetime), t.calldate) as recall_time
from
daily_cdr t
inner join
queue_log t1
on t.dst = t1.data2
where
t1.event = "ENTERQUEUE"
and t.disposition = "ANSWERED"
and t.billsec > 0
and t.calldate > Cast(t1.time as datetime)
and t.accountcode is not null
group by
t.uniqueid;
Upvotes: 1