The patient dashboard takes 2-3 minutes to load when a patient has over 500 encounters, causing significant delays in workflow. The issue appears to be related to processing a large volume of encounter data during dashboard loading. Optimizing performance in such cases is essential to improve usability and efficiency.
$appendSql = '';
if ($eid) {
$appendSql = " AND encounter = ?";
$bindingValue[] = $eid;
}
$sql = "select
sum(
CASE WHEN ? = 1
AND frresult.last_level_closed < frresult.insurance_data_count
AND frresult.stmt_count = 0 THEN (
SELECT
COALESCE(
SUM(b.fee),
0.00
) - COALESCE(
SUM(a.pay_amount),
0.00
) AS ptbal
FROM
billing b
LEFT JOIN ar_activity a ON b.pid = a.pid
AND b.encounter = a.encounter
AND a.deleted IS NULL
AND a.payer_type = 0
WHERE
b.pid = frresult.pid
AND b.encounter = frresult.encounter
AND b.code_type = 'copay'
AND b.activity = 1
) ELSE (
SELECT
( (
SELECT
COALESCE(SUM(fee), 0)
FROM
billing
WHERE
pid = frresult.pid
AND encounter = frresult.encounter
AND activity = 1
) + (
SELECT
COALESCE(SUM(fee), 0)
FROM
drug_sales
WHERE
pid = frresult.pid
AND encounter = frresult.encounter
) - (
SELECT
COALESCE(SUM(pay_amount), 0) + COALESCE(SUM(adj_amount), 0)
FROM
ar_activity
WHERE
deleted IS NULL
AND pid = frresult.pid
AND encounter = frresult.encounter
)
) as amount
) END
) AS balance
from
(
SELECT
fe.pid,
fe.encounter,
fe.last_level_billed,
fe.last_level_closed,
fe.stmt_count,
(
SELECT
count(idata.id)
FROM
insurance_data idata
WHERE
idata.pid = fe.pid
AND idata.type IN (
‘primary’, ‘secondary’, ‘tertiary’
)
AND (
idata.date <= DATE(fe.date)
OR idata.date IS NULL
)
AND idata.provider IS NOT NULL
AND idata.provider != ‘’
ORDER BY
FIELD(
idata.type, ‘primary’, ‘secondary’,
‘tertiary’
),
idata.date DESC
LIMIT
3
) as insurance_data_count
FROM
form_encounter fe
WHERE
pid = ? " . $appendSql . "
) as frresult ;";