YAML或serialize()在MySQL中存储数据 [英] YAML or serialize() to store data in MySQL

查看:1103
本文介绍了YAML或serialize()在MySQL中存储数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在DB中尝试存储临时数据(如cart产品,session_data)。我选择YAML这个而不是serialize()函数。因为YAML数据是人类容易阅读的,并且在编程语言之间是可移植的。

I am trying store temporary data (such as cart products, session_data) in DB. And I choosed YAML for this instead of serialize() function. Because YAML data is easily readable by human and portable between programming languages.

如果我将临时数据存储在数据库中,我会遇到YAML的麻烦吗?

Am I in trouble with YAML if I store my temprory data in database?

推荐答案

我个人不会使用YAML。它太格式化(需要新行,空格等),并没有PHP中的本地解析器。相反,我会使用JSON为此。它本质上是微不足道的,是相当人类可读的(没有像YAML一样多,但更多的比序列化)。这是两个世界中最好的。

Personally, I wouldn't use YAML. It's too format-dependent (Requiring new lines, whitespace, etc) and there's no native parser in PHP. Instead, I'd use JSON for this. It's trivial to handle natively, and is quite human readable (no as much as YAML, but much more so than serialized). It's the best of both worlds.

但是,说真的,你应该问自己一个问题,为什么要存储一个复杂的数据结构的序列化表示在DB中的字段...对于大多数情况下,最好存储数据的规范化表示(因此可以很容易搜索等)。它不是坏存储序列化数据,但它可能不是最佳或正确的选择,取决于你想要做什么。它通常远远优于使用Entity-Attribute-Value存储,但是你需要真正考虑你正在做什么来决定它是否是正确的。

But, with that said, you really should ask yourself the question as to why you want to store a serialized representation of a complex data structure in a field in the DB... For most cases, it might be better to store a normalized representation of the data (so it's searchable easily, etc). It's not "bad" to store serialized data, but it might not be optimal or the right choice depending on what you're trying to do. It's generally far better than using an Entity-Attribute-Value store, but you need to really think about what you're doing to decide if it's the right thing.

这篇关于YAML或serialize()在MySQL中存储数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆