I have a table:
+----+---------+----------+
| id | user_id | comment |
+----+---------+----------+
Where column type is:
- id (bigint not null primary key autoincrement)
- user_id (bigint not null)
- comment (text)
How can I partition this table on user_id by range? I tried to partition this table by range in PHPMyAdmin but doesn't allow me because user_id isn't a primary key. If I have many 10 billion users and each has an infinite amount of comments this table will be very large. I want to partition it like:
partition 1 (user_id<500)
+----+---------+----------+
| id | user_id | comment |
+----+---------+----------+
partition 2 (user_id<1000)
+----+---------+----------+
| id | user_id | comment |
+----+---------+----------+
And so on.
Yes, since
user_idis not part of the tableprimary key(s)orunique keysyou can't create partitions solely for theuser_idon your table as the DOCs states very clearlySo for your case what you can do is to add a
unique keyon your table for bothidanduser_idAnd then add the range partition for only
user_idcolumn as such:Note Since you already have a table with values in order to define your partition ranges you need to wrap around all existing values for your
user_idcolumn in your partitions. That is if you have auser_idof1000you can not define your last partition asPARTITION p3 VALUES LESS THAN (1000)that will fail. You will need one more partition i.e.:PARTITION p3 VALUES LESS THAN (2000)orPARTITION p3 VALUES LESS THAN MAXVALUESee it working here: http://sqlfiddle.com/#!9/8ca7ed
Full working example: