Join the forums now, and start posting to receive access to our Scripts Vault!
Lets say you have a table call Units which contains a list of army units and quantities each player has. Lets also say that every page displays the total number of units (across all unit types) in your army. Is it better to:A) Do a Select * from Units, then do a while loop to sum the total quantityB) Keep a sum on file in the general_user_info table?
select unit_type, sum(quantity) from units where owner = $player_id group by unit_type;
@Chris - I think I worry because I don't know what will happen if I don't worry about it first.
Option D, database trigger, which is what we use for the same exact scenario. When the table detects an insert or update we fire off the trigger and the summary columns in the player table are automatically updated with the new values.
Maybe triggerd would have a little bit better performance
Trigger is a procedure that is called upon a certain event.
Quote from: Nox on January 22, 2011, 09:26:56 AMMaybe triggerd would have a little bit better performanceI would say opposite. If there is any update trigger the storage engine has to check for its conditions upon each update of the table. Even if the triggered row is not in a query at the very least it has to compare all rows with some 'trigger is present' table/flag.