SET @idx_exists := (
  SELECT COUNT(1)
  FROM information_schema.STATISTICS
  WHERE TABLE_SCHEMA = DATABASE()
    AND TABLE_NAME = 'orders'
    AND INDEX_NAME = 'idx_orders_customer_created_at'
);

SET @sql := IF(
  @idx_exists = 0,
  'ALTER TABLE orders ADD INDEX idx_orders_customer_created_at (customer_id, created_at)',
  'SELECT 1'
);

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