Postgresql – Storing no of players the last seven days in an online game

postgresqlpostgresql-9.1

I want to plot the number of players, in the last seven days, of an MMORPG which haves 50~ servers. The system fetches the number of players in each server every 15 minutes and I'm not really sure how to store and update this information in an efficient way.

The easiest (but probably not so efficient) way I can think of is just having three columns, "time_fetched", "server", "no_players". Every 15 minutes it inserts 50 new rows and deletes the 50 oldest rows.

Maybe I could store weekday, hours and minute instead? So instead of deleting and inserting rows it just updates the correct rows? This would mean that I always have a fixed number of rows in the table (50 * 4 * 24 * 7 = 33600).

Also; would it be more efficient to store the server name as an enum instead of just strings (or some other way, maybe one table for each server)?

Best Answer

I would store the timestamp, server identifier, and number of players in just the way you thought of, deleting the oldest (if you need to) and adding new ones every 15 minutes.

Moving the server names into another "server" table and referencing the primary key in your player count table would be a more conventional normalised design.

With reference to the trigger, don't forget that you can perform both the insert and the delete in the same SQL statement with a common table expression: http://sqlfiddle.com/#!1/c8362/5