【发布时间】:2016-05-10 04:41:14
【问题描述】:
我不喜欢新的 mongo,MongoDB 需要 PHP7 中的多个库。
php 5 中的 MongoClient(已弃用)更加舒适和轻便!
我决定启动一个脚本并比较两个版本,结果非常令人惊讶:
MongoDB (PHP 7.0.2)
$client = new MongoDB\Client(
'mongodb://root:password@localhost:port',
['readPreference' => 'secondaryPreferred']
);
$db = $client->selectDatabase('namedb');
$collection = $client->selectCollection('namedb', 'test');
$document = $collection->findOne(['_id' => 'works']);
var_dump($document);
$time = microtime();
$time = explode(' ', $time);
$time = $time[1] + $time[0];
$finish = $time;
$total_time = round(($finish - $start), 4);
echo '<br><br>Page generated in ' . $total_time . ' seconds.';
Mongo/MongoClient (PHP 5.6.17)
$db = new MongoClient('mongodb://root:password@localhost:port');
$c = $db->namedb->test;
$a = $c->findOne(array("_id" => 'works'));
var_dump($a);
$time = microtime();
$time = explode(' ', $time);
$time = $time[1] + $time[0];
$finish = $time;
$total_time = round(($finish - $start), 4);
echo '<br><br>Page generated in ' . $total_time . ' seconds.';
还有其他人遇到过这个问题吗? 用新版的MongoDB没发现什么好处,都是问题!
【问题讨论】:
-
MongoDB仅提供低级 API。如果你想要一个更高级别的 API,比如 MongoDB 驱动程序为其他语言提供的,你需要使用 MongoDB 的 PHPLib。查看有关 MongoDB 的 php 手册,phplib 链接在那里。 -
另外你需要使用
microtime(true)!如果没有将参数设置为true,您将返回一个字符串,这将在数学运算中得到错误的结果。 -
$start = microtime(true); ....your mongodb operations... $totaltime = microtime(true) - $start;真的没必要爆炸什么的。 -
@CharlotteDunois 我已经安装并查看结果。
-
Github 问题将是提出此类问题的一个更有成效的地方......性能的回归仍然是回归,会有差异,但我相信维护者愿意无论如何都知道...