views:

72

answers:

1

I am creating a light RPG game where a character is able to equipment a weapon, an armor and 2 accessories slot. Here's one possible solution:

equipped_equipment(<characterid>, <equipmentid>, <slotid>)
slot(<slotid>, slotname)
equipment(<equipmentid>, equipment_name, equipment_script_name)

So, to find out what weapon the character is equipping, I could do

SELECT equipmentid, equipment_name FROM equipment e, equipped_equipment eq
  WHERE e.equipmentid = eq.equipmentid AND eq.slotid = 'weapon' AND
  eq.characterid = 1

But how does this compare to something a schema like this?

equipment (<characterid>, weapon_slot, armor_slot, accessory1_sot, accessory2_slot)

Of course, using the three tables above (which is somewhat normalized - I could probably separate out equipment_name and put in into an equipment_details table or some sort) it allows me

  1. Add new slots easily
  2. Avoid blank entries (...anything else I may have missed out)

But the second, non-normalized solution allows me to get the IDs of all the equipment with one query, and adding a new slot is simply adding a new column. Which one is better in the long run? Suggestions and improvements welcomed!

+4  A: 

There are tons of dupes for this, I'll let somebody else find them :-)

I think a reasonable path to follow is:

  • Have a normalized database
  • Create denormalized views
  • Write your app to query those views
  • Profile (with tons of clients)
  • If db is certainly a bottleneck, profile harder to find out which tables are involved in it, then optimize the DB (indexes, query plans, and so on)
  • Profile again
  • If db is still a bottleneck, you can then denormalize to the views' interface (so you don't have to change the app)

The reason for this is that fixing data corruption, duplication or inconsistency bugs is hard and sometimes even impossible, so try as hard as you can to get the proper performance with normalization. Only then, if you've proven you can't get the desired performance, try denormalizing and only what needs to be denormalized.

Vinko Vrsalovic