为什么PHP运行速度比MySQL快 [英] Why does the PHP run faster than MySQL

查看:50
本文介绍了为什么PHP运行速度比MySQL快的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我运行了一些简单的测试:在 mysql 和 php 中进行了相同的复杂查询,并测量了处理它们所需的时间.结果非常有争议,并表明 PHP 处理某些查询比 MySql 更快.

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.

理论上,不应该是MySql更快吗?我想比较 PHP(脚本语言)界面如何降低复杂 SQL 查询的性能.但为什么我的结果表明并非如此?

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?

这些是平均值,我每个查询运行 3 次!

Those are averages, I run each query 3 times!

php 代码:

<?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:

推荐答案

我可以看到您正在使用 phpmyadmin,它也被脚本化以返回值.这可能比您的脚本速度慢,这肯定很小.您是否考虑过从命令提示符运行 mysql 查询?这可能会给您带来明显的不同.

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.

这篇关于为什么PHP运行速度比MySQL快的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆