>백엔드 개발 >PHP 튜토리얼 >访问量一大功能就不能正确实现了,求高手帮忙优化两段PHP代码解决方法

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

WBOY
WBOY원래의
2016-06-13 10:14:52972검색

访问量一大功能就不能正确实现了,求高手帮忙优化两段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加上索引就行了
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.