存储“不规则”的数据MySQL 中的数据

发布于 2024-12-06 14:24:58 字数 356 浏览 1 评论 0原文

如果已经问过这个问题,请提前道歉,但我真的不知道如何搜索我遇​​到的问题。

我正在开发一个酒店预订和预订管理系统,旨在与多个客户合作,预计一些客户会有不同的要求,其中包括超出预订响应表中标准的其他字段(姓名、地址、电子邮件、邮政编码等)。

为了实现这一目标,我创建了一个“选项”字段,它将这些附加字段及其附带值存储为序列化数据。然而,客户希望这些字段是可搜索的。虽然这是可能的,但这显然不是存储需要搜索的数据的最佳方式。

另外,该表在 MySQL 中是 InnoDB 格式。

我唯一能想到的就是将这些额外的字段移到一个单独的表中,但这给预订的读写过程带来了很多挑战。

为了搜索的目的存储这种不规则数据的最佳方式是什么?

Apologies in advance if this question has already been asked, but I really don't know how to search for the problem I have.

I'm developing a hotel booking and reservation management system with the intention of working with multiple clients, and it is expected that some clients will have different requirements, which include additional fields beyond the standard found in the booking response form (name, address, email, post code et cetera).

To accomplish this, I've created an "options" field that stores these additional fields and their attendant values as serialized data. However, the client wants those fields to be searchable. While this is possible, this quite clearly isn't the optimal way of storing data that needs to be searched.

Also, the table in question is of InnoDB format in MySQL.

About the only thing I can think of is moving these additional fields into a separate table, but that presents a lot challenges to the reading and writing process of bookings.

What is an optimal way of storing this kind of irregular data for the purposes of searching?

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

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

发布评论

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

评论(3

薄荷港 2024-12-13 14:24:58

一种选择是使用键值表。单独存储密钥,例如:

CREATE TABLE keys (
  id INT(11) UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
  keyName VARCHAR(100)
);

CREATE TABLE values (
  key_id INT(11) UNSIGNED NOT NULL,
  customer_id INT(11) UNSIGNED NOT NULL, 
  value VARCHAR(100)
);

绝对仍然不是很好,但它是解决您问题的有效解决方案。

One option is to use a key-value table. Store the keys separately, e.g.:

CREATE TABLE keys (
  id INT(11) UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
  keyName VARCHAR(100)
);

CREATE TABLE values (
  key_id INT(11) UNSIGNED NOT NULL,
  customer_id INT(11) UNSIGNED NOT NULL, 
  value VARCHAR(100)
);

Definitely still not great, but it's a valid solution to your problem.

感受沵的脚步 2024-12-13 14:24:58

您的问题实际上并不是数据库问题,而是如何在单个程序中支持多个客户端及其需求的问题。

您需要决定的第一件事是程序的单个实例中是否支持多个客户端,或者每个客户端是否都有自己的实例。

如果每个客户端都有自己的实例,那么您可以单独维护公共代码库并根据需要自定义每个客户端的实例。可能需要进行一些规划才能创建一个系统,其中对通用代码库所做的更改可以由自定义版本正确继承,但最终每个客户都会得到他们想要的东西。

如果您采用多租户方法,那么您需要提前决定每个客户能够自定义其系统的确切程度。然后,您可以在数据库和应用程序结构中提供该自定义。在最简单的层面上,这允许每个客户端将其识别信息和徽标存储在表中的某个位置(可能是他们自己的 CSS 链接,以真正为应用程序提供定制的外观)。

对于“额外字段”,可以通过多种方式处理。一种方法是简单地在相关表上放置 10 个额外的 VARCHAR 字段,并允许每个客户端根据自己认为合适的方式“命名”这些字段(可能使用 TYPE 说明符,您的应用程序将在需要时使用该说明符强制使用 VARCHAR 数据)。然而,他们命名的许多列都会在用户界面中适当的位置显示(带有正确的提示)。这种方法的优点是,一旦设置完毕,您就不需要为每个客户端做任何额外的工作。

另一种方法是允许每个客户端有一个额外的一对一相关表来存储额外的字段。在这种情况下,您可以正确命名并键入数据库中的字段。缺点是,如果您允许客户自由选择这些字段,您就必须修改用户界面才能“了解”每个单独的客户。

Your problem isn't really a database problem but rather an issue of how to support multiple clients and their requirements in a single program.

The first thing you need to decide is whether multiple clients will be supported within a single instance of your program, or whether each client will have their own instance.

If each client has their own instance, then you can maintain the common codebase separately and customize each client's instance as required. It may take some planning to produce a system in which changes made to the common codebase are correctly inherited by the customized versions, but in the end each client will get exactly what they want.

If you're taking the multi-tenanted approach then you need to decide in advance the exact degree to which each client will be able to customize their system. You then provide for that customization in your database and application structure. At the simplest level, this allows each client to store their identifying information and a logo in a table someplace (possibly their own CSS link to really give the application a customized look).

In the case of "extra fields", this can be handled in several ways. One is to simply put 10 extra VARCHAR fields on the table in question, and allow each client to "name" these fields as they see fit (possibly with TYPE specifiers that your application will use to coerce the VARCHAR data if needed). However many of the columns they've named are shown (with the correct prompt) where appropriate in the user interface. This approach has the benefit that, once set up, you won't have any additional work to do per client.

Another approach would be to allow each client to have an additional 1 to 1 related table that stores the extra fields. In this case you can correctly name and type the fields in the database. The disadvantage is that to the extent you allow the client to freely choose what these fields are, you'll have to modify your user interface to "know" about each individual client.

自在安然 2024-12-13 14:24:58

如果您在阅读该表时并不总是需要它,我可能会将其分离到自己的表中,即您是否曾经查询过该表而不返回该字段?

如果您确实选择将其保留在同一个表中,则没有理由不能在其上放置索引,但这将尽可能加快搜索速度,而无需将数据拆分为逻辑块(如果它可以是“任何东西”)。请注意,添加索引会增加写入时间,同时减少读取时间(因此,不要在写入频率高于读取频率或写入对时间更敏感的系统中执行此操作)。

I would probably separate it out in to a table of its own if it's something that you won't always be needing when reading that table, i.e., do you ever query this table without returning this field?

If you do choose to keep it in the same table there's no reason you can't place an index on it however which would speed up the searches as much as is possible without splitting up the data into logical chunks (which you can't if it can be "anything"). Do be warned that adding indexes will increase write times while decreasing read times (so don't do it in a system that will write more frequently than read or where the writes are more time sensitive).

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