我对MongoDb感到非常兴奋,并且最近对其进行了测试。我在MySQL中有一个名为posts的表,其中大约2000万条记录仅在名为“ id”的字段上建立索引。
我想将速度与MongoDB进行比较,然后运行了一个测试,该测试将从我们的大型数据库中随机获取并打印15条记录。我为mysql和MongoDB分别运行了约1,000次查询,但我惊讶地发现我在速度上并没有太大差异。也许MongoDB快1.1倍。真令人失望。我做错什么了吗?我知道我的测试并不完美,但是在阅读繁琐的杂物方面,MySQL与MongoDb相当。
注意:
- 我有双核+(2个线程)i7 cpu和4GB ram
- 我在MySQL上有20个分区,每个分区有1百万条记录
用于测试MongoDB的示例代码
<?php
function microtime_float()
{
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
$time_taken = 0;
$tries = 100;
// connect
$time_start = microtime_float();
for($i=1;$i<=$tries;$i++)
{
$m = new Mongo();
$db = $m->swalif;
$cursor = $db->posts->find(array('id' => array('$in' => get_15_random_numbers())));
foreach ($cursor as $obj)
{
//echo $obj["thread_title"] . "<br><Br>";
}
}
$time_end = microtime_float();
$time_taken = $time_taken + ($time_end - $time_start);
echo $time_taken;
function get_15_random_numbers()
{
$numbers = array();
for($i=1;$i<=15;$i++)
{
$numbers[] = mt_rand(1, 20000000) ;
}
return $numbers;
}
?>
测试MySQL的样例代码
<?php
function microtime_float()
{
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
$BASE_PATH = "../src/";
include_once($BASE_PATH . "classes/forumdb.php");
$time_taken = 0;
$tries = 100;
$time_start = microtime_float();
for($i=1;$i<=$tries;$i++)
{
$db = new AQLDatabase();
$sql = "select * from posts_really_big where id in (".implode(',',get_15_random_numbers()).")";
$result = $db->executeSQL($sql);
while ($row = mysql_fetch_array($result) )
{
//echo $row["thread_title"] . "<br><Br>";
}
}
$time_end = microtime_float();
$time_taken = $time_taken + ($time_end - $time_start);
echo $time_taken;
function get_15_random_numbers()
{
$numbers = array();
for($i=1;$i<=15;$i++)
{
$numbers[] = mt_rand(1, 20000000);
}
return $numbers;
}
?>