-- Füge Index für vacation Tabelle hinzu
-- Optimiert die Abfrage: WHERE user_id = ? AND first_day >= ? ORDER BY first_day DESC
-- Ausführen mit: mysql -u root -p timeclock < add-vacation-index.sql

-- Prüfe zuerst ob der Index bereits existiert
SELECT COUNT(*) INTO @index_exists 
FROM information_schema.statistics 
WHERE table_schema = DATABASE() 
  AND table_name = 'vacation' 
  AND index_name = 'idx_vacation_user_first_day';

SET @sql = IF(@index_exists = 0,
  'CREATE INDEX idx_vacation_user_first_day ON vacation (user_id, first_day DESC)',
  'SELECT "Index idx_vacation_user_first_day existiert bereits" AS Info'
);

PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SELECT 'Index idx_vacation_user_first_day erfolgreich erstellt oder bereits vorhanden.' AS Status;

