简体   繁体   English

PHP json_decode Big(O)和优化

[英]PHP json_decode Big(O) and optimization

I am trying to write php code which takes an NSArray of NSDictionaries and adds the records to a database. 我正在尝试编写PHP代码,该代码采用NSDictionaries的NSArray并将记录添加到数据库中。 Unfortunately, this is taking around 20 seconds to process around 500 records with a total size of around 2mb. 不幸的是,这大约需要20秒来处理大约500条记录,总大小约为2mb。

function uploadTracks($tracks, $partyID, $pName) {
$tracks = json_decode($tracks, true);

$itemInfo = array();

foreach($tracks as $itemInfo){
    $track = $itemInfo['songTitle'];
    $artist = $itemInfo['artistName'];
    $album = $itemInfo['albumName'];
    $artwork = $itemInfo['artwork'];
    $result = query("INSERT INTO partyRecords(Pkey,songName,songArtist,imageBinary,partyName) VALUES ('%s','%s','%s','%s','%s')" ,$partyID,$track,$artist,$artwork,$pName);
}


}

Is there anyway to optimize the above code? 反正有优化上面的代码吗? Could json_decode be what is taking the most time? json_decode可能是花费最多的时间吗?

You can insert multiple records in single insert query. 您可以在单个插入查询中插入多个记录。 In this case your db indexes will be updated once and it gain you performance boost: 在这种情况下,您的数据库索引将更新一次,从而提高性能:

INSERT INTO partyRecords
  (Pkey, songName, songArtist, imageBinary, partyName)
VALUES
  (1, 'Name 1', 'Artist 1', 'Image 1', 'Party 1'),
  (2, 'Name 2', 'Artist 2', 'Image 2', 'Party 2'),
  (3, 'Name 3', 'Artist 3', 'Image 3', 'Party 3'),
  (4, 'Name 4', 'Artist 4', 'Image 4', 'Party 4');

I would say 95% it is the queries. 我会说95%是查询。 You need to compile the queries and insert them 100 or so at a time. 您需要编译查询,并一次插入100个左右。 It would be best to escape the query variables yourself (mysql: mysql_real_escape_string() ). 最好自己转义查询变量(mysql:mysql_real_escape_string())。

INSERT INTO partyRecords(Pkey,songName,songArtist,imageBinary,partyName) VALUES (2,'12312','12312321','12312332423','23423432');INSERT INTO partyRecords(Pkey,songName,songArtist,imageBinary,partyName) VALUES (2,'12312','12312321','12312332423','23423432');...

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

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