How can I use mysql partitioning on this table?
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 hav开发者_C百科ing 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.
CREATE TABLE user_actions (
auto_id INT NOT NULL AUTO_INCREMENT,
type INT NOT NULL,
user_id INT NOT NULL,
insert_datetime DATE NOT NULL,
action_id INT NOT NULL)
PARTITION BY RANGE(TO_DAYS(insert_datetime))
(
PARTITION p0 VALUES LESS THAN (to_days('2011-06-01')),
PARTITION p1 VALUES LESS THAN (to_days('2012-01-01')) ,
PARTITION p11 VALUES LESS THAN MAXVALUE
);
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.
-- Untested, but again should be close
ALTER TABLE t1 REORGANIZE PARTITION (pM) INTO
(PARTITION p20110101 VALUES LESS THAN (to_days('2011-07-01'),
PARTITION pM VALUES LESS THAN MAXVALUE);
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
精彩评论