在这种情况下,非规范化可以接受吗?

发布于 2024-09-17 06:43:44 字数 973 浏览 3 评论 0原文

我有以下 locations 表:

----------------------------------------------------------
| ID | zoneID | storeID | address | latitude | longitude |
----------------------------------------------------------

phones 表:

-----------------------
| locationID | number |
-----------------------

现在,请记住,对于任何捐赠商店,最多可以包含五个电话号码。顺序并不重要。

最近,我们需要添加另一个表,其中包含商店相关信息,其中还包括电话号码。

现在,这个新表不应用 locationID,因此我们无法将电话存储在以前的电话表中。

保持数据库规范化最终需要 2 个新表和总共 4 个联接来检索数据。对其进行非规范化会呈现旧表,如下所示:

----------------------------------------------------------------------------------
| ID | zoneID | storeID | address | latitude | longitude | phone1 | ... | phone5 |
----------------------------------------------------------------------------------

总共有 2 个表和 2 个连接。

我不喜欢使用 data1data2data3 字段,因为这可能会带来巨大的痛苦。那么,你有什么看法呢。

I have the following locations table:

----------------------------------------------------------
| ID | zoneID | storeID | address | latitude | longitude |
----------------------------------------------------------

and the phones table:

-----------------------
| locationID | number |
-----------------------

Now, keep in mind that for any giving store it can be up to five phone numbers, top. Order doesn't matter.

Recently we needed to add another table which would contain stores related info which would also include phone numbers.

Now, to this new table doesn't apply locationID so we can't store the phones in the previous phone table.

Keeping the DB normalized would require, in the end, 2 new tables and a total of 4 joins to retrieve the data. Denormalizing it would render the old table like:

----------------------------------------------------------------------------------
| ID | zoneID | storeID | address | latitude | longitude | phone1 | ... | phone5 |
----------------------------------------------------------------------------------

and having a total of 2 tables and 2 joins.

I'm not a fan of having data1, data2, data3 fields as it can be a huge pain. So, what's your opinion.

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

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

发布评论

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

评论(3

抱猫软卧 2024-09-24 06:43:44

我认为,就其价值而言,反规范化是当且仅当您确实存在性能问题时才可以采取的措施来获得性能。我总是针对 3NF 进行设计,并且仅在绝对必要时才恢复。

您所做的并不是为了让您的查询看起来更好。任何体面的数据库开发人员都不会担心中等复杂的 SQL 语句,尽管我必须承认我见过一些让我不寒而栗的数百行语句 - 请注意,这些语句来自无法控制模式的客户: DBA 首先会重新设计架构以避免出现这样的问题。

但是,只要您对非规范化所施加的限制感到满意,您就可以做任何您想做的事情。这并不是说有一群 3NF 警察在地球上漫游寻找违规者:-​​)

我能看到的直接限制(可能还有其他限制)是:

  • 你将被限制(最初,没有架构更改)最多 5 部手机每个位置的数量。从您的描述来看,您似乎不认为这是一个问题。
  • 您将浪费空间来存储不必要的数据。换句话说,每行都使用五个数字的空间,无论它们实际有什么,尽管这种影响可能很小(例如,如果它们是 varchar 且可为空)。
  • 查找电话号码的查询将会很复杂,因为您必须检查五个不同的列。我不知道这是否是您的用例之一,所以它可能无关紧要。

不过,您可能应该选择一种方式(我不确定这是否是您的意图)。如果我遇到一个在存储表和单独的电话号码表中都有电话号码的架构,我会特别恼火,尤其如果它们彼此不一致。即使当我去规范化时,我也倾向于使用插入/更新触发器来确保保持数据一致性。

My opinion, for what it's worth, is that de-normalisation is something you do to gain performance if, and only if, you actually have a performance problem. I always design for 3NF and only revert if absolutely necessary.

It's not something you do to make your queries look nicer. Any decent database developer would not fear a moderately complex SQL statement although I do have to admit I've seen some multi-hundred-line statements that gave me the shivers - mind you, these were from customers who had no control over the schema: a DBA would have first re-engineered the schema to avoid such a monstrosity.

But, as long as you're happy with the limitations imposed by de-normalisation, you can do whatever you want. It's not as if there's a band of 3NF police roaming the planet looking for violators :-)

The immediate limitations (there may be others) that I can see are:

  • You'll be limited (initially, without a schema change) to five phone numbers per location. From your description, it doesn't appear you see this as a problem.
  • You'll waste space storing data that doesn't have to be there. In other words, every row uses space for five numbers regardless of what they actually have, although this impact is probably minimal (e.g., if they're varchar and nullable).
  • Your queries to look up a phone number will be complicated since you'll have to check five different columns. Whether that's one of your use cases, I don't know, so it may be irrelevant.

You should probably choose one way or the other though (I'm not sure if that's your intent here). I'd be particularly annoyed if I came across a schema that had phone numbers in both the store table and a separate phone numbers table, especially if they disagreed with each other. Even when I de-normalise, I tend to use insert/update triggers to ensure data consistency is maintained.

红玫瑰 2024-09-24 06:43:44

我认为你的问题源于错误的模型。

为什么有位置 ID 和商店 ID?一家商店可以占据多个地点吗?
电话号码是否与地理位置相关?

只需通过 StoreId 输入所有内容,您的问题就会消失。

I think your problem stems from an erroneous model.

Why do you have a location id and a store id? Can a store occupy more than one location?
Is the phone number tied to a geographic location?

Just key everything by StoreId and your problems will disappear.

飞烟轻若梦 2024-09-24 06:43:44

只需尝试将新表与旧位置表关联起来,因为这两个表都代表商店,您应该能够找到某种方式将两者关联起来。如果你能做到这一点,你的问题就解决了,因为你可以像以前一样继续使用电话桌。

将新表与旧位置表相关联将帮助您获取电话号码之外的信息

just try to relate your new table with old location table, as both the tables represent the store you should be able to find someway to relate both. if you can do that your problem is solved, because than you can keep using phone table as before.

Related the new table with old location table will help you beyond getting phone numbers

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