In case you have your own slow SQL query, you can optimize it automatically here.
For the query above, the following recommendations will be helpful as part of the SQL tuning process.
You'll find 3 sections below:
ALTER TABLE `invoice` ADD INDEX `invoice_idx_patientid_invoicednumber` (`patientid`,`invoicednumber`);
ALTER TABLE `payment` ADD INDEX `payment_idx_patientid_paymentdate` (`patientid`,`paymentdate`);
SELECT
patient.patientid,
patient.firstname,
patient.lastname,
patient.mobilephone,
patient.email,
Format(Coalesce((SELECT
Sum(invoice.amount) - Sum((invoice.amount * (invoice.discount / 100)))
FROM
invoice
WHERE
invoice.patientid = patient.patientid
AND invoice.invoicednumber > 0) - (SELECT
Sum(payment.amount)
FROM
payment
WHERE
payment.patientid = patient.patientid),
0),
0) AS answer,
Date_format((SELECT
Max(payment.paymentdate)
FROM
payment
WHERE
payment.patientid = patient.patientid),
'%d-%m-%Y') AS lastpaymentdate
FROM
patient
WHERE
1