简体   繁体   English

分区具有外键的mySQL表?

[英]Partitioning mySQL tables that has foreign keys?

What would be an appropriate way to do this, since mySQL obviously doesnt enjoy this. 什么是合适的方法,因为mySQL显然不喜欢这个。 To leave either partitioning or the foreign keys out from the database design would not seem like a good idea to me. 从数据库设计中留下分区或外键对我来说似乎不是一个好主意。 I'll guess that there is a workaround for this? 我猜这有一个解决方法吗?

Update 03/24: 更新03/24:

http://opendba.blogspot.com/2008/10/mysql-partitioned-tables-with-trigger.html http://opendba.blogspot.com/2008/10/mysql-partitioned-tables-with-trigger.html

How to handle foreign key while partitioning 如何在分区时处理外键

Thanks! 谢谢!

I would strongly suggest sharding using Date as the key for archiving data to archive tables. 我强烈建议使用Date作为归档数据归档表的关键字进行分片。 If you need to report off multiple archive tables, you can use Views, or build the logic into your application. 如果需要报告多个归档表,可以使用视图,或在应用程序中构建逻辑。

However, with a properly structured DB, you should be able to handle tens of millions of rows in a table before partitioning, or sharding is really needed. 但是,使用结构合理的数据库,您应该能够在分区之前处理表中的数千万行,或者确实需要分片。

It depends on the extent to which the size of rows in the partitioned table is the reason for partitions being necessary. 它取决于分区表中行的大小是否需要分区的原因。

If the row size is small and the reason for partitioning is the sheer number of rows, then I'm not sure what you should do. 如果行大小很小并且分区的原因是行 ,那么我不确定你应该做什么。

If the row size is quite big, then have you considered the following: 如果行大小很大,那么您是否考虑过以下内容:

Let P be the partitioned table and F be the table referenced in the would-be foreign key. P为分区表, F为be-be外键中引用的表。 Create a new table X : 创建一个新表X

CREATE TABLE `X` (
    `P_id` INT UNSIGNED NOT NULL,
        -- I'm assuming an INT is adequate, but perhaps
        -- you will actually require a BIGINT
    `F_id` INT UNSIGNED NOT NULL,
    PRIMARY KEY (`P_id`, `F_id`),
    CONSTRAINT `Constr_X_P_fk`
        FOREIGN KEY `P_fk` (`P_id`) REFERENCES `P`.`id`
        ON DELETE CASCADE ON UPDATE RESTRICT,
    CONSTRAINT `Constr_X_F_fk`
        FOREIGN KEY `F_fk` (`F_id`) REFERENCES `F`.`id`
        ON DELETE RESTRICT ON UPDATE RESTRICT
) ENGINE=INNODB CHARACTER SET ascii COLLATE ascii_general_ci

and crucially, create a stored procedure for adding rows to table P . 并且至关重要的是,创建一个用于向表P添加行的存储过程。 Your stored procedure should make certain (use transactions) that whenever a row is added to table P , a corresponding row is added to table X . 您的存储过程应该确定(使用事务),每当向表P添加行时,相应的行将添加到表X You must not allow rows to be added to P in the "normal" way! 您不能允许以“正常”方式将行添加到P中! You can only guarantee that referential integrity will be maintained if you keep to using your stored procedure for adding rows. 如果继续使用存储过程添加行,则只能保证将保留参照完整性。 You can freely delete from P in the normal way, though. 但是,您可以以正常方式从P中自由删除。

The idea here is that your table X has sufficiently small rows that you should hopefully not need to partition it, even though it has many many rows. 这里的想法是你的表X有足够小的行,你应该不需要对它进行分区,即使它有很多行。 The index on the table will nevertheless take up quite a large chunk of memory, I guess. 我猜,桌子上的索引将占据相当大的一块内存。

Should you need to query P on the foreign key, you will of course query X instead, as that is where the foreign key actually is. 如果您需要在外键上查询P ,您当然会查询X ,因为这是外键的实际位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM