views:

39

answers:

2

Hi,

We are building a set of features for our application. One of which is a list of recent user activities ala on SO. I'm having a little problem finding the best way to design the table for these activities.

Currently we have an Activities table with the following columns

UserId (Id of the user the activity is for)
Type (Type of activity - i.e. PostedInForum, RepliedInForum, WroteOnWall - it's a tinyint with values taken from an enumerator in C#)
TargetObjectId  (An id of the target of the activity. For PostedInForum this will be the Post ID, for WroteOnWall this will be the ID of the User whose wall was written on)
CreatedAtUtc (Creationdate)

My problem is that TargetObjectId column doesn't feel right. It's a soft link - no foreign keys and only a knowledge about the Type tells you what this column really contains.

Does any of you have a suggestion on an alternate/better way of storing a list of user activites?

I should also mention that the site will be multilingual, so you should be able to see the activity list in a range of languages - that's why we haven't chosen for instance to just put the activity text/html in the table.

Thanks

+1  A: 

You can place all content to a single table with a discriminator column and then just select top 20 ... from ... order by CreatedAtUtc desc.

Alternatively, if you store different type of content in different tables, you can try something like (not sure about exact syntax):

select top 20 from (
    select top 20 ID, CreatedAtUtc, 'PostedToForum' from ForumPosts order by CreatedAtUtc
    union all
    select top 20 ID, CreatedAtUtc, 'WroteOnWalll' from WallPosts order by CreatedAtUtc) t
order by t.CreatedAtUtc desc
Anton Gogolev
I've thought about this approach, but I'm a little concerned about the scalability. Right now we have about 6-7 different activity types so we would need to draw on data from 6-7 tables, but in the future this might expand to 20 or 30 different activities.
Micael
A: 

Hi Micael,

You might want to check out http://activitystrea.ms/ for inspiration, especially the schema definition. If you look at that spec you'll see that there is also the concept of a "Target" object. I have recently done something very similar but I had to create my own database to encapsulate all of the activity data and feed data into it because I was collecting activity data from multiple applications with disparate data sources in different databases.

Max

MadMax1138