# Database Schema & Queue Structure

This diagram shows the entity relationships between BundleBacktestRequest, BacktestJob, and User entities.

```mermaid
erDiagram
    BundleBacktestRequest ||--o{ BacktestJob : "has many"
    BacktestJob }o--|| User : "belongs to"
    
    BundleBacktestRequest {
        UUID RequestId PK
        INT UserId FK
        STRING Status
        INT TotalBacktests
        INT CompletedBacktests
        INT FailedBacktests
        DATETIME CreatedAt
        DATETIME CompletedAt
        STRING UniversalConfigJson
        STRING DateTimeRangesJson
        STRING MoneyManagementVariantsJson
        STRING TickerVariantsJson
    }
    
    BacktestJob {
        UUID Id PK
        UUID BundleRequestId FK
        STRING JobType
        STRING Status
        INT Priority
        TEXT ConfigJson
        TEXT CandlesJson
        INT ProgressPercentage
        INT CurrentBacktestIndex
        INT TotalBacktests
        INT CompletedBacktests
        DATETIME CreatedAt
        DATETIME StartedAt
        DATETIME CompletedAt
        TEXT ResultJson
        TEXT ErrorMessage
        STRING AssignedWorkerId
        DATETIME LastHeartbeat
    }
    
    User {
        INT Id PK
        STRING Name
    }
```

## Table Descriptions

### BundleBacktestRequest
- Represents a bundle of multiple backtest jobs
- Contains variant configurations (date ranges, money management, tickers)
- Tracks overall progress across all jobs

### BacktestJob
- Individual backtest execution unit
- Contains serialized config and candles
- Tracks progress, worker assignment, and heartbeat
- Links to parent bundle request

### Key Indexes
- `idx_status_priority`: For efficient job claiming (Status, Priority DESC, CreatedAt)
- `idx_bundle_request`: For bundle progress queries
- `idx_assigned_worker`: For worker health monitoring

