加入收藏 | 设为首页 | 会员中心 | 我要投稿 成都站长网 (https://www.028zz.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 云计算 > 正文

php+mysql prepare 与普通查询的性能对比实例讲解

发布时间:2016-12-06 13:28:39 所属栏目:云计算 来源:站长网
导读:php+mysql prepare 与普通查询的性能对比 实例代码如下: lt;#63;php class timer { public $StartTime = 0; public $StopTime = 0; public $TimeSpent = 0; function start(){ $this-gt;StartTime = microtime(); } function stop(){ $this-gt;StopTime =

php+mysql prepare 与普通查询的性能对比

实例代码如下:

lt;#63;php 
class timer {   
    public $StartTime = 0;   
    public $StopTime = 0;   
    public $TimeSpent = 0;   
      
    function start(){   
      $this-gt;StartTime = microtime();   
    }   
      
    function stop(){   
      $this-gt;StopTime = microtime();   
    }   
      
    function spent() {   
      if ($this-gt;TimeSpent) {   
      return $this-gt;TimeSpent;   
 
      } else {  
        // http://www.manongjc.com
        $StartMicro = substr($this-gt;StartTime,0,10);   
        $StartSecond = substr($this-gt;StartTime,11,10);   
        $StopMicro = substr($this-gt;StopTime,0,10);   
        $StopSecond = substr($this-gt;StopTime,11,10);   
        $start = floatval($StartMicro) + $StartSecond;   
        $stop = floatval($StopMicro) + $StopSecond;   
        $this-gt;TimeSpent = $stop - $start; 
         
      return round($this-gt;TimeSpent,8).'秒';   
      }  
    }  
  
} 
 
$timer = new timer;   
$timer-gt;start();  
 
$mysql = new mysqli('localhost','root','root','ganbaobao_ucenter'); 
 
/* 
$query = $mysql-gt;query("select username,email from uc_members where uid lt; 100000"); 
$result = array(); 
http://www.manongjc.com/article/1194.html
while($result = $query-gt;fetch_array()) 
{ 
  $result[] = array('name'=gt;$result['username'],'email'=gt;$result['email']); 
} 
*/ 
$query_prepare = $mysql-gt;prepare("select username,email from uc_members where uid lt; #63;"); 
 
$id = 100000; 
$query_prepare-gt;bind_param("i",$id); 
 
$query_prepare-gt;execute(); 
$query_prepare-gt;bind_result($username,$email); 
 
$result = array(); 
while($query_prepare-gt;fetch()) 
{ 
  $result[] = array('name'=gt;$username,'email'=gt;$email); 
} 
 
$timer-gt;stop();  
echo 'lt;/brgt;预查询mysql运行100000条数据时间为: '.$timer-gt;spent();  
unset($timer);  
//var_dump($result); 

运行结果:

普通mysql运行1000条数据时间为: 0.011621秒

普通mysql运行10000条数据时间为: 0.07766891秒

普通mysql运行100000条数据时间为: 0.10834217秒

预查询mysql运行1000条数据时间为: 0.00963211秒

预查询mysql运行10000条数据时间为: 0.04614592秒

预查询mysql运行100000条数据时间为: 0.05989885秒

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

(编辑:成都站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读