简体   繁体   中英

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

vs

B - storing the data as fifty numeric fields and having mysql do some sums and avgs instead

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. Perhaps in a JSON encoded field, but not serialized.

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. Meaning that you're not sorting on it, you're not using it in a where clause, you're not aggregating the data, etc.

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. It'll be significantly more efficient than you could make PHP be...

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.

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.

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.

Joomla and Wordpress do this too. Typo3 has some tables with lots and lots of columns, and that is kind of ugly :-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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