什么时候将数据存储在指定表中或者序列化?如何拨打电话?
我正在使用 postgresql 安装来存储我的数据。
我的 Post 模型有一个名为“选择”的属性,该属性当前以以下形式将数据存储在 TEXT 列中:“x1,x2,x3,x4,x5...”
当我需要访问此数据时,我将其拆分为逗号并用它做我的事情。
我正在制作一个应用程序原型,所以我在编写它时很快就做了最简单的事情,但现在我可以看到另一种选择是为“选择”创建一个表并将其关联回帖子,然后为每一位。
我的问题是,我如何或何时选择存储或不存储这样的数据?
谢谢
I'm using a postgresql install to house my data.
My Post model has an attribute called "selection" which currently stores data within a TEXT column in the form of: "x1,x2,x3,x4,x5..."
When I need to access this data i split it on the the comma and do my thing with it.
I'm prototyping an app so i quickly just did the easiest thing when i was writing it but now i can see an alternative option would be to create a table for "selections" and associate it back to the post, then have individual rows for each bit.
My question is, how or when do i make the choice to store or not data like this?
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
PostgreSQL 有数组类型 - 所以你可以使用“text[]”类型
你也可以对大数据使用索引
PostgreSQL has array types - so you can use a "text[]" type
You can use index for large data too
如果这些代表数据库中其他表中的其他数据元素,那么我永远不会将其存储为逗号分隔的字符串。
SQL 通常针对基于集合的算术和函数进行优化,而不是针对字符串解析。
我能想到的字符串版本可能更容易/更快的唯一场景是,如果您想查找一组特定的值并且仅查找这些值,即
Col = 'A1, B2, C3, d4'.
否则,如果您想要检查各个字段或进行其他比较,则将该数据存储在规范化表中是最好的做法。检查特定值更具可扩展性、更容易且更高效,并且将使该表上的其他操作更快(因为您为主表存储的行内数据更少)。
If those represent other data elements in other tables in your database, then I would never store it as a comma separated string.
SQL in general is optimized for set-based arithmetic and functions, not for string parsing.
The only scenario that I can think of where the string version may be easier/faster is if you want to find a specific set of values and ONLY those values, i.e.
Col = 'A1, B2, C3, d4'
.Otherwise, if you want to check individual fields or do other comparisons, storing that data in a normalized table is the best course of action. It's more extensible, easier and more efficient to check for specific values, and will make other operations on that table quicker (since you store less data in-row for that main table).