Storing an Array of Strings in a database

*爱你&永不变心* 提交于 2019-12-05 02:48:28

问题


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.) ?


回答1:


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


来源:https://stackoverflow.com/questions/21584612/storing-an-array-of-strings-in-a-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!