写代码不难,优化代码才是真正的技术活
写代码不难,优化代码才是真正的技术活,特别是大数据应用。小弟结合bootstrap-table写了一个服务器端分页,数据量数万条时还好,数据量到达数十万甚至一百万+时,卡得他妈都认不出,换页耗时十几秒到二十几秒。
不知道我的代码有没有优化的空间,请各位兄弟指导、砸砖{:1_893:}{:1_893:}{:1_893:}
<?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();
?> 上个UI图片:
PHP是最好的语言, 分页 还可以继续优化 explain 一下sql 看看慢在哪里 Enter_R 发表于 2019-10-7 17:42
PHP是最好的语言,
是没有之一的语言么?
sort全排序改成手写的二分部分排序
即Qsort基准数两边二分本页位置,可以把O(nlog2n)改成O(log2n) life is short ,you need python Enter_R 发表于 2019-10-7 17:42
PHP是最好的语言,
人生苦短,我用Python 刚走进PHP的大门,看见一堆白头发的年轻人,吃惊的跟我说,PHP是最好的语言,。。。
页:
[1]
2