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