MySQL & PHP Performance -


i have question regarding differences between mysql_query (which know being deprecated , obsolete) , mysqli , relative performance difference notice.

so putting can - developed api scratch in php 5.4, using mysql v5.0.96, running on lamp server (centos, apache, mysql, php) quad core 3.0ghz, 16gb ram, 100mbps duplex con... standard server really.

to make initial call api postman (great app if don't know - it's google chrome extension worth look) take average of 160ms, considering on other side of world, , have authentication checks, not fussed rest stunned me difference in performance between 2 sets of code below:

the call + return of array of 55 results in indexed table using mysqli below:

    $mysqli = new \mysqli("127.0.0.1", "username", "password", "database", 3306);     if ($mysqli->connect_errno) {         echo "failed connect mysql: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;     }     $result = $mysqli->query("select id, name products order id");     $rows = array();     while($row = $result->fetch_row()) {         $rows[] = $row;     }     return $rows; 

the mysqli above takes average of 850ms, spikes @ 1000ms , lows of 650ms

in comparison, did because thought seemed incredibly high returning array of 55 records, decided use old fashioned mysql_query (which yes know - obsolete etc...) following code, you'll notice i've been careful replicate both identically can using different method.

    $con = mysql_connect("127.0.0.1","username","password");     if (!$con){ die('could not connect: ' . mysql_error()); }     mysql_select_db('database', $con);     $row = array();     $result = mysql_query("select id, name products order id");     while($rows = mysql_fetch_assoc($result)) {         $row[] = $rows;     }     return $row; 

the mysql_query took average of 220ms highs of 300ms , lows of 180ms

can out there point me out why original mysql_query 4 times faster new mysqli?? can't head around why happen... i'd keen know if doing wrong mysqli or weird observation? surely newer mean better, , hence better mean faster / more accurate less computational power required?

mysql_query, traditional way has own advantage due simplicity. mysqli has own.

it depends on mysql database how designed, , accordingly speed depends on it.

if need connect database more once or again , again, better use mysqli not make multi-connections database single host, whereas mysql_query traditional way, make multiple connections, reach maximum number of connection limit of database host. yes more connections, faster work.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

inno setup - TLabel or TNewStaticText - change .Font.Style on Focus like Cursor changes with .Cursor -