Performance comparison of Php+mysql pre-query prepare and normal query

Source: Internet
Author: User

Prepare can solve the large-volume Web site to the database server load and cost, this article through an example to introduce the pre-query prepare and the performance of ordinary query comparison, the need for friends can refer to.

The instance code is as follows:

<?PHPclassTimer { Public $StartTime= 0;  Public $StopTime= 0;  Public $TimeSpent= 0; functionstart () {$this->starttime =Microtime(); }                         functionStop () {$this->stoptime =Microtime(); }                         functionspent () {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=Newtimer; $timer-start (); $mysql=NewMysqli (' localhost ', ' root ', ' root ', ' ganbaobao_ucenter '); /*$query = $mysql->query ("Select Username,email from Uc_members where UID < 100000"); $result = Array (); http://w Ww.manongjc.com/article/1194.htmlwhile ($result = $query->fetch_array ()) {$result [] = Array (' name ' = = $result [' Username '], ' email ' and $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 for: '.$timer-spent (); unset($timer); //Var_dump ($result); 

Normal MySQL run 1000 data time is: 0.011621 seconds

Normal MySQL Run 10,000 data time is: 0.07766891 seconds

Normal MySQL run 100,000 data time is: 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

Performance comparison of Php+mysql pre-query prepare and normal query

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.