简体   繁体   English

执行时间PHP vs MySQL

[英]Execution time PHP vs MySQL

I have a simple question. 我有一个简单的问题。

Lets say I have two methods of doing a simple sum of values. 可以说,我有两种方法可以对值进行简单sum I can do it with PHP 我可以用PHP做到

foreach($rows as $row)
{
    $total += $row->column;
}

Or I can do it that I can run a query in MySQL database to do the sum 或者我可以在MySQL数据库中运行查询来完成总和

"SELECT sum(column) FROM table";

Which one would finish first. 哪一个先完成。 Assuming there are multiple queries already running in a function like 假设函数中已有多个查询正在运行

public function abc()
{
      //Query 1 Execution from MySQL
      //Query 2 Execution from MySQL

      //Then comes the "SELECT sum(column) FROM table";
      //OR
      //foreach()
}

Usually the DBMS (in this case mysql) is optimized to do sum and other calculations that outperform a loop in php. 通常,对DBMS(在本例中为mysql)进行优化以进行求和和其他计算,这些计算的性能优于php中的循环。 In case you have doubts because there are other things that are needed in your script, you can always do this: 如果由于脚本中还有其他需要而有疑问,可以随时执行以下操作:

$time_start = microtime(true);

// Your code here
.
.
.

$time_end = microtime(true);
$time = $time_end - $time_start;

In the section of your code you can try the different scenarios that you have. 在代码部分中,您可以尝试使用不同的方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM