Bird
Raised Fist0
MLOpsdevops~20 mins

Trigger-based retraining (schedule, drift, performance) in MLOps - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Trigger-based Retraining Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Identify models scheduled for retraining this week

Given a table model_retraining_schedule with columns model_id, retrain_day (day of week), and last_retrained (date), write a query to find all models scheduled for retraining on the current day.

MLOps
SELECT model_id FROM model_retraining_schedule WHERE retrain_day = TO_CHAR(CURRENT_DATE, 'FMDay');
ASELECT model_id FROM model_retraining_schedule WHERE retrain_day = CURRENT_DATE;
BSELECT model_id FROM model_retraining_schedule WHERE retrain_day = EXTRACT(DOW FROM CURRENT_DATE);
CSELECT model_id FROM model_retraining_schedule WHERE retrain_day = DAYNAME(CURRENT_DATE);
DSELECT model_id FROM model_retraining_schedule WHERE retrain_day = TO_CHAR(CURRENT_DATE, 'FMDay');
Attempts:
2 left
💡 Hint

Use a function that returns the day name as text matching the retrain_day format.

query_result
intermediate
2:00remaining
Detect models with data drift exceeding threshold

Given a table model_performance with columns model_id, drift_score (float), and evaluation_date, write a query to find models with a drift_score greater than 0.3 in the last 7 days.

MLOps
SELECT DISTINCT model_id FROM model_performance WHERE drift_score > 0.3 AND evaluation_date >= CURRENT_DATE - INTERVAL '7 days';
ASELECT model_id FROM model_performance WHERE drift_score >= 0.3 AND evaluation_date > CURRENT_DATE - 7;
BSELECT DISTINCT model_id FROM model_performance WHERE drift_score > 0.3 AND evaluation_date >= CURRENT_DATE - INTERVAL '7 days';
CSELECT model_id FROM model_performance WHERE drift_score > 0.3 AND evaluation_date BETWEEN CURRENT_DATE - 7 AND CURRENT_DATE;
DSELECT DISTINCT model_id FROM model_performance WHERE drift_score > 0.3 AND evaluation_date > CURRENT_DATE - INTERVAL '7 days';
Attempts:
2 left
💡 Hint

Use interval syntax to subtract 7 days from current date and include all models with drift_score strictly greater than 0.3.

schema
advanced
3:00remaining
Design a table to track retraining triggers

You need to design a table to store retraining triggers for ML models. Each trigger can be based on schedule, data drift, or performance drop. Which schema design best supports flexible trigger types and efficient querying?

ACREATE TABLE retraining_triggers (trigger_id SERIAL PRIMARY KEY, model_id INT, trigger_type VARCHAR(20), threshold JSONB, last_triggered TIMESTAMP);
BCREATE TABLE retraining_triggers (model_id INT PRIMARY KEY, schedule_day VARCHAR(10), drift_threshold FLOAT, performance_threshold FLOAT);
CCREATE TABLE retraining_triggers (trigger_id SERIAL PRIMARY KEY, model_id INT, schedule_day VARCHAR(10), drift_threshold FLOAT, performance_threshold FLOAT);
DCREATE TABLE retraining_triggers (trigger_id SERIAL PRIMARY KEY, model_id INT, trigger_type VARCHAR(20), threshold FLOAT, last_triggered TIMESTAMP);
Attempts:
2 left
💡 Hint

Consider flexibility to store different threshold types per trigger and efficient querying.

optimization
advanced
3:00remaining
Optimize query for recent performance drops

Given a large model_performance table with millions of rows, which index will best optimize this query?

SELECT model_id FROM model_performance WHERE performance_metric < 0.7 AND evaluation_date > CURRENT_DATE - INTERVAL '30 days';
ACREATE INDEX idx_date_perf ON model_performance(evaluation_date, performance_metric);
BCREATE INDEX idx_eval_date ON model_performance(evaluation_date);
CCREATE INDEX idx_perf_date ON model_performance(performance_metric, evaluation_date);
DCREATE INDEX idx_perf_metric ON model_performance(performance_metric);
Attempts:
2 left
💡 Hint

Consider the order of columns in a composite index matching the query filters.

🔧 Debug
expert
3:00remaining
Identify error in retraining trigger query

Consider this query to find models needing retraining due to performance drop:

SELECT model_id FROM model_performance WHERE performance_metric < 0.8 AND evaluation_date > CURRENT_DATE - 7;

What error or issue will this query cause?

ATypeError because performance_metric is compared to a string
BReturns no rows because CURRENT_DATE - 7 is invalid date arithmetic
CSyntaxError due to missing INTERVAL keyword in date subtraction
DCorrect query with no errors
Attempts:
2 left
💡 Hint

Check how date arithmetic is done in SQL standard.

Practice

(1/5)
1. What is the main purpose of trigger-based retraining in machine learning operations?
easy
A. Automatically update models when data or performance changes
B. Manually retrain models on a fixed schedule
C. Store training data in a database
D. Visualize model performance metrics

Solution

  1. Step 1: Understand trigger-based retraining concept

    Trigger-based retraining means models update automatically when certain conditions happen, like data changes or performance drops.
  2. Step 2: Compare options to concept

    Only Automatically update models when data or performance changes describes automatic updates based on triggers, matching the concept.
  3. Final Answer:

    Automatically update models when data or performance changes -> Option A
  4. Quick Check:

    Trigger-based retraining = automatic updates [OK]
Hint: Triggers mean automatic updates, not manual tasks [OK]
Common Mistakes:
  • Confusing manual retraining with trigger-based retraining
  • Thinking triggers only store data
  • Assuming triggers visualize data
2. Which SQL statement correctly creates a trigger to start retraining after new data is inserted into a table named training_data?
easy
A. CREATE retrain_trigger AFTER INSERT ON training_data CALL start_retraining();
B. INSERT TRIGGER retrain_trigger ON training_data AFTER EXEC start_retraining();"
C. TRIGGER CREATE retrain_trigger ON training_data AFTER INSERT EXEC start_retraining();
D. CREATE TRIGGER retrain_trigger AFTER INSERT ON training_data FOR EACH ROW EXECUTE PROCEDURE start_retraining();

Solution

  1. Step 1: Recall correct SQL trigger syntax

    Standard SQL triggers use CREATE TRIGGER, specify timing (AFTER), event (INSERT), table, and procedure to execute.
  2. Step 2: Match syntax to options

    CREATE TRIGGER retrain_trigger AFTER INSERT ON training_data FOR EACH ROW EXECUTE PROCEDURE start_retraining(); matches correct syntax: CREATE TRIGGER retrain_trigger AFTER INSERT ON training_data FOR EACH ROW EXECUTE PROCEDURE start_retraining();
  3. Final Answer:

    CREATE TRIGGER retrain_trigger AFTER INSERT ON training_data FOR EACH ROW EXECUTE PROCEDURE start_retraining(); -> Option D
  4. Quick Check:

    Correct trigger syntax = CREATE TRIGGER retrain_trigger AFTER INSERT ON training_data FOR EACH ROW EXECUTE PROCEDURE start_retraining(); [OK]
Hint: Look for 'CREATE TRIGGER ... EXECUTE PROCEDURE' pattern [OK]
Common Mistakes:
  • Using CALL instead of EXECUTE PROCEDURE
  • Wrong order of keywords
  • Missing FOR EACH ROW clause
3. Given this trigger function in PostgreSQL:
CREATE OR REPLACE FUNCTION check_drift() RETURNS trigger AS $$
BEGIN
  IF NEW.error_rate > 0.1 THEN
    PERFORM start_retraining();
  END IF;
  RETURN NEW;
END;
$$ LANGUAGE plpgsql;

What happens when a new row with error_rate = 0.15 is inserted?
medium
A. The retraining procedure is called because error_rate > 0.1
B. Nothing happens because triggers don't run on INSERT
C. An error occurs due to syntax mistake
D. The row is rejected and not inserted

Solution

  1. Step 1: Analyze trigger function logic

    The function checks if NEW.error_rate > 0.1; if true, it calls start_retraining().
  2. Step 2: Apply condition to given data

    Since error_rate is 0.15, which is greater than 0.1, the retraining procedure is called.
  3. Final Answer:

    The retraining procedure is called because error_rate > 0.1 -> Option A
  4. Quick Check:

    error_rate 0.15 > 0.1 triggers retraining [OK]
Hint: Check condition in trigger function with inserted data [OK]
Common Mistakes:
  • Thinking triggers don't run on INSERT
  • Assuming syntax error without checking code
  • Believing row insertion fails
4. You wrote this trigger to start retraining on performance drop:
CREATE TRIGGER retrain_on_drop
AFTER UPDATE ON model_metrics
FOR EACH ROW
WHEN (NEW.accuracy < OLD.accuracy)
EXECUTE PROCEDURE start_retraining();

But retraining never starts. What is the likely problem?
medium
A. Triggers cannot run AFTER UPDATE events
B. The WHEN clause is not supported in all SQL dialects
C. start_retraining() must be a procedure, not a function
D. The trigger name is invalid

Solution

  1. Step 1: Understand WHEN clause support

    Not all SQL databases support the WHEN clause in triggers; some require condition checks inside the function.
  2. Step 2: Identify why retraining doesn't start

    If the database ignores the WHEN clause, the condition is never checked, so retraining never triggers.
  3. Final Answer:

    The WHEN clause is not supported in all SQL dialects -> Option B
  4. Quick Check:

    WHEN clause support varies by SQL dialect [OK]
Hint: Check if your SQL dialect supports WHEN in triggers [OK]
Common Mistakes:
  • Assuming triggers can't run AFTER UPDATE
  • Confusing functions and procedures
  • Thinking trigger names cause failure
5. You want to design a trigger-based retraining system that retrains a model only if both the data drift exceeds threshold and model accuracy drops below 90%. Which approach is best?
hard
A. Manually retrain the model when you notice performance issues
B. Create two separate triggers: one for drift and one for accuracy, each retraining independently
C. Create a trigger that calls a procedure checking both drift and accuracy before retraining
D. Schedule retraining daily regardless of drift or accuracy

Solution

  1. Step 1: Understand combined condition requirement

    The retraining should happen only if both drift and accuracy conditions are met together.
  2. Step 2: Evaluate options for combined logic

    Create a trigger that calls a procedure checking both drift and accuracy before retraining uses a single trigger calling a procedure that checks both conditions before retraining, ensuring both must be true.
  3. Step 3: Why other options fail

    Create two separate triggers: one for drift and one for accuracy, each retraining independently retrains independently on each condition, not requiring both. Schedule retraining daily regardless of drift or accuracy ignores conditions. Manually retrain the model when you notice performance issues is manual, not trigger-based.
  4. Final Answer:

    Create a trigger that calls a procedure checking both drift and accuracy before retraining -> Option C
  5. Quick Check:

    Combined condition needs single trigger with logic [OK]
Hint: Use one trigger with combined condition check procedure [OK]
Common Mistakes:
  • Using separate triggers causing unnecessary retraining
  • Ignoring condition checks in triggers
  • Relying on manual retraining only