好友
阅读权限 20
听众
最后登录 1970-1-1
经常需要大量更新数据到MYSQL(根据操作类型决定增改删),动辄数万甚至十几万行,前端POST过来是JSON,为了提高稳定性和避免服务器限制数据大小,前端会将数据分割成1000条每组发到服务器(1000条不到1M)。
问题来了,我写的代码处理这1000条数据竟然要解决20秒,效率很低。我想应该是我写的代码不够科学,请大神抽时间帮忙改改,感谢
<?php
header( "Content-Type: text/html; charset=utf-8" );
//连接数据库
include( "conn.php" );
$arrDiZhi = json_decode( $_POST[ 'arrDaoRuDiZhi' ], true );
$DangQianShiJian = date( "Y-m-d H:i:s" );
//计数变量
$vXinZengT = 0;
$vXinZengF = 0;
$vXiuGaiT = 0;
$vXiuGaiF = 0;
$vShanChuT = 0;
$vShanChuF = 0;
$vLeiXingCuoWu = 0;
foreach ( $arrDiZhi as $arrRow ) {
$DaoRuCaoZuo = $arrRow[ 'DaoRuCaoZuo' ];
$WangGeID = $arrRow[ 'WangGeID' ];
$WangGe = $arrRow[ 'WangGe' ];
$DiZhiID = $arrRow[ 'DiZhiID' ];
$PARENTID = $arrRow[ 'PARENTID' ];
$BenJiMingCheng = $arrRow[ 'BenJiMingCheng' ];
$DiZhiJiBie = $arrRow[ 'DiZhiJiBie' ];
$DiZhiQuanCheng = $arrRow[ 'DiZhiQuanCheng' ];
$ChangJingLeiXing = $arrRow[ 'ChangJingLeiXing' ];
$DiYuShuXing = $arrRow[ 'DiYuShuXing' ];
$DiZhi1Ji = $arrRow[ 'DiZhi1Ji' ];
$DiZhi2Ji = $arrRow[ 'DiZhi2Ji' ];
$DiZhi3Ji = $arrRow[ 'DiZhi3Ji' ];
$DiZhi4Ji = $arrRow[ 'DiZhi4Ji' ];
$DiZhi5Ji = $arrRow[ 'DiZhi5Ji' ];
$WagnLuoLaiYuan = $arrRow[ 'WagnLuoLaiYuan' ];
$DiZhiLaiYuan = $arrRow[ 'DiZhiLaiYuan' ];
if ( $arrRow[ 'DaoRuCaoZuo' ] == 1 ) {
if ( $result = $mysqli->query( "SELECT id FROM dizhi WHERE DiZhiID='$DiZhiID'" ) ) {
$row_cnt = $result->num_rows;
$result->close();
if ( $row_cnt > 0 ) {
//修改
if ( $result = $mysqli->query( "UPDATE `dizhi` SET `WangGeID` = '$WangGeID', `WangGe` = '$WangGe', `PARENTID` = '$PARENTID', `BenJiMingCheng` = '$BenJiMingCheng', `DiZhiJiBie` = '$DiZhiJiBie', `DiZhiQuanCheng` = '$DiZhiQuanCheng', `ChangJingLeiXing` = '$ChangJingLeiXing', `DiYuShuXing` = '$DiYuShuXing', `DiZhi1Ji` = '$DiZhi1Ji', `DiZhi2Ji` = '$DiZhi2Ji', `DiZhi3Ji` = '$DiZhi3Ji', `DiZhi4Ji` = '$DiZhi4Ji', `DiZhi5Ji` = '$DiZhi5Ji', `WagnLuoLaiYuan` = '$WagnLuoLaiYuan', `DiZhiLaiYuan` = '$DiZhiLaiYuan' WHERE `DiZhiID` = '$DiZhiID'" ) ) {
if ( $mysqli->affected_rows > 0 ) {
++$vXiuGaiT;
} else {
++$vXiuGaiF;
};
};
} else {
//新增
$query = "INSERT INTO `dizhi` (`id`, `WangGeID`, `WangGe`, `DiZhiID`, `PARENTID`, `BenJiMingCheng`, `DiZhiJiBie`, `DiZhiQuanCheng`, `ChangJingLeiXing`, `DiYuShuXing`, `DiZhi1Ji`, `DiZhi2Ji`, `DiZhi3Ji`, `DiZhi4Ji`, `DiZhi5Ji`, `WagnLuoLaiYuan`, `DiZhiLaiYuan`, `LuRuShiJian`, `LuRuRen`) VALUES (NULL, '$WangGeID', '$WangGe', '$DiZhiID', '$PARENTID', '$BenJiMingCheng', '$DiZhiJiBie', '$DiZhiQuanCheng','$ChangJingLeiXing', '$DiYuShuXing', '$DiZhi1Ji', '$DiZhi2Ji', '$DiZhi3Ji', '$DiZhi4Ji', '$DiZhi5Ji', '$WagnLuoLaiYuan', '$DiZhiLaiYuan', '$DangQianShiJian', '管理员')";
$mysqli->query( $query );
if ( $mysqli->affected_rows > 0 ) {
++$vXinZengT;
} else {
++$vXinZengF;
};
}; //判断结果集是否大于0
}; //判断是否已存在
} else if ( $arrRow[ 'DaoRuCaoZuo' ] == 0 ) {
//删除
$query = "DELETE FROM dizhi WHERE DiZhiID='$DiZhiID'";
$mysqli->query( $query );
if ( $mysqli->affected_rows > 0 ) {
++$vShanChuT;
} else {
++$vShanChuF;
};
} else {
//操作类型错误
++$vLeiXingCuoWu;
}; //判断操作
}; //循环Row
$mysqli->close();
$arrFanHui = array( $vXinZengT, $vXinZengF, $vXiuGaiT, $vXiuGaiF, $vShanChuT, $vShanChuF, $vLeiXingCuoWu );
echo json_encode( $arrFanHui, JSON_UNESCAPED_UNICODE );
?>