enhancement slow query

This commit is contained in:
Mohamad Ihsan
2026-04-25 22:19:36 +07:00
parent 676553409b
commit d84bc6941e
5 changed files with 700 additions and 481 deletions
+133
View File
@@ -0,0 +1,133 @@
# Cash-in Index Checklist
These indexes are suggested for the heavy cash-in read paths in `application/modules/cashin/cahsin.model.js`.
Apply them first in a staging environment and validate with `EXPLAIN (ANALYZE, BUFFERS)` before promoting to production.
## 1. Reservation balance lookup
Used by `getReserve()` and `getPartnerRecon()`.
```sql
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_tdrb_cash_in_lookup
ON trx.t_d_reservation_balance (c_partner_code, c_invoice_no, c_reservation_no)
WHERE c_status = 'A'
AND c_load_balance_no IS NOT NULL
AND c_cash_in_no IS NULL;
```
## 2. Reservation balance join key
Supports joins to payment recon and load balance.
```sql
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_tdrb_load_balance_no
ON trx.t_d_reservation_balance (c_load_balance_no);
```
```sql
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_tdrb_reservation_no
ON trx.t_d_reservation_balance (c_reservation_no);
```
## 3. Payment recon lookup
Used by `getReserve()`, `getPartnerRecon()`, and related sales/topup queries.
```sql
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_tdpr_reservation_status_payment
ON payment.t_d_payment_recon (c_reservation_no, d_payment)
WHERE c_status = 'S';
```
```sql
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_tdpr_sales_status_payment
ON payment.t_d_payment_recon_sales_kmt_mobile (c_sales_no, d_payment)
WHERE c_status = 'S';
```
```sql
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_tdpr_sales_internal_status_payment
ON payment.t_d_payment_recon_sales_internal (c_sales_no, d_payment)
WHERE c_status = 'S';
```
```sql
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_tdpr_topup_internal_status_payment
ON payment.t_d_payment_recon_topup_cashless_internal (c_topup_no, d_payment)
WHERE c_status = 'S';
```
## 4. Invoice lookup
Used by the cash-in and recon queries.
```sql
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_tdi_invoice_status_return
ON payment.t_d_invoice (c_invoice_no)
WHERE c_status <> 'X'
AND c_return_sap IS NOT NULL;
```
```sql
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_tdsi_invoice_status_return
ON payment.t_d_sales_invoice (c_invoice_no)
WHERE c_status <> 'X'
AND c_return_sap IS NOT NULL;
```
```sql
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_tdsi_internal_invoice_status_return
ON payment.t_d_sales_invoice_internal (c_invoice_no)
WHERE c_status <> 'X'
AND c_return_sap_fi IS NOT NULL;
```
```sql
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_tdsi_topup_internal_invoice_status_return
ON payment.t_d_topup_cashless_invoice_internal (c_invoice_no)
WHERE c_status <> 'X'
AND c_return_sap IS NOT NULL;
```
## 5. Cash-in detail anti-join
Used by `getSalesKMT()`, `getSalesInternal()`, and `getTopUpCashlessInternal()` to exclude already-posted rows.
```sql
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_tdcid_reservation_not_reversed
ON payment.t_d_cash_in_detail (c_reservation_no)
WHERE b_reversal IS NOT TRUE;
```
## 6. Cash-in code generation
Used by `generateCode()`.
```sql
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_tdcashin_code_prefix
ON payment.t_d_cash_in (c_cash_in_no);
```
If prefix lookups still scan too much data, consider a functional index on the prefix pattern that matches your code format.
## 7. Partner lookup
Used by recon queries that join through `public.t_m_partner`.
```sql
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_tmp_i_partner
ON public.t_m_partner (i_partner);
```
## Notes
- `CONCURRENTLY` avoids blocking writes, but it cannot run inside a transaction.
- Partial indexes are the best fit here because the queries always filter on status and posting flags.
- After creating indexes, run `EXPLAIN (ANALYZE, BUFFERS)` on:
- `getReserve()`
- `getPartnerRecon()`
- `getPartnerReconSales()`
- `getPartnerReconSalesInternal()`
- `getPartnerReconTopUpCashlessInternal()`
- If you want, I can also draft a migration file for these indexes in Knex format.