Why does the PHP run faster than MySQL [closed]

北城余情 提交于 2019-12-27 05:35:32

问题


I ran some simple tests: same complex query in mysql and php and measured the times taken to process them. The results are very controversial and suggest that some queries PHP processes faster than MySql.

In theory, shouldn't be the MySql faster? I am want compare how PHP (scripting language) interface decreases the performance of complex SQL query. But why my results suggest otherwise?

Those are averages, I run each query 3 times!

php code:

<?php
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("test3", $con);

    $start = microtime(TRUE);
    $result = mysql_query("SELECT * FROM `test`
    ORDER BY `test`.`test1` DESC
    LIMIT 0,100");


echo "<table border='1'>
<tr>
<th>ID</th>
<th>test1</th>
<th>test2</th>
<th>test3</th>
<th>test4</th>
</tr>";

while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['ID'] . "</td>";
  echo "<td>" . $row['Test1'] . "</td>";
  echo "<td>" . $row['Test2'] . "</td>";
  echo "<td>" . $row['Test3'] . "</td>";
  echo "<td>" . $row['Test4'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

$end = microtime(TRUE);

$sqlTime = $end - $start;


mysql_close($con);
echo $sqlTime
?> 

The results: time in seconds against the number of records:


回答1:


I can see you are using phpmyadmin, it is scripted too to return the values.This might be slow than the speed of your script which is definitely small. Did you consider to run the mysql queries from the command prompt?This might give you a clear difference.




回答2:


There can be numerous reasons why PHP runs faster than MySQL. If I put the MySQL server on my 2008 300$ lenovo netbook and run the PHP on my server, I even get much more speed differences.

The way how you formulated your question shows, that your understanding of performance measured in time in relation to some software only is the issue here.



来源:https://stackoverflow.com/questions/9685444/why-does-the-php-run-faster-than-mysql

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!