【问题标题】:Php/MySQL PerformancePHP/MySQL 性能
【发布时间】:2014-02-25 15:28:36
【问题描述】:

假设我必须通过一个 php 对象进行 MySQL 查询,有什么更好的方法?,使该方法显式返回一条记录并调用该方法,假设 1000 次(改变参数)或给该方法的范围为 1000并一次调用返回 1000 条记录?都是在速度/性能方面。

【问题讨论】:

  • 不费吹灰之力:进行一次返回 1000 条记录的调用....每次调用数据库都是开销,您进行的查询越少,性能就会越好。 1000 次调用是很多开销,1 次调用减少 1000 倍

标签: php mysql performance web-services web


【解决方案1】:

没有什么比一点运动更好的了

如果你想玩得开心一点,你可以自己试试这个:

function getOneRecordAtATime()
{
    for ($i = 0; $i < 1000; $i++)
    {
        // call the db and retrieve one record
    }
}

function getAllRecords()
{
    // call the db once and retrieve 1000 records
}

$startOne = microtime(true);
getOneRecordAtATime();
$endOne = microtime(true);

$startTwo = microtime(true);
getAllRecords();
$endTwo = microtime(true);

$timeOne = ($endOne - $startOne) * 1000;
$timeTwo = ($endTwo - $startTwo) * 1000;

print "Time to fetch one record at a time, 1000 times : ".$timeOne." ms\n";
print "Time to fetch 1000 records at once : ".$timeTwo." ms\n";

告诉我们结果;)

【讨论】:

  • "一次获取一条记录的时间,1000 次:9.97631549835E-5 ms 一次获取 1000 条记录的时间:2.9571056366E-6 ms",正如 Mark Berker 所说,“越少你提出的查询会更好。”
  • 哎呀 - 我的意思是 * 1000 / 1000 - 但比例保持不变,你有 99 毫秒,而 2.9 毫秒是 30 倍,这是天文数字。
  • 感谢分享结果!
猜你喜欢
  • 2014-05-20
  • 1970-01-01
  • 1970-01-01
  • 2016-09-11
  • 2014-11-24
  • 2016-09-05
  • 1970-01-01
  • 1970-01-01
  • 2017-10-09
相关资源
最近更新 更多