# Query Enhancement Quick Start Guide

## 🚀 Immediate Actions (Do These First)

### 1. Create Performance Indexes (5 minutes)

Run the index creation script in your database:

```bash
# Connect to your database and run:
psql $DATABASE_URL -f backend/database/performance_indexes.sql

# Or in Supabase SQL Editor, copy/paste the contents of:
# backend/database/performance_indexes.sql
```

**Expected Result:** 50-80% faster queries immediately

**Verify:** Check index usage:
```sql
SELECT indexname, idx_scan 
FROM pg_stat_user_indexes 
WHERE tablename = 'OptionsFlow_monthly' 
ORDER BY idx_scan DESC;
```

---

### 2. Test Query Profiling (2 minutes)

The profiler is already integrated! Just make a request:

```bash
# Make a normal options flow request
curl http://localhost:3010/api/options/flow?startDate=2024-01-15&endDate=2024-01-15

# Check performance stats
curl http://localhost:3010/api/performance/stats

# See slow queries
curl http://localhost:3010/api/performance/slow-queries
```

**Expected Result:** You'll see query timing in console logs and response metadata

---

### 3. Monitor Performance (Ongoing)

Access the performance dashboard:

```bash
# Comprehensive metrics
GET http://localhost:3010/api/performance/metrics

# Connection pool stats
GET http://localhost:3010/api/performance/pool-stats

# Get EXPLAIN plan for a query
POST http://localhost:3010/api/performance/explain
Body: {
  "query": "SELECT * FROM \"OptionsFlow_monthly\" WHERE \"CreatedDate\" = '2024-01-15'",
  "params": []
}
```

---

## 📊 What You Get

### Immediate Benefits

1. **Faster Queries** - Composite indexes reduce query time by 50-80%
2. **Performance Visibility** - See exactly how long queries take
3. **Slow Query Detection** - Automatically identify bottlenecks
4. **Connection Pool Monitoring** - Track database connection health

### New Endpoints

- `GET /api/performance/stats` - Query performance statistics
- `GET /api/performance/slow-queries` - List slow queries
- `GET /api/performance/metrics` - Comprehensive metrics
- `GET /api/performance/pool-stats` - Connection pool info
- `POST /api/performance/explain` - Get query execution plan
- `POST /api/performance/clear-metrics` - Reset metrics

---

## 🔍 Understanding the Results

### Query Metrics in Response

When you call `/api/options/flow`, you'll now see:

```json
{
  "success": true,
  "data": [...],
  "metadata": {
    "queryMetrics": {
      "queryName": "optionsFlowQuery",
      "duration": 234.56,  // milliseconds
      "memoryDelta": 12.34, // MB
      "rowCount": 150
    }
  }
}
```

### Performance Stats

```json
{
  "stats": {
    "count": 42,              // Total queries profiled
    "avgDuration": 234.56,    // Average query time
    "p95Duration": 450.23,     // 95th percentile
    "slowQueries": 3          // Queries > 1000ms
  }
}
```

---

## ⚠️ Important Notes

### Index Creation

- **Test in development first** - Indexes take time to build on large tables
- **Monitor disk space** - Indexes use additional storage
- **Run during off-hours** - Large index creation can lock tables

### Query Profiling

- **Overhead is minimal** - < 1ms per query
- **Metrics are in-memory** - Cleared on server restart
- **Slow query threshold** - Default is 1000ms (configurable)

---

## 🎯 Next Steps

After verifying the indexes work:

1. **Review slow queries** - Use `/api/performance/slow-queries` to identify bottlenecks
2. **Get EXPLAIN plans** - Use `/api/performance/explain` to see query execution details
3. **Implement materialized views** - See `QUERY_ENHANCEMENT_ANALYSIS.md` Section 2
4. **Add enhanced caching** - See `QUERY_ENHANCEMENT_ANALYSIS.md` Section 4

---

## 🐛 Troubleshooting

### Indexes Not Being Used

```sql
-- Check if indexes exist
SELECT indexname FROM pg_indexes WHERE tablename = 'OptionsFlow_monthly';

-- Force index usage (temporary)
SET enable_seqscan = off;
-- Run your query
SET enable_seqscan = on;
```

### Profiler Not Working

- Check that `QueryProfiler` is imported in `optionsFlow.js`
- Verify the route is using `QueryProfiler.profile()`
- Check console logs for errors

### High Memory Usage

- Check `queryMetrics.memoryDelta` in responses
- Consider implementing result streaming (see analysis doc)
- Review date range - smaller ranges = less memory

---

## 📚 Full Documentation

See `QUERY_ENHANCEMENT_ANALYSIS.md` for:
- Complete optimization strategies
- Materialized views implementation
- Advanced caching strategies
- Query builder patterns
- Connection pooling enhancements

---

## ✅ Success Checklist

- [ ] Indexes created and verified
- [ ] Query profiler working (check console logs)
- [ ] Performance endpoint accessible
- [ ] Slow queries identified
- [ ] Query times improved by 50%+

---

**Questions?** Check the full analysis document or review the code in:
- `backend/database/performance_indexes.sql`
- `backend/src/utils/queryProfiler.js`
- `backend/src/routes/performance.js`

