Php + mysql prequery prepare and general query performance comparison, mysqlprepare
Prepare can solve the load and overhead brought by a high-traffic website to the database server. This article introduces the performance comparison between prepare and common queries through examples, for more information, see.
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,); $ StartSecond = substr ($ this-> StartTime, 11,10); $ StopMicro = subs Tr ($ this-> StopTime,); $ StopSecond = substr ($ this-> StopTime,); $ start = floatval ($ StartMicro) + $ StartSecond; $ stop = floatval ($ StopMicro) + $ StopSecond; $ this-> TimeSpent = $ stop-$ start; return round ($ this-> TimeSpent, 8 ). 'second' ;}}$ 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.htmlwhile ($ result = $ query-> fetch_array ()) {$ result [] = array ('name' => $ result ['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> the pre-query time for mysql to run 100000 data entries is :'. $ timer-> spent (); unset ($ timer); // var_dump ($ result );
Normal mysql runs 1000 data entries for 0.011621 seconds
Normal mysql runs 10000 data entries for 0.07766891 seconds
Normal mysql runs 100000 data entries for 0.10834217 seconds
Pre-query mysql runs 1000 data records for 0.00963211 seconds
Pre-query mysql runs 10000 data records for 0.04614592 seconds
Pre-query mysql runs 100000 data records for 0.05989885 seconds