如何在该表上使用 mysql 分区?
我正在开发一个社交网络类型的项目,就像大多数社交网络一样,用户提要将显示您的朋友在该网站上所做的事情。
假设我有一个包含这些字段的 MySQL 表;
// 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
现在,在我的 PHP 脚本中,我将查询此表以获取用户的所有好友操作。
我认为这是使用 MySQL 分区的完美表类型,因此不要显示您朋友的所有操作并让它查询网站上发布的每个操作,这可能是基于以前网站的数百万条记录,我完成后,我认为最好对再见日期进行分区,也许将所有操作划分为 6 个月的分区,这样查询的记录就更少了。
我从未使用过分区,但多年来一直在寻找与此类似的解决方案,我刚刚发现了内置的 MySQL 分区,它们看起来就像这里的门票。
有人可以告诉我如何将这样的表创建到分区中,而且由于我需要每 6 个月创建一个新分区,有没有办法自动创建新分区?请帮忙
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这未经测试,但应该很接近。
您可以通过以下方式进行管理:
您可以让 MAXVALUE 分区始终代表您的“活动”(在您的情况下为当前 6 个月期间)分区。当时间段结束时,您可以拆分/重组 MAXVALUE 分区,其中过去的时间段将进入新分区,其中 MAXVALUE 分区再次代表当前/活动分区。
例如,2011 年 1 月 1 日,您将有一个分区,我们将其称为 pM,它将存储所有内容,因为它具有 LESS THAN MAXVALUE 子句。然后,6 个月过去后,您将重组/拆分该单个分区,创建一个新分区,用于保存前 6 个月的所有数据,并且 MAXVALUE 分区再次代表当前/活动期间。
您也可以考虑子分区。您可以通过 HASH 对 user_id 进行子分区,从而进一步减少基于 user_id 查询数据的 I/O 和成本。
查看以下链接以获取有关分区的更多信息。
MySQL 分区
分区管理
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