-- Performance indexes for existing installs. Safe to re-run - MySQL/MariaDB
-- will error harmlessly on "duplicate key name" if already applied; ignore
-- that specific error if it occurs.
--
-- Why these: the accounting reports (cash book, bank book, day book, trading
-- account, P&L, balance sheet, debtors/creditors) all filter ledger_entries
-- by account_id + a voucher_date range, or by voucher_date alone (day book).
-- Without an index covering both columns together, MySQL can only use the
-- existing single-column account_id index (auto-created by the FOREIGN KEY
-- constraint) and then scan/filter the date range row-by-row - fine on a
-- small dataset, increasingly slow as ledger_entries grows into the
-- thousands/millions of rows a real trading business accumulates over time.

ALTER TABLE ledger_entries ADD INDEX idx_account_date (account_id, voucher_date);
ALTER TABLE ledger_entries ADD INDEX idx_voucher_date (voucher_date);

-- Sales/purchase reports filter by date range; orders admin listing filters
-- by status and sorts by placed_at.
ALTER TABLE sales_invoices ADD INDEX idx_invoice_date (invoice_date);
ALTER TABLE purchase_invoices ADD INDEX idx_purchase_date (purchase_date);
ALTER TABLE orders ADD INDEX idx_placed_at (placed_at);
ALTER TABLE orders ADD INDEX idx_order_status (order_status);
