访问量一大功能就不能正确实现了,求高手帮忙优化两段PHP代码解决方法

WBOY
リリース: 2016-06-13 10:14:52
オリジナル
945 人が閲覧しました

访问量一大功能就不能正确实现了,求高手帮忙优化两段PHP代码
第一段:

PHP code
<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--><?php @session_start();date_default_timezone_set('Asia/Shanghai');$link_id =@ mysql_connect('localhost', 'root', 'password');if ($link_id) {     mysql_select_db('database');     mysql_query("set names 'utf8'"); //select 数据库之后加多这一句} else{    echo "数据库连接错误!";}//var_dump($current);$Datetime=date("Y-m-d G:i:s");//获取注册时间,也就是数据写入到用户表的时间$bijiao="select max(current) from online";[email&#160;protected]_query($bijiao,$link_id);$most= intval( @mysql_result($max,0) );$current= intval($_SESSION['current']);$query="insert into online(current,mostcount,time)  values('".$current."','".$most."','$Datetime')";$result=mysql_query($query);echo $most;?>
ログイン後にコピー

第二段:
PHP code
<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--><?php @session_start();date_default_timezone_set('Asia/Shanghai');$link_id =@ mysql_connect('localhost', 'root', 'password');if ($link_id) {     mysql_select_db('database');     mysql_query("set names 'utf8'"); //select 数据库之后加多这一句} else{    echo "数据库连接错误!";}$bijiao="select max(mostcount) from online";[email&#160;protected]_query($bijiao,$link_id);$most= intval( @mysql_result($max,0) );$time="select time from online where mostcount='$most'";[email&#160;protected]_query($time,$link_id);[email&#160;protected]_result($time,0);$already="select max(mostcount) from record";[email&#160;protected]_query($already,$link_id);$already= intval( @mysql_result($already,0) );if($most>$already){    $query="insert into record(mostcount,time)  values('".$most."','$time')";    [email protected]_query($query);}?>
ログイン後にコピー


------解决方案--------------------
第一段与第二端的算法大致相同
第二段的算法有点胡闹

如果你对未加索引的 current 或 mostcount 做 max 操作,势必占用大量的时间
------解决方案--------------------
你在数据库中给current和mostcount加上索引就行了
関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!