简体   繁体   English

在 MySQL 字段中存储 csv – 坏主意?

[英]Storing csv in MySQL field – bad idea?

I have two tables, one user table and an items table.我有两个表,一个用户表和一个项目表。 In the user table, there is the field "items".在用户表中,有字段“项目”。 The "items" table only consists of a unique id and an item_name. “items”表只包含一个唯一的 id 和一个 item_name。

Now each user can have multiple items.现在每个用户可以拥有多个项目。 I wanted to avoid creating a third table that would connect the items with the user but rather have a field in the user_table that stores the item ids connected to the user in a "csv" field.我想避免创建将项目与用户连接起来的第三个表,而是在 user_table 中有一个字段,用于在“csv”字段中存储连接到用户的项目 ID。

So any given user would have a field "items" that could have a value like "32,3,98,56".因此,任何给定的用户都会有一个字段“items”,其值可能类似于“32,3,98,56”。

It maybe is worth mentioning that the maximum number of items per user is rather limited (<5).值得一提的是,每个用户的最大项目数是相当有限的(<5)。

The question: Is this approach generally a bad idea compared to having a third table that contains user->item pairs?问题:与包含用户-> 项目对的第三个表相比,这种方法通常是一个坏主意吗?

Wouldn't a third table create quite an overhead when you want to find all items of a user (I would have to iterate through all elements returned by MySQL individually).当您想要查找用户的所有项目时,第三个表不会产生相当大的开销(我必须单独遍历 MySQL 返回的所有元素)。

You don't want to store the value in the comma separated form.您不想以逗号分隔的形式存储值。

Consider the case when you decide to join this column with some other table.当您决定将此列与某个其他表连接时,请考虑这种情况。

Consider you have,考虑到你有,

x  items
1  1, 2, 3
1  1, 4
2  1

and you want to find distinct values for each x ie:并且您想为每个 x 找到不同的值,即:

x  items
1  1, 2, 3, 4
2  1

or may be want to check if it has 3 in it或者可能想检查它是否有 3 个

or may be want to convert them into separate rows:或者可能想要将它们转换为单独的行:

x  items
1  1
1  2
1  3
1  1
1  4
2  1

It will be a HUGE PAIN.这将是一个巨大的痛苦。

Use atleast normalization 1st principle - have separate row for each value.使用至少标准化第一原则 - 每个值都有单独的行。

Now, say originally you had this as you table:现在,说最初你在桌子上有这个:

x  item
1  1
1  2
1  3
1  1
1  4
2  1

You can easily convert it into csv values:您可以轻松地将其转换为 csv 值:

select x, group_concat(item order by item) items
from t
group by x

If you want to search if x = 1 has item 3. Easy.如果要搜索 x = 1 是否有第 3 项。简单。

select * from t where x = 1 and item = 3

which in earlier case would use horrible find_in_set :在较早的情况下会使用可怕的find_in_set

select * from t where x = 1 and find_in_set(3, items);

If you think you can use like with CSV values to search, then first like %x% can't use indexes.如果您认为可以使用 like 和 CSV 值进行搜索,那么首先like %x%不能使用索引。 Second, it will produce wrong results.其次,它会产生错误的结果。
Say you want check if item ab is present and you do %ab% it will return rows with abc abcd abcde .... .假设您想检查项目 ab 是否存在并且您执行%ab%它将返回带有 abc abcd abcde .... 的行。

If you have many users and items, then I'd suggest create separate table users with an PK userid, another items with PK itemid and lastly a mapping table user_item having userid, itemid columns.如果您有很多用户和项目,那么我建议使用 PK userid 创建单独的表users ,使用 PK itemid 创建另一个items ,最后创建一个具有 userid、itemid 列的映射表user_item

If you know you'll just need to store and retrieve these values and not do any operation on it such as join, search, distinct, conversion to separate rows etc. etc. - may be just may be, you can (I still wouldn't).如果您知道您只需要存储和检索这些值而不对其进行任何操作,例如连接、搜索、不同、转换为单独的行等 - 可能只是可能,您可以(我仍然会't)。

Storing complex data directly in a relational database is a nonstandard use of a relational database.将复杂数据直接存储在关系数据库中是关系数据库的非标准用法。 Normally they are designed for normalized data.通常它们是为规范化数据设计的。

There are extensions which vary according to the brand of software which may help.根据软件品牌的不同,有一些扩展可能会有所帮助。 Or you can normalize your CSV file into properly designed table(s).或者您可以将您的 CSV 文件规范化为正确设计的表格。 It depends on lots of things.这取决于很多事情。 Talk to your enterprise data architect in this case.在这种情况下,请咨询您的企业数据架构师。

Whether it's a bad idea depends on your business needs.这是否是一个坏主意取决于您的业务需求。 I can't assess your business needs from way out here on the internet.我无法从互联网上评估您的业务需求。 Talk to your product manager in this case.在这种情况下,请与您的产品经理交谈。

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

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