在数据库中存储字符串数组(Storing an Array of Strings in a database)

编程入门 行业动态 更新时间:2024-10-28 04:25:49
在数据库中存储字符串数组(Storing an Array of Strings in a database)

我有一个通过ORM保存到数据库的对象。 该对象有一个字符串数组,数组长度可以根据对象而变化。我想知道在数据库中存储String数组的标准做法(例如,我应该将所有字符串存储在一个字段中作为csv等。)?

I have an object that I save to a database through ORM. The object has an array of Strings and the array length can vary per object.I want to know the standard practice for storing the array of String in the db (for example, should I store all the strings in one field as a csv, etc.) ?

最满意答案

我猜你有一个MySql,关系数据库。 作为第一种方法 ,您必须考虑在关系数据库 的字段插入任何类型的组合数据 (CSV,JSON, serialize() ), 这是您应始终应避免的 。 这是我在大学学习数据库时学到的第一件事。 这是因为当您设计数据库时,您的第一个方法应该是数据库规范化 。

非规范化是在寻找性能时常用的东西。 为此,您需要在数据库(建模,访问等)方面拥有丰富的经验。 这是经验丰富的DBA和商业智能专业人士所做的事情,但如果你真的不知道自己在做什么,那么你不必尝试。

所以,你的目标是设计一个规范化的数据库。 为什么这是针对数据库规范化的? 好吧,我们知道有几种“正常形式”可以确定表格对逻辑不一致和异常的免疫程度。 如果你看一下第一范式的定义

第一范式(1NF)是关系数据库中关系的属性。 如果每个属性的域仅包含原子值,并且每个属性的值仅包含该域中的单个值,则关系为第一范式。

因此,当您在字段中保存数组时,您的数据库甚至不是第一个普通形式。

不这样做的一些实际原因是:

你不能使用JOIN 您不能使用索引 搜索,过滤,排序并不容易 失去了参考的能力 如果您真的不知道自己在做什么,那么应用程序层的性能会更差。

确实有些人(像Joomla那样)存储了实体中不太重要的数据,例如字段中的非关键配置值。 对此最好的方法可能是使用serialize() 。 在这里,您可以解释何时可以考虑这样做。 但这又是你应该做的事情,如果你真的知道你在做什么,你真的需要它


如果您想要更多参考,可以阅读:

http://publib.boulder.ibm.com/infocenter/dzichelp/v2r2/index.jsp?topic=%2Fcom.ibm.db2z10.doc.intro%2Fsrc%2Ftpc%2Fdb2z_denormalizationforperformance.htm https://dba.stackexchange.com/questions/4622/when-should-you-denormalize http://searchdatamanagement.techtarget.com/definition/denormalization

而这个SO答案:

https://stackoverflow.com/a/4310112/2357411 https://stackoverflow.com/a/16132444/2357411 https://stackoverflow.com/a/5341286/2357411 https://stackoverflow.com/a/10399902/2357411 https://stackoverflow.com/a/17371729/2357411

I guess you have a MySql, relational database. As a first approach, you have to think that inserting any kind of composed data (CSV, JSON, serialize()) in a field in a relational database, is something that you always should avoid. That was one of the first thing that i learn when studing databases at the university. This is because when you desing a database, your first approach should be Database normalization.

Denormalization is something that is commonly used when looking for perfomance. For doing this, you need to have a great experience in database (modeling, accesing, etc). This is something that experienced DBA and Business Intelligence professionals do, but its not anything you have to try if you dont really know what you are doing.

So, your goal is to desing a normalized database. Why this is against database normalization? well, we know that there are several "normal forms", that determine a table's degree of immunity against logical inconsistencies and anomalies. If you take a look at the definition of the First normal form

First normal form (1NF) is a property of a relation in a relational database. A relation is in first normal form if the domain of each attribute contains only atomic values, and the value of each attribute contains only a single value from that domain.

So, when you save an array in a field, your database is not even in the first normal form.

Some of the practical reasons for not doing this are:

You cannot use JOIN You cannot use indexes Searching, filtering, sorting, is not easy The ability of making references is lost If you dont really know what you are doing, the performance in the application layer is worse.

It is true that some people (like Joomla does) store less important data of the entity, such as non-critical configuration values in a field. The best approach for this probably would be using serialize(). Here you have an explanation on when you can consider to do this. But again this is something you should only do if you really know what you are doing, and you really need it


If you want more references, you can read this:

http://publib.boulder.ibm.com/infocenter/dzichelp/v2r2/index.jsp?topic=%2Fcom.ibm.db2z10.doc.intro%2Fsrc%2Ftpc%2Fdb2z_denormalizationforperformance.htm https://dba.stackexchange.com/questions/4622/when-should-you-denormalize http://searchdatamanagement.techtarget.com/definition/denormalization

And also this SO answers:

https://stackoverflow.com/a/4310112/2357411 https://stackoverflow.com/a/16132444/2357411 https://stackoverflow.com/a/5341286/2357411 https://stackoverflow.com/a/10399902/2357411 https://stackoverflow.com/a/17371729/2357411

更多推荐

本文发布于:2023-08-03 03:08:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1383196.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   字符串   数据库中   Storing   Strings

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!