JSON 与数据库中的序列化数组 [已关闭]
2022-08-30 08:43:36
与序列化数组相比,在MySQL数据库中存储JSON数据有哪些优点和缺点?
与序列化数组相比,在MySQL数据库中存储JSON数据有哪些优点和缺点?
选择权在您手中。
如注释所示,JSON 占用的空间比序列化数组少。我还检查了JSON或序列化是否更快,令人惊讶的是,JSON编码比序列化更快。不过,取消序列化比JSON解码更快。
这是我用来测试的脚本:
<?php
function runTime(){
$mtime = microtime();
$mtime = explode(' ', $mtime);
$mtime = $mtime[1] + $mtime[0];
return $mtime;
}
?>
<pre>
<?php
$start = runTime();
$ser;
for($i=0; $i<1000; $i++){
$a = array(a => 1, x => 10);
$ser = serialize($a);
}
$total = runTime() - $start;
echo "Serializing 1000 times took \t$total seconds";
?>
<?php
$start = runTime();
$json;
for($i=0; $i<1000; $i++){
$a = array(a => 1, x => 10);
$json = json_encode($a);
}
$total = runTime() - $start;
echo "JSON encoding 1000 times took \t$total seconds";
?>
<?php
$start = runTime();
$ser;
for($i=0; $i<1000; $i++){
$a = unserialize($ser);
}
$total = runTime() - $start;
echo "Unserializing 1000 times took \t$total seconds";
?>
<?php
$start = runTime();
$json;
for($i=0; $i<1000; $i++){
$a = json_decode($json);
}
$total = runTime() - $start;
echo "JSON decoding 1000 times took \t$total seconds";
?>
</pre>