Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What database should I use in an app where my models don't represent different ideas, but instead different types with overlapping fields?

I'm building an application where I will be gathering statistics from a game. Essentially, I will be parsing logs where each line is a game event. There are around 50 different kinds of events, but a lot of them are related. Each event has a specific set of values associated with it, and related events share a lot of these attributes. Overall there are around 50 attributes, but any given event only has around 5-10 attributes.

I would like to use Rails for the backend. Most of the queries will be event type related, meaning that I don't especially care about how two event types relate with each other in any given round, as much as I care about data from a single event type across many rounds. What kind of schema should I be building and what kind of database should I be using?

Given a relational database, I have thought of the following:

  1. Have a flat structure, where there are only a couple of tables, but the events table has as many columns as there are overall event attributes. This would result in a lot of nulls in every row, but it would let me easily access what I need.

  2. Have a table for each event type, among other things. This would let me save space and improve performance, but it seems excessive to have that many tables given that events aren't really seperate 'ideas'.

  3. Group related events together, minimizing both the numbers of tables and number of attributes per table. The problem then becomes the grouping. It is far from clear cut, and it could take a long time to properly establish event supertypes. Also, it doesn't completely solve the problem of there being a fair amount of nils.

It was also suggested that I look into using a NoSQL database, such as MongoDB. It seems very applicable in this case, but I've never used a non-relational database before. It seems like I would still need a lot of different models, even though I wouldn't have tables for each one.

Any ideas?

like image 910
WhatAWorld Avatar asked Dec 04 '25 15:12

WhatAWorld


1 Answers

This feels like a great use case for MongoDB and a very awkward fit for a relational database.

The types of queries you would be making against this data is very key to best schema design but imagine that your documents (in a single collection similar to 1. above) look something like this:

{  "round" : 1,
   "eventType": "et1",
   "attributeName": "attributeValue",
   ...
}

You can easily query by round, by eventType, getting back all attributes or just a specified subset, etc.

You don't have to know up front how many attributes you might have, which ones belong with which event types, or even how many event types you have. As you build your prototype/application you will be able to evolve your model as needed.

There is a very large active community of Rails/MongoDB folks and there's a good chance that you can find a lot of developers you can ask questions and a lot of code you can look at as examples.

I would encourage you to try it out, and see if it feels like a good fit. I was going to add some links to help you get started but there are too many of them to choose from! Since you might have a question about whether to use an object mapper or not so here's a good answer to that.

A good write-up of dealing with dynamic attributes with Ruby and MongoDB is here.

like image 81
Asya Kamsky Avatar answered Dec 06 '25 04:12

Asya Kamsky