I'll try to give this as generically as I can so it's reusable.
I am running a site with a fairly large MySQL database which has grown to need some summary/rollup tables initialized. For the example's sake, let's say it's soccer statistics. Since I handle multiple soccer leagues in the same database, many of them play games of different lengths - for instance, indoor soccer leagues play four quarters while most outdoor leagues play halves.
I have three tables important to this exercise. I've redacted all of the fields that I don't consider significant to the answer I'm looking for.
GAME
`game`.id
`game`.home_team_id
`game`.away_team_id
`game`.number_of_periods
GOAL
// Records for each goal scored in the game
`goal`.id
`goal`.game_id
`goal`.team_id
`goal`.period_number
`goal`.player_id
`goal`.assist_player_id
PERIOD_SUMMARY
`period`.id
`period`.game_id
`period`.team_id
`period`.number
`period`.goals_scored
Ultimately I should have records for EVERY period played in the period summary table, regardless of whether or not a goal was scored. This table only needs to be initialized once, as it's fairly easy to add the appropriate zero-filled records via a trigger on game creation and fire on insert/update requests to update the period_summary table.
It is also fairly easy for me to group all of the goals and initialize the period summary table with the SUM(), what I am having a bit of trouble figuring out an efficient way to "fill" any periods that don't have a goal scored with a 0.
What I am trying to figure out is if it's easier/more efficient to:
- Write the trigger and prefill the entire period_summary table with 0-filled values, then run the query I already know to update the appropriate records for periods in which goals were scored.
- Use some other method (perhaps a temporary stored procedure?) that will only 0-fill records where there is not a match in the goals table.