我有以下查询,执行时间超过3分钟。我还发布了解释计划。我该怎么做才能减少执行时间?由于左连接,它需要更多时间吗?
SELECT d.name as doc, SUM(
CASE
WHEN bill_type = 'registration' AND bop.particulars = 'Consultation Fee' THEN bop.amount
ELSE 0
END) AS 'op',
SUM(
CASE
WHEN bill_type = 'casuality' THEN b.payable_amount
ELSE 0
END) AS 'cas' ,
SUM(
CASE
WHEN bill_type = 'pharmacy' THEN b.payable_amount
ELSE 0
END) AS 'ph',
SUM(
CASE
WHEN bill_type = 'lab' THEN b.payable_amount
ELSE 0
END) AS 'lab' ,
SUM(
CASE
WHEN bill_type = 'xray' THEN b.payable_amount
ELSE 0
END) AS 'x' ,
SUM(
CASE
WHEN bill_type = 'audiology' THEN b.payable_amount
ELSE 0
END) AS 'au',
SUM(
CASE
WHEN bill_type = 'discharge' THEN b.payable_amount + IFNULL(w.balance, 0)
ELSE 0
END) AS 'ip'
FROM bills b
LEFT JOIN patient_wallet w on w.id = (SELECT x.id FROM patient_wallet x WHERE x.ip_id = b.ip_id ORDER BY x.created_at DESC LIMIT 1)
LEFT JOIN bill_contents_op bop on bop.bill_id = b.id
LEFT JOIN doctors d on d.id = b.doctor_id
WHERE b.doctor_id IN(41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80) AND CAST(b.created_at AS DATE) >= '2016-06-01' AND CAST(b.created_at AS DATE) <= '2016-06-30' AND b.is_cancelled = 0 AND b.is_deleted = 0 GROUP BY d.name
解释计划如下。
1
PRIMARY
b
NULL
ALL
NULL
NULL
NULL
NULL
161351
0.50
Using where; Using temporary; Using filesort
1
PRIMARY
w
NULL
eq_ref
PRIMARY
PRIMARY
4
func
1
100.00
Using where
1
PRIMARY
bop
NULL
ALL
NULL
NULL
NULL
NULL
124490
100.00
Using where; Using join buffer (Block Nested Loop)
1
PRIMARY
d
NULL
eq_ref
PRIMARY
PRIMARY
4
santhwana.b.doctor_id
1
100.00
NULL
2
DEPENDENT SUBQUERY
x
NULL
ALL
NULL
NULL
NULL
NULL
78
10.00
Using where; Using filesort