-- Performance indexes for tradebook queries
-- These indexes will significantly speed up the tradebook page queries
-- Index on trades table for common query patterns
-- This speeds up: WHERE a.user_id = ? AND t.account_id = ? AND t.symbol = ?
CREATE INDEX IF NOT EXISTS idx_trades_account_symbol ON trades(account_id, symbol);
-- Index for date filtering
CREATE INDEX IF NOT EXISTS idx_trades_trade_date ON trades(trade_date);
-- Composite index for account + date filtering
CREATE INDEX IF NOT EXISTS idx_trades_account_date ON trades(account_id, trade_date);
-- Index for symbol + date (used in trade grouping)
CREATE INDEX IF NOT EXISTS idx_trades_symbol_date ON trades(symbol, trade_date);
-- Index on accounts for user lookup (should already exist, but ensuring it's there)
CREATE INDEX IF NOT EXISTS idx_accounts_user_id ON accounts(user_id);
-- Index for checking authenticated accounts
CREATE INDEX IF NOT EXISTS idx_accounts_user_access_token ON accounts(user_id, access_token(50));
-- Show the indexes created
SHOW INDEX FROM trades WHERE Key_name LIKE 'idx_trades_%';
SHOW INDEX FROM accounts WHERE Key_name LIKE 'idx_accounts_%';