I am planning on using the following formula to calculate "trending" posts:
Trending Score = (p - 1) / (t + 2)^1.5
p = votes (points) from users. t = time since submission in hours.
I am looking for advice on how to structure my database tables so that I can query for trending posts with DynamoDB (a nosql database service from Amazon).
DynamoDB requires a Primary Key for each item in a table. The Primary Key can consist of 2 parts: the Hash Attribute (string or number) and the Range Attribute (string or number). The Hash Attribute must be unique for each item and is required. The Range Attribute is optional, but if used DynamoDB will build a sorted range index on the Range Attribute.
The structure I had in mind goes as follows:
TableName: Users
HashAttribute: user_id
RangeAttribute: NONE
OtherFields: first_name, last_name
TableName: Posts
HashAttribute: post_id
RangeAttribute: NONE
OtherFields: user_id,title, content, points, categories[ ]
TableName: Categories
HashAttribute: category_name
RangeAttribute: post_id
OtherFields: title, content, points
TableName: Counters
HashAttribute: counter_name
RangeAttribute: NONE
OtherFields: counter_value
So here is an example of the types of requests I would make with the following table setup (example: user_id=100):
User Action 1:
User creates a new post and tags the post for 2 categories (baseball,soccer)
Query (1):
Check current value for the counter_name='post_id' and increment+1 and use the new post_id
Query (2): Insert the following into the Posts table:
post_id=value_from_query_1, user_id=100, title=user_generated, content=user_generated, points=0, categories=['baseball','soccer']
Query (3):
Insert the following into the Categories table:
category_name='baseball', post_id=value_from_query_1, title=user_generated, content=user_generated, points=0
Query (4):
Insert the following into the Categories table:
category_name='soccer', post_id=value_from_query_1, title=user_generated, content=user_generated, points=0
The end goal is to be able to conduct the following types of queries:
1. Query for trending posts
2. Query for posts in a certain category
3. Query for posts with the highest point values
Does anyone have any idea how I could structure my tables so that I could do a query for trending posts? Or is this something I give the up the ability to do by switching to DynamoDB?