我正在尝试编写PHP代码,该代码采用NSDrray的NSDrray并将记录添加到数据库中。不幸的是,这需要大约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);
}
}
有没有优化上面的代码? json_decode可以占用最多的时间吗?
答案 0 :(得分:1)
您可以在单个插入查询中插入多个记录。在这种情况下,您的数据库索引将更新一次,并获得性能提升:
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');
答案 1 :(得分:0)
我会说95%是查询。您需要编译查询并一次插入100个左右。最好自己转义查询变量(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');...