简体   繁体   English

处理序列化数据与添加更多字段-PHP-MySQL

[英]crunching serialized data vs adding more fields - php - mysql

okay, let's pretend i've got fifty pieces of information that i want to store in each record of a table. 好的,让我们假设我有50条要存储在表的每个记录中的信息。 when i pull the data out, i'm going to be doing basic maths on some of them. 当我提取数据时,我将对其中一些进行基本数学运算。 on any given page request, i'm going to pull out a hundred records and do the calculations. 在任何给定的页面请求中,我将提取一百条记录并进行计算。

what are the performance impacts of: 有哪些性能影响:

A - storing the data as a serialized array in a single field and doing the crunching in php A-将数据作为序列化数组存储在单个字段中,并在php中进行运算

vs

B - storing the data as fifty numeric fields and having mysql do some sums and avgs instead B-将数据存储为五十个数字字段,并让mysql做一些总和和平均数

please assume that normalization is not an issue in those fifty fields. 请假设在这五十个领域中标准化不是问题。 please also assume that i don't need to sort by any of these fields. 还请假设我不需要按任何这些字段进行排序。

thanks in advance! 提前致谢!

First, I would never store data serialized , it's just not portable enough. 首先,我永远不会存储serialized数据,因为它的可移植性不够。 Perhaps in a JSON encoded field, but not serialized. 也许在JSON编码字段中,但未序列化。

Second, if you're doing anything with the data (searching, aggregating, etc), make them columns in the table. 其次,如果您要对数据做任何事情(搜索,汇总等),请将它们设置为表中的列。 And I do mean anything (sorting, etc). 我的意思是什么(排序等)。

The only time it's even acceptable to store formatted data (serialized, json, etc) in a column is if it's read only. 在列中存储格式化数据(序列化,json等)的唯一一次是只读的。 Meaning that you're not sorting on it, you're not using it in a where clause, you're not aggregating the data, etc. 这意味着您没有对它进行排序,没有在where子句中使用它,没有在汇总数据,等等。

Database servers are very efficient at doing set-based operations. 数据库服务器在执行基于集合的操作时非常高效。 So if you're doing any kind of aggregation (summing, etc), do it in MySQL. 因此,如果您要进行任何类型的汇总(求和等),请在MySQL中进行。 It'll be significantly more efficient than you could make PHP be... 它将比使PHP变得更加高效。

MySQL几乎肯定会比PHP更快地执行这些计算。

While I would almost always recommend option B, I'm running into a unique situation myself where storing serialized into a text field might make more sense. 尽管我几乎总是建议使用选项B,但我本人也遇到了一种独特的情况,在这种情况下,将序列化存储到文本字段中可能更有意义。

I have a client who has an application form on their website. 我有一个客户,他们的网站上有一份申请表。 There are around 50 fields on the form, and all the data will only ever be read only. 表单上大约有50个字段,并且所有数据将只能是只读的。

Moreover, this application may change over time. 此外,此应用程序可能会随着时间而改变。 Fields may be added, fields may be removed. 可以添加字段,可以删除字段。 By using serialized data, I can save all the questions and answers in a serialized format. 通过使用序列化数据,我可以将所有问题和答案以序列化格式保存。 If the form changes, the old data stays in tact, along with the original questions. 如果表格更改,则旧数据以及原始问题将保持不变。

I go with Jonathan! 我和乔纳森一起去! If you have a table where the number of fields would vary depending on the options or contents the user makes, and those fields are neither aggregated nor calculated, i would serialize(and base64_encode) or json_encode the values too. 如果您有一个表,其中字段的数量将根据用户做出的选择或内容而变化,并且这些字段既不聚合也不计算,那么我也会对这些值进行序列化(和base64_encode)或json_encode。

Joomla and Wordpress do this too. Joomla和Wordpress也这样做。 Typo3 has some tables with lots and lots of columns, and that is kind of ugly :-) Typo3的一些表有很多列,这很丑陋:-)

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

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