简体   繁体   English

可扩展的一对多表(MySQL)

[英]Scalable one to many table (MySQL)

I have a MySQL database, and a particular table in that database will need to be self-referencing, in a one-to-many fashion. 我有一个MySQL数据库,并且该数据库中的特定表需要以一对多的方式进行自我引用。 For scalability, I need to find the most efficient solution possible. 为了实现可伸缩性,我需要找到最有效的解决方案。 The two ways most evident to me are: 对我来说最明显的两种方法是:

1) Add a text field to the table, and store a serialized list of primary keys there 1)在表中添加一个文本字段,并在其中存储主键的序列化列表

2) Keep a linker table, with each row being a one-to-one. 2)保留一个链接器表,每一行都是一对一的。

In case #1, I see the table growing very very wide (using a spatial analogy), but in case #2, I see the linker table growing to a very large number of rows, which would slow down lookups (by far the most common operation). 在情况#1中,我看到表增长得非常宽(使用空间类比),但是在情况#2中,我看到链接器表增长到非常多的行,这将减慢查找(到目前为止最多)常见操作)。

What's the most efficient manner in which to implement such a one-to-many relationship in MySQL? 在MySQL中实现这种一对多关系的最有效方式是什么? Or, perhaps, there is a much saner solution keeping the data all directly on the filesystem somehow, or else some other storage engine? 或者,也许有一个更明智的解决方案以某种方式将所有数据直接保留在文件系统上,或者使用其他存储引擎?

Just keep a table for the "many", with a key column for the primary table. 只需为“许多”保留一个表,并为主表保留一个键列。

I quarantee you'll have lots of other more important problems to solve before you run into efficiency or capacity constraints in a standard industrial-strength relational dbms. 在您遇到标准的工业强度关系dbms中的效率或容量限制之前,我将为您解决许多其他更重要的问题。

IMHO the most likely second option (with numerous alternative products) is to use an isam. 恕我直言,最有可能的第二种选择(有许多替代产品)是使用isam。

If you need to do deep/recursive traversals into the data, a graph database like Neo4j (where I'm on the team) is a good choice. 如果您需要对数据进行深度/递归遍历,那么像Neo4j (我在团队中的位置)这样的图形数据库是一个不错的选择。 You'll find some information in the article Should you go Beyond Relational Databases? 您将在文章《 您应该超越关系数据库吗?》中找到一些信息 and in this post at High Scalability . 并在此帖子中具有High Scalability For a use case that may be similar to yours, read this thread on MetaFilter . 对于可能与您相似的用例,请在MetaFilter上阅读此主题 For information on language bindings and other things you may also find the Neo4j wiki and mailing list useful. 有关语言绑定和其他内容的信息,您可能还会发现Neo4j Wiki邮件列表很有用。

My first comment would be that you'll get better responses if you can describe how the data will be used (frequency of adds/updates vs lookups, adds vs updates, etc) in addition to what you've already described. 我的第一句话是,除了已经描述的内容之外,如果您可以描述数据的使用方式(添加/更新与查找的频率,添加与更新的频率等),您将获得更好的响应。 That being said, my first thought would be to just go with a generic representation of 话虽这么说,我的第一个想法是只采用


CREATE  TABLE IF NOT EXISTS one_table (
  `one_id` INT UNSIGNED  NOT NULL AUTO_INCREMENT
           COMMENT 'The The ID of the items in the one table' ,
  ... other data
)

CREATE  TABLE IF NOT EXISTS many_table (
  `many_id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT
            COMMENT 'the id of the items in the many table',
  `one_id` INT UNSIGNED  NOT NULL
           COMMENT 'The ID of the item in the one table that this many item belongs to' ,
  ... other data
)

Making sure, of course, to create an index on the one_id in both tables. 当然,请确保在两个表中的one_id上​​创建索引。

Not so much an answer but a few questions and a possible approach.... 与其说答案,不如说是几个问题和一种可能的方法。

If you want to make the table self referencing and only use one field ... there are some options. 如果要使表自引用,并且仅使用一个字段,则有一些选项。 A calculated maskable 'join' field describes a way to associate many rows with each other. 计算得出的可屏蔽“连接”字段描述了一种将许多行彼此关联的方法。

The best solution will probably consider the nature of the data and relationships? 最佳解决方案可能会考虑数据和关系的性质? What is the nature of the data and lookups? 数据和查找的本质是什么? What sort of relationship are you trying to contain? 您想遏制什么样的关系? Association? 协会? Related? 有关? Parent/Children? 家长/孩子?

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

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