Hi Asha Misal
I analyzed this and can guide you step by step process
Step1:Create a new OQL DataSet in Mendix that returns only the latest StatusHistory record per MPDI. Name it something like DS_LatestStatusPerMPDI
SELECT
StatusHist/InServiceOutbound.MemberPayrollDeductionInstructionStatusHistory_MemberPayrollDeductionInstruction AS MPDI_ID,
StatusHist/DeductionInstructionStatus AS LatestStatus,
StatusHist/StatusEffectiveDate AS LatestEffectiveDate
FROM InServiceOutbound.MemberPayrollDeductionInstructionStatusHistory AS StatusHist
WHERE StatusHist/Id = (
SELECT InnerHist/Id
FROM InServiceOutbound.MemberPayrollDeductionInstructionStatusHistory AS InnerHist
WHERE InnerHist/InServiceOutbound.MemberPayrollDeductionInstructionStatusHistory_MemberPayrollDeductionInstruction
= StatusHist/InServiceOutbound.MemberPayrollDeductionInstructionStatusHistory_MemberPayrollDeductionInstruction
ORDER BY InnerHist/StatusEffectiveDate DESC, InnerHist/createdDate DESC
LIMIT 1
)
This isolates the expensive "find the latest status" logic into one dedicated dataset. Step2: Now replace your original WHERE EXISTS with a direct JOIN to the result above:
SELECT
MPDI/UniqueIdentifier AS UniqueID,
MPDI/TargetPayrollDate AS TargetDate,
-- ... all your existing columns ...
LatestStatus/DeductionInstructionStatus AS CurrentStatus
FROM InServiceOutbound.MemberPayrollDeductionInstruction AS MPDI
JOIN InServiceOutbound.MemberPayrollDeductionInstructionStatusHistory AS LatestStatus
ON LatestStatus/InServiceOutbound.MemberPayrollDeductionInstructionStatusHistory_MemberPayrollDeductionInstruction
= MPDI/Id
WHERE LatestStatus/DeductionInstructionStatus = $StatusParameter
ORDER BY UniqueID DESC
Replace $StatusParameter with your status value ('PDTR' or whichever status each tab uses).
Step3: Instead of hardcoding 'PDTR' in three separate queries, pass the status as a parameter into the DataSet.
In your page's microflow or data source
Tab 1 → call query with StatusParameter = 'PDTR'
Tab 2 → call query with StatusParameter = 'XXX'
Tab 3 → call query with StatusParameter = 'YYY' Or even You can retrive all data and filter using nanoflow but i wont suggest this. and more over try to add indexes also for faster.
I hope this helps!!