|
| 1 | +# Database Migrations |
| 2 | + |
| 3 | +This directory contains SQL migration scripts for the LogMk database. |
| 4 | + |
| 5 | +## Migration 001: Add Composite Indexes |
| 6 | + |
| 7 | +**File:** `001-add-composite-indexes.sql` |
| 8 | + |
| 9 | +### Problem |
| 10 | +The `/api/log/counts` and `/api/log/times` endpoints perform `GROUP BY Deployment, Pod` queries on the entire Log table. Without proper indexing, these queries can timeout on large datasets (>1M rows). |
| 11 | + |
| 12 | +### Solution |
| 13 | +Create a composite index on `(Deployment, Pod, TimeStamp)` columns. |
| 14 | + |
| 15 | +### When to Run |
| 16 | +- If you see timeout errors in the LogMkAgent logs when calling `/api/log/counts` or `/api/log/times` |
| 17 | +- If the API logs show: `PERFORMANCE WARNING: Missing composite index 'Deployment_Pod_TimeStamp_idx'` |
| 18 | +- During a scheduled maintenance window (recommended for production) |
| 19 | + |
| 20 | +### How to Run |
| 21 | + |
| 22 | +1. **Connect to your MySQL database:** |
| 23 | + ```bash |
| 24 | + mysql -h <host> -u <user> -p <database> |
| 25 | + ``` |
| 26 | + |
| 27 | +2. **Check if index exists:** |
| 28 | + ```sql |
| 29 | + SELECT COUNT(*) |
| 30 | + FROM information_schema.statistics |
| 31 | + WHERE table_schema = DATABASE() |
| 32 | + AND table_name = 'Log' |
| 33 | + AND index_name = 'Deployment_Pod_TimeStamp_idx'; |
| 34 | + ``` |
| 35 | + If the count is 0, the index doesn't exist and should be created. |
| 36 | + |
| 37 | +3. **Create the index:** |
| 38 | + ```sql |
| 39 | + CREATE INDEX Deployment_Pod_TimeStamp_idx ON `Log` (Deployment, Pod, TimeStamp); |
| 40 | + ``` |
| 41 | + |
| 42 | +4. **Monitor progress (in another MySQL session):** |
| 43 | + ```sql |
| 44 | + SHOW PROCESSLIST; |
| 45 | + ``` |
| 46 | + |
| 47 | +### Expected Time |
| 48 | +- **1M rows:** ~30 seconds |
| 49 | +- **10M rows:** ~5 minutes |
| 50 | +- **100M rows:** ~30-60 minutes |
| 51 | + |
| 52 | +### Impact |
| 53 | +- **During creation:** High CPU and I/O usage, queries to the Log table may be slower |
| 54 | +- **After creation:** 10-100x faster queries for counts and times endpoints |
| 55 | +- **Disk space:** Approximately 10-20% of the Log table size |
| 56 | + |
| 57 | +### Rollback |
| 58 | +If you need to remove the index: |
| 59 | +```sql |
| 60 | +DROP INDEX Deployment_Pod_TimeStamp_idx ON `Log`; |
| 61 | +``` |
| 62 | + |
| 63 | +### Alternative: Online Index Creation (MySQL 5.6+) |
| 64 | +For minimal downtime on large tables: |
| 65 | +```sql |
| 66 | +CREATE INDEX Deployment_Pod_TimeStamp_idx ON `Log` (Deployment, Pod, TimeStamp) ALGORITHM=INPLACE, LOCK=NONE; |
| 67 | +``` |
| 68 | + |
| 69 | +This allows concurrent reads/writes during index creation but may take longer. |
| 70 | + |
| 71 | +## Future Migrations |
| 72 | + |
| 73 | +Add new migration files with incremental numbering: |
| 74 | +- `002-description.sql` |
| 75 | +- `003-description.sql` |
| 76 | +- etc. |
0 commit comments