写代码不难,优化代码才是真正的技术活,特别是大数据应用。
小弟结合bootstrap-table写了一个服务器端分页,数据量数万条时还好,数据量到达数十万甚至一百万+时,卡得他妈都认不出,换页耗时十几秒到二十几秒。
不知道我的代码有没有优化的空间,请各位兄弟指导、砸砖
[PHP] 纯文本查看 复制代码 <?php
header( "Content-Type: text/html; charset=utf-8" );
set_time_limit( 60 );
include( "conn.php" );
//拼装搜索语句
$vSouSuo = "";
if ( isset( $_GET[ "search" ] ) && $_GET[ "search" ] != "" ) {
$txtSouSuo = $mysqli->real_escape_string( $_GET[ "search" ] );
$vSouSuo = " WHERE WangGeID LIKE '%" . $txtSouSuo . "%' OR WangGe LIKE '%" . $txtSouSuo . "%' OR DiZhiID LIKE '%" . $txtSouSuo . "%' OR PARENTID LIKE '%" . $txtSouSuo . "%' OR BenJiMingCheng LIKE '%" . $txtSouSuo . "%' OR DiZhiJiBie LIKE '%" . $txtSouSuo . "%' OR DiZhiQuanCheng LIKE '%" . $txtSouSuo . "%' OR ChangJingLeiXing LIKE '%" . $txtSouSuo . "%' OR DiYuShuXing LIKE '%" . $txtSouSuo . "%' OR WagnLuoLaiYuan LIKE '%" . $txtSouSuo . "%' OR DiZhiLaiYuan LIKE '%" . $txtSouSuo . "%'";
};
//拼装排序语句
$vPaiXu = "";
if ( isset( $_GET[ "sort" ] ) ) {
$vPaiXu = " ORDER BY " . $mysqli->real_escape_string( $_GET[ 'sort' ] ) . " " . $mysqli->real_escape_string( $_GET[ 'order' ] );
};
//拼装起点和偏移语句
$vFanWei = "";
if ( isset( $_GET[ "offset" ] ) ) {
$vFanWei = " LIMIT " . $mysqli->real_escape_string( $_GET[ 'offset' ] ) . "," . $mysqli->real_escape_string( $_GET[ 'limit' ] );
};
if ( $result = $mysqli->query( "SELECT WangGeID,WangGe,DiZhiID,PARENTID,BenJiMingCheng,DiZhiJiBie,DiZhiQuanCheng,ChangJingLeiXing,DiYuShuXing,DiZhi1Ji,DiZhi2Ji,DiZhi3Ji,DiZhi4Ji,DiZhi5Ji,WagnLuoLaiYuan,DiZhiLaiYuan,LuRuShiJian,LuRuRen,ShiYongCiShu FROM dizhi$vSouSuo$vPaiXu$vFanWei" ) ) {
$arr = array();
if ( $result->num_rows > 0 ) {
// 写入数组
while ( $row = $result->fetch_assoc() ) {
array_push( $arr, $row );
};
$result->close();
// 获得总页数
if ( $vSouSuo != "" ) {
//如果有搜索条件
if ( $result = $mysqli->query( "SELECT id FROM dizhi$vSouSuo" ) ) {
$vHangShu = $result->num_rows;
$result->close();
};
} else {
//如果没有搜索条件
if ( $result = $mysqli->query( "SELECT id FROM dizhi" ) ) {
$vHangShu = $result->num_rows;
$result->close();
};
};
// 返回汇总数组
$arrHuiZong = array( "total" => $vHangShu, "totalNotFiltered" => $vHangShu, "rows" => $arr );
echo json_encode( $arrHuiZong, JSON_UNESCAPED_UNICODE );
};
};
$mysqli->close();
?> |