Scenario
1. Two tables exist called 'score_log' and 'games_played_count'
2. Computing the "count" of rows from score_log that uniquely describe the number of games that have been played by a given member. Each row is unique based on a combination of playerid, teamid and leagueid
3. The 'games_played_count' table contains a unique row based on playerid, teamid and leagueid with a variable column 'games_played_total' which is designed to be updated automatically when an INSERT OR DELETE occurs on the score_log table.
Current Solution
a. Two triggers defined on 'score_log' table. One trigger executes a stored procedure (defined below) using AFTER INSERT condition. The other trigger defined executes the stored procedure using AFTER DELETE condition.
b. The stored procedure that achieves the objective is defined below:
CREATE TEMPORARY TABLE temp_table AS
SELECT playerid, leagueid, COUNT(*) AS games_played_count
FROM score_log
GROUP BY playerid, leagueid;
UPDATE games_played_by_league
INNER JOIN temp_table ON games_played_by_league.playerid = temp_table.playerid AND games_played_by_league.leagueid = temp_table.leagueid
SET games_played_by_league.games_played_count = temp_table.games_played_count
WHERE games_played_by_league.playerid = temp_table.playerid AND games_played_by_league.leagueid = temp_table.leagueid;
Is this the most optimal way to achieve the desired outcome?