I have two tables which I need to perform a left outer join on to get the result set I want. The query I have now works but its taking too long to process. Any suggestions?
Query:
SELECT Date_format(a.call_date, '%Y-%m-%d') Call_Date,
Date_format(a.call_date, '%H:%i:%s') Call_Time,
a.lead_id,
customer_number,
status,
a.call_type,
agent,
skill,
campaign,
disposition,
hangup,
a.uniqueid,
time_to_answer,
talk_time,
hold_sec,
wrapup_sec,
Date_format(start_time, '%H:%i:%s') Start_Time,
Date_format(end_time, '%H:%i:%s') End_Time,
Ifnull(a.transfered, b.transfered) AS transfer,
comments,
location,
duration,
handling_time,
number_dialed AS DID
FROM cdr_temp a
LEFT OUTER JOIN (SELECT USER,
Substring(number_dialed, 18, 11) AS transfered,
uniqueid
FROM transfertable)
b
ON a.uniqueid = b.uniqueid
WHERE a.call_date BETWEEN '2019-01-01 00:00:00' AND '2019-03-23 00:00:00'
GROUP BY a.lead_id,
b.uniqueid
Index:
uniqueid and call_date for transfertable
uniqueid and lead_id for cdr_temp
Explain on query
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE a ALL NULL NULL NULL NULL 1333 Using where; Using temporary; Using filesort
1 SIMPLE transfertable ref uniqueid uniqueid 22 test.a.uniqueid 1
PS: I need to concatenate skills and number_dialed on GROUP BY. I've tried using GROUP_CONCAT but it didn't work and I have no idea why.
I don't really see the point of the subquery on the transfertable
. But, looking closely at that subquery, I can see that you are calling SUBSTRING
there. This almost certainly means that MySQL won't be able to use any index to improve that join. One approach here would be to create a temporary table containing the substring of the number_dialed
as a bona-fide column.
CREATE TEMPORARY TABLE transfertable_temp (
INDEX idx (uniqueid, transfered)
)
SELECT uniqueid, SUBSTRING(number_dialed, 18, 11) AS transfered
FROM transfertable;
Joining to this temporary table (in lieu of a materialized view, which MySQL does not yet support) should improve the performance of the join, should MySQL choose to use it. Note that I also include the substring number in the index, to cover that other column being used.
Here is your updated query:
SELECT
DATE_FORMAT(a.call_date, '%Y-%m-%d') Call_Date,
DATE_FORMAT(a.call_date, '%H:%i:%s') Call_Time,
a.lead_id,
customer_number,
status,
a.call_type,
agent,
skill,
campaign,
disposition,
hangup,
a.uniqueid,
time_to_answer,
talk_time,
hold_sec,
wrapup_sec,
Date_format(start_time, '%H:%i:%s') Start_Time,
Date_format(end_time, '%H:%i:%s') End_Time,
IFNULL(a.transfered, b.transfered) transfer,
comments,
location,
duration,
handling_time,
b.number_dialed DID
FROM cdr_temp a
LEFT JOIN transfertable_temp b
ON a.uniqueid = b.uniqueid
WHERE
a.call_date BETWEEN '2019-01-01 00:00:00' AND '2019-03-23 00:00:00';
Note: Using GROUP BY
in your current query makes no sense, especially since you don't even call any aggregate functions. If you think you have a need for aggregation, then edit your question and tell us why.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.