Php + mysql prequery prepare and general query performance comparison, mysqlprepare

Source: Internet
Author: User

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

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.