在mysql中,我可以有一个由自动增量和另一个字段组成的复合主键吗?另外,请批评我的“mysql分区”逻辑

发布于 2024-11-10 10:58:31 字数 1655 浏览 3 评论 0原文

我正在尝试 mysql 分区(将表拆分以帮助其更好地扩展),并且我在表上的键上遇到了问题。首先,我正在使用 python 的线程注释模块...这是架构

+-----------------+------------------+------+-----+---------+-------+
| Field           | Type             | Null | Key | Default | Extra |
+-----------------+------------------+------+-----+---------+-------+
| content_type_id | int(11)          | NO   | MUL | NULL    |       |
| object_id       | int(10) unsigned | NO   |     | NULL    |       |
| parent_id       | int(11)          | YES  | MUL | NULL    |       |
| user_id         | int(11)          | NO   | MUL | NULL    |       |
| date_submitted  | datetime         | NO   |     | NULL    |       |
| date_modified   | datetime         | NO   |     | NULL    |       |
| date_approved   | datetime         | YES  |     | NULL    |       |
| comment         | longtext         | NO   |     | NULL    |       |
| markup          | int(11)          | YES  |     | NULL    |       |
| is_public       | tinyint(1)       | NO   |     | NULL    |       |
| is_approved     | tinyint(1)       | NO   |     | NULL    |       |
| ip_address      | char(15)         | YES  |     | NULL    |       |
| id              | int(11)          | YES  |     | NULL    |       |
+-----------------+------------------+------+-----+---------+-------+

注意,我通过删除 id col(默认情况下为主要)并重新添加它来修改此数据库。

本质上,我想将 id 和 content_type_id 作为我的主键。我还希望 id 自动递增。这可能吗。

第二个问题。由于我刚刚学习mysql分区,我想知道我的分区逻辑是否合理。有 67 种不同的 content_type,其中一些(也许是全部)内容类型允许对其进行评论。我的计划是根据正在评论的对象类型进行分区。例如,图像会被大量评论,所以我将与图像相关的任何内容类型放入一个分区,而另一种可以评论的内容类型是“博客条目”,因此有一个单独的分区,并且等等等等。随着负载的增长,这将使我能够将这些分区分散到专用机器上。到目前为止我对这个概念的理解如何?

非常感谢!

I am experimenting with mysql partitioning ( splitting the table up to help it scale better ), and I am having a problem with the keys on the table. First, I am using a python's threaded comments module... here is the schema

+-----------------+------------------+------+-----+---------+-------+
| Field           | Type             | Null | Key | Default | Extra |
+-----------------+------------------+------+-----+---------+-------+
| content_type_id | int(11)          | NO   | MUL | NULL    |       |
| object_id       | int(10) unsigned | NO   |     | NULL    |       |
| parent_id       | int(11)          | YES  | MUL | NULL    |       |
| user_id         | int(11)          | NO   | MUL | NULL    |       |
| date_submitted  | datetime         | NO   |     | NULL    |       |
| date_modified   | datetime         | NO   |     | NULL    |       |
| date_approved   | datetime         | YES  |     | NULL    |       |
| comment         | longtext         | NO   |     | NULL    |       |
| markup          | int(11)          | YES  |     | NULL    |       |
| is_public       | tinyint(1)       | NO   |     | NULL    |       |
| is_approved     | tinyint(1)       | NO   |     | NULL    |       |
| ip_address      | char(15)         | YES  |     | NULL    |       |
| id              | int(11)          | YES  |     | NULL    |       |
+-----------------+------------------+------+-----+---------+-------+

Note, I have modified this database by dropping the id col (primary by default), and re adding it.

Essentially, I want to have id AND content_type_id as my primary keys. I also want id to auto increment. Is this possible.

Second question. Since I am just learning about mysql partitioning, I am wondering if my partitioning logic is sound. There are 67 different content_types, and some (maybe all) of those content types allow comments to be made on them. My plan is to partition based on the type of object that is being commented on. For instance, the images will be commented on a lot, so I put any content type pertaining to images into one partition, and another content type that can be commented on is "blog entries", so there is a separate partition for that, and so on and so on. This will allow me to spread these partitions possibly to dedicated machines as the load grows. How is my understanding of this concept so far?

Thanks so much!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

゛时过境迁 2024-11-17 10:58:32

由于 id 会自动递增,因此它本身可以成为主键。将 content_type 添加到主键不会为您带来任何关于键的唯一性的信息。

如果要向 2 列添加索引以提高性能,请向包含 2 列的表添加备用唯一索引,而不是尝试将它们都添加到主键。但是,请注意,在 2 列上强制唯一性将是一种浪费,因为 id 本身已经保证是唯一的,因此如果需要,常规索引会更有意义。

Since id will be auto incremented, it can be the primary key all by itself. Adding content_type to the primary key would not gain you anything in regards to the uniqueness of the key.

If you want to add an index for faster performance to the 2 columns, then add an alternate unique index to the table with the 2 columns instead of trying to add them both to the primary key. However, be aware that enforing uniqueness on the 2 columns would be a waste since id is already guaranteed to be unique by itself, so a regular index would make more sense if needed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文