我对MongoDb非常兴奋,最近一直在测试它。我在MySQL中有一个叫posts的表,大约有2000万条记录,索引只在一个名为“id”的字段上。

我想与MongoDB比较速度,我运行了一个测试,从我们巨大的数据库中随机获取并打印15条记录。我为mysql和MongoDB分别运行了大约1000次查询,我很惊讶我没有注意到速度上有很大的差异。也许MongoDB快1.1倍。这太令人失望了。我做错什么了吗?我知道我的测试并不完美,但当涉及到读取密集的杂务时,MySQL与MongoDb不相上下。

注意:

我有双核+(2线程)i7 cpu和4GB ram 我在MySQL上有20个分区,每个分区有100万条记录

用于测试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;
}
?>

当前回答

来自MongoDB官方网站

观察这两个系统的一些高级查询行为,我们可以看到MySQL在选择大量记录时速度更快,而MongoDB在插入或更新大量记录时速度明显更快。

参考

其他回答

来源:https://github.com/webcaetano/mongo-mysql

10行

mysql insert: 1702ms
mysql select: 11ms

mongo insert: 47ms
mongo select: 12ms

100行

mysql insert: 8171ms
mysql select: 10ms

mongo insert: 167ms
mongo select: 60ms

1000行

mysql insert: 94813ms (1.58 minutes)
mysql select: 13ms

mongo insert: 1013ms
mongo select: 677ms

10.000行

mysql insert: 924695ms (15.41 minutes)
mysql select: 144ms

mongo insert: 9956ms (9.95 seconds)
mongo select: 4539ms (4.539 seconds)

答案是你基本上是在测试PHP而不是数据库。

不要费心迭代结果,不管是否注释掉打印结果。还有很多时间。

   foreach ($cursor as $obj)
    {
        //echo $obj["thread_title"] . "<br><Br>";
    }

而另一部分则是花在一堆兰特数字上。

function get_15_random_numbers()
{
    $numbers = array();
    for($i=1;$i<=15;$i++)
    {
        $numbers[] = mt_rand(1, 20000000) ;

    }
    return $numbers;
}

然后有一个主要的区别b/w内爆和在。

最后是这里发生了什么。看起来像是每次都创建一个连接,因此它测试连接时间加上查询时间。

$m = new Mongo();

vs

$db = new AQLDatabase();

因此,对于去除jazz的底层查询,您的101%的速度可能会提高1000%。

呃。

在单服务器上,给定表/doc, MongoDb在读写方面不会比mysql MyISAM更快 大小从1gb到20gb不等。 在多节点集群上,MonoDB在并行缩减(Parallel Reduce)上速度更快,而Mysql不能水平扩展。

这里有一个小研究,探讨了RDBMS vs NoSQL使用MySQL vs Mongo,结论是一致的@Sean Reilly的回应。简而言之,好处来自于设计,而不是一些原始的速度差异。35-36页结论:

RDBMS vs NoSQL:性能和伸缩性比较

The project tested, analysed and compared the performance and scalability of the two database types. The experiments done included running different numbers and types of queries, some more complex than others, in order to analyse how the databases scaled with increased load. The most important factor in this case was the query type used as MongoDB could handle more complex queries faster due mainly to its simpler schema at the sacrifice of data duplication meaning that a NoSQL database may contain large amounts of data duplicates. Although a schema directly migrated from the RDBMS could be used this would eliminate the advantage of MongoDB’s underlying data representation of subdocuments which allowed the use of less queries towards the database as tables were combined. Despite the performance gain which MongoDB had over MySQL in these complex queries, when the benchmark modelled the MySQL query similarly to the MongoDB complex query by using nested SELECTs MySQL performed best although at higher numbers of connections the two behaved similarly. The last type of query benchmarked which was the complex query containing two JOINS and and a subquery showed the advantage MongoDB has over MySQL due to its use of subdocuments. This advantage comes at the cost of data duplication which causes an increase in the database size. If such queries are typical in an application then it is important to consider NoSQL databases as alternatives while taking in account the cost in storage and memory size resulting from the larger database size.

老实说,即使MongoDB更慢,MongoDB肯定会让我和你的代码更快....不需要担心愚蠢的表列,行或实体迁移…

使用MongoDB,您只需实例化一个类并保存!