mysql - 如何构建数据库模式以允许 "1 in a million"情况?

标签 mysql database-design doctrine-orm

在我的数据库中的所有表中,我有两个当前具有多对多连接。然而,捕获的实际数据群体几乎总是具有一对多关联。

考虑到我希望数据库查找(学说查询)尽可能不受阻碍,我应该:

  • 在表之间创建两个关联(其中第二个仅是 在这些特殊情况下填充)?
  • 更改关联的数据类型(例如,更改为text/tinyblob)以记录 2 个(或技术上甚至 3 个)关联记录的迷你数组?<

这就是我目前所拥有的(尽管 TableB-> JoinTable 通常只是一对一):

TableA.id --< a_id.JoinTable.b_id >-- TableB.id

所以,我想看看是否可以捕获“异常”。以下是正确的方法吗?

TableA.id     TableB.id
       +----< TableB.A_id1
       +----- TableB.A_id2
       +----- TableB.A_id3

最佳答案

您似乎对以下内容感兴趣:

-- a and b are related by the association of interest
Foo(a, b)

-- foo(a, b) but not foo(a2, b) for some a2 <> a
Boring(a, b)
unique(b)
FK (a, b) references Foo

-- foo(a, b) and foo(a2, b) for some a2 <> a
Rare(a, b)
FK (a, b) references foo

如果您希望查询不受阻碍,只需定义 Foo 即可。您可以查询稀有。

Rare = select * from Foo f join Foo f2
    where f.a <> f2.a and f.b = f2.b

任何其他设计都会受到保持数据库一致性的更新复杂性的影响。

您对 Rare 比 Foo 小得多有一些模糊的担忧。但是您的要求是什么?在一百万个 Foo 记录中只有 n 个:许多:您会选择其他设计吗?

下一个复杂程度是拥有 Foo 和 Rare。更新必须保持上述等式成立。

仅通过 Boring + Rare 并从中重建 Foo 来减少 Foo + Rare 的百万分之二或三的冗余似乎极不可能有好处。但为 Boring 定义一个唯一索引 (b) 可能会有好处,这将保持其中的 b 只有一个 a。当你需要 Foo 时:

Foo = select * from Boring union select * from Rare

但是您的更新必须保持这一点

not exists (select * from Boring b join Rare r where b.b = r.b)

关于mysql - 如何构建数据库模式以允许 "1 in a million"情况?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40361323/

相关文章:

MySQL 其中连接不存在或 = 'default' 或 = ''

sql - 为什么这个MySQL触发器会导致堆栈溢出?

mysql - 想要记录已完成的付款、固定付款和原始付款

database - char 在数据库中占用 1 个字节吗?

php - 存储其中包含 unicode 字符的字符串

PHP 错误 : "supplied argument is not a valid MySQL result resource in"

php - Laravel 导入程序随着时间的推移会变慢

sql - 数据库设计——它是否尊重 3rd NF?

php - 如何设置外键 ID #sf2 #doctrine2

symfony - 将 Sylius 包集成到 Laravel 项目中