在mysql中缓慢加载子查询

时间:2018-12-04 09:44:11

标签: php mysql

SELECT * FROM (
    SELECT a.appointment_made_date_time as timestamp, a.appoimentid as deleteid, tx.treatment_name, tc.treatment_duration, a.clientname,a.clientmail,a.clientphone, a.date, a.time, w.workername, 'create' AS event, (SELECT COUNT(*) FROM appointments WHERE clientmail = a.clientmail OR clientphone = a.clientphone OR clientip = a.clientip OR identifier_id = a.identifier_id) as amount,(SELECT COUNT(*) FROM blacklist_request WHERE clientmail = a.clientmail OR clientphone = a.clientphone OR ip_address = a.clientip OR identifier_id = a.identifier_id) as blacklisted FROM appointments AS a LEFT JOIN treatments_connection AS tc ON a.treatment_connection_id = tc.connection_id LEFT JOIN barberworkeraccounts AS w ON tc.worker_id = w.workerid LEFT JOIN treatments AS tx ON tc.treatment_id = tx.treatment_id WHERE a.barberid = 58 AND RIGHT(a.clientip,2) != '.x'
    UNION
    SELECT a.appointment_made_datetime as timestamp, a.deleteid as deleteid, tx.treatment_name,tc.treatment_duration, a.clientname,a.clientmail,a.clientphone, a.appdate as date, a.apptime as time, w.workername, 'create' AS event, 'null' AS amount, 'null' AS blacklisted FROM deleted AS a LEFT JOIN treatments_connection AS tc ON a.treatment_connection_id = tc.connection_id LEFT JOIN barberworkeraccounts AS w ON tc.worker_id = w.workerid LEFT JOIN treatments AS tx ON tc.treatment_id = tx.treatment_id WHERE a.barberid = 58 AND RIGHT(a.appointment_made_ip,2) != '.x'
    UNION
    SELECT a.deleted_datetime as timestamp, a.deleteid as deleteid, tx.treatment_name, tc.treatment_duration, a.clientname,a.clientmail,a.clientphone, a.appdate as date, a.apptime as time, w.workername, 'delete' AS event, 'null' AS amount, 'null' AS blacklisted FROM deleted AS a LEFT JOIN treatments_connection AS tc ON a.treatment_connection_id = tc.connection_id LEFT JOIN barberworkeraccounts AS w ON tc.worker_id = w.workerid LEFT JOIN treatments AS tx ON tc.treatment_id = tx.treatment_id WHERE a.barberid = 58 AND RIGHT(a.appointment_deleted_ip,2) != '.x'
) entries
GROUP BY deleteid, event
ORDER BY timestamp DESC
LIMIT 90

由于我正在尝试运行此查询,因此子查询太慢。有时需要30秒或更长时间。

这是引起问题的子查询:

(SELECT COUNT(*) FROM appointments WHERE clientmail = a.clientmail OR clientphone = a.clientphone OR clientip = a.clientip OR identifier_id = a.identifier_id) as amount

appointments表中,有超过40K的行。而另一张桌子大约有300到400行。

是否有不使用子查询的替代方法?

0 个答案:

没有答案