I am working on a social network type project, as most social networks have, a user feed that will show things that YOUR friends have done on the site.
So let’s say I have a MySQL table for these items with these fields;
// user_actions
auto_id = auto increment ID
type = a number (1 = photo upload, 2 = friend added, 3 = status post, 4 = so other action, etc..)
user_id = The id of the user who did the action
datetime = date and time
action_id = this could be the ID of the action, so if it is for a status post, it could be the ID of the actual status post record
Now in my PHP script, I would query this table to get all friend actions of a user.
I think this is the perfect type of table to use the MySQL partitioning, so instead of showing all actions from your friends and having it query every action ever posted on the site, which could be in the millions of records based off a previous site I had done, I think it would be good to partition bye date, maybe have all actions partioned into 6 month partitions, so it is less records to query.
I have never worked with the partitions but have been looking for a sollution similar to this for a few years, I just discovered the built in MySQL partitions and they seem like the ticket here.
Can someone show me how I could go about creating a table like that into partitions, also since I would need a new partition created every 6 months, is there a way to automate new partitions? Please help
This is untested, but should be close.
You can manage this in the following way:
You can have the MAXVALUE partition always represent your “active” (in your case current 6 month period) partition. When the period is up, you can split/reorg that MAXVALUE partition where the period that past goes into a new partition with the MAXVALUE partition representing again the current/active partition.
For example, Jan 1st of 2011 you would have one partition, let’s call it pM and it would store everything as it would have the LESS THAN MAXVALUE clause. Then after 6 months have passed, you would reorg/split that single partition creating a new partition that holds all the data for the previous 6 months and the MAXVALUE partition again representing the current/active period.
You may also consider sub-partitioning. You could sub-partition your user_id by HASH and therefore further reduce I/O and cost on queries for data based on the user_id.
Check out the following links for more information on partitioning.
MySQL Partitioning
Partition Managment