Php+mysql prepare and ordinary query performance comparison example explained _mysql

Source: Internet
Author: User
Tags php class prepare

Performance comparison of Php+mysql prepare and normal query

The instance code is as follows:

<?php class Timer {public $StartTime = 0;   
    Public $StopTime = 0;   
      
    Public $TimeSpent = 0;   
    function Start () {$this->starttime = Microtime ();   
    function Stop () {$this->stoptime = Microtime ();   
 
      function spent () {if ($this->timespent) {return $this->timespent;   
        else {//http://www.manongjc.com $StartMicro = substr ($this->starttime,0,10);   
        $StartSecond = substr ($this->starttime,11,10);   
        $StopMicro = substr ($this->stoptime,0,10);   
        $StopSecond = substr ($this->stoptime,11,10);   
        $start = Floatval ($StartMicro) + $StartSecond;   
        $stop = Floatval ($StopMicro) + $StopSecond; 
         
      $this->timespent = $stop-$start; Return round ($this->timespent,8). '   
      Seconds ';   
}} $timer = new timer;  
 
$timer->start (); $mysql = new Mysqli (' LocalHost ', ' root ', ' root ', ' ganbaobao_ucenter '; 
/* $query = $mysql->query ("Select Username,email from Uc_members where UID < 100000"); 
$result = Array (); http://www.manongjc.com/article/1194.html while ($result = $query->fetch_array ()) {$result [] = Array (' name ' => $r 
esult[' username ', ' email ' => $result [' email ']); 
 
} */$query _prepare = $mysql->prepare ("Select Username,email from Uc_members where UID <"); 
$id = 100000; 
 
$query _prepare->bind_param ("i", $id); 
$query _prepare->execute (); 
 
$query _prepare->bind_result ($username, $email); 
$result = Array (); 
while ($query _prepare->fetch ()) {$result [] = Array (' name ' => $username, ' email ' => $email);  
} $timer->stop ();  
Echo ' </br> pre-query MySQL run 100,000 data time: '. $timer->spent ();  
Unset ($timer); 
 Var_dump ($result);

Run Result:

Normal MySQL run 1000 data time: 0.011621 seconds

Normal MySQL Run 10,000 data time: 0.07766891 seconds

Normal MySQL run 100,000 data time: 0.10834217 seconds

Pre-query MySQL run 1000 data time: 0.00963211 seconds

Pre-query MySQL run 10,000 data time: 0.04614592 seconds

Pre-query MySQL run 100,000 data time: 0.05989885 seconds

Thank you for reading, I hope to help you, thank you for your support for this site!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.