MySQL vs MongoDB 1000读取

    技术2023-05-28  40

    本文翻译自:MySQL vs MongoDB 1000 reads

    I have been very excited about MongoDb and have been testing it lately. 我对MongoDb感到非常兴奋,并且最近对其进行了测试。 I had a table called posts in MySQL with about 20 million records indexed only on a field called 'id'. 我在MySQL中有一个名为posts的表,其中大约2000万条记录仅在名为“ id”的字段上建立索引。

    I wanted to compare speed with MongoDB and I ran a test which would get and print 15 records randomly from our huge databases. 我想将速度与MongoDB进行比较,然后运行了一个测试,该测试将从我们的大型数据库中随机获取并打印15条记录。 I ran the query about 1,000 times each for mysql and MongoDB and I am suprised that I do not notice a lot of difference in speed. 我为mysql和MongoDB分别运行了约1,000次查询,但我很惊讶我没有注意到速度上的很大差异。 Maybe MongoDB is 1.1 times faster. 也许MongoDB快1.1倍。 That's very disappointing. 真令人失望。 Is there something I am doing wrong? 我做错什么了吗? I know that my tests are not perfect but is MySQL on par with MongoDb when it comes to read intensive chores. 我知道我的测试并不完美,但是在阅读繁琐的杂物方面,MySQL与MongoDb相当。

    Note: 注意:

    I have dual core + ( 2 threads ) i7 cpu and 4GB ram 我有双核+(2个线程)i7 cpu和4GB ram I have 20 partitions on MySQL each of 1 million records 我在MySQL上有20个分区,每个分区有1百万条记录

    Sample Code Used For Testing MongoDB 用于测试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; } ?>

    Sample Code For Testing MySQL 测试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; } ?>

    #1楼

    参考:https://stackoom.com/question/ei6F/MySQL-vs-MongoDB-读取


    #2楼

    On Single Server, MongoDb would not be any faster than mysql MyISAM on both read and write, given table/doc sizes are small 1 GB to 20 GB. 在单服务器上,给定的表/文档大小在1 GB到20 GB之间,MongoDb在读写方面都不会比mysql MyISAM快。 MonoDB will be faster on Parallel Reduce on Multi-Node clusters, where Mysql can NOT scale horizontally. 在Mysql无法水平扩展的多节点群集上,MonoDB在Parallel Reduce上会更快。


    #3楼

    man,,, the answer is that you're basically testing PHP and not a database. 答案是,您基本上是在测试PHP,而不是数据库。

    don't bother iterating the results, whether commenting out the print or not. 无论是否注释打印件,都不必理会结果。 there's a chunk of time. 有很多时间。

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

    while the other chunk is spend yacking up a bunch of rand numbers. 而另一部分是花费大量的兰特数字。

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

    then theres a major difference b/w implode and in. 然后黑白内爆和内爆之间存在重大差异。

    and finally what is going on here. 最后是这里发生的事情。 looks like creating a connection each time, thus its testing the connection time plus the query time. 看起来每次都创建一个连接,因此它测试了连接时间加上查询时间。

    $m = new Mongo();

    vs

    $db = new AQLDatabase();

    so your 101% faster might turn out to be 1000% faster for the underlying query stripped of jazz. 因此对于去除爵士乐的基础查询,您的速度提高101%可能会快1000%。

    urghhh. 恩。


    #4楼

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

    10 rows 10行

    mysql insert: 1702ms mysql select: 11ms mongo insert: 47ms mongo select: 12ms

    100 rows 100行

    mysql insert: 8171ms mysql select: 10ms mongo insert: 167ms mongo select: 60ms

    1000 rows 1000行

    mysql insert: 94813ms (1.58 minutes) mysql select: 13ms mongo insert: 1013ms mongo select: 677ms

    10.000 rows 10.000行

    mysql insert: 924695ms (15.41 minutes) mysql select: 144ms mongo insert: 9956ms (9.95 seconds) mongo select: 4539ms (4.539 seconds)

    #5楼

    https://github.com/reoxey/benchmark https://github.com/reoxey/benchmark

    benchmark 基准

    speed comparison of MySQL & MongoDB in GOLANG1.6 & PHP5 GOLANG1.6和PHP5中MySQL和MongoDB的速度比较

    system used for benchmark: DELL cpu i5 4th gen 1.70Ghz * 4 ram 4GB GPU ram 2GB 用于基准测试的系统:DELL cpu i5 4th gen 1.70Ghz * 4 ram 4GB GPU ram 2GB

    Speed comparison of RDBMS vs NoSQL for INSERT, SELECT, UPDATE, DELETE executing different number of rows 10,100,1000,10000,100000,1000000 针对执行不同行数10,100,1000,10000,100000,1000000的INSERT,SELECT,UPDATE,DELETE的RDBMS与NoSQL的速度比较

    Language used to execute is: PHP5 & Google fastest language GO 1.6 用于执行的语言是:PHP5和Google最快的语言GO 1.6

    ________________________________________________ GOLANG with MySQL (engine = MyISAM) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ INSERT ------------------------------------------------ num of rows time taken ------------------------------------------------ 10 1.195444ms 100 6.075053ms 1000 47.439699ms 10000 483.999809ms 100000 4.707089053s 1000000 49.067407174s SELECT ------------------------------------------------ num of rows time taken ------------------------------------------------ 1000000 872.709µs SELECT & DISPLAY ------------------------------------------------ num of rows time taken ------------------------------------------------ 1000000 20.717354746s UPDATE ------------------------------------------------ num of rows time taken ------------------------------------------------ 1000000 2.309209968s 100000 257.411502ms 10000 26.73954ms 1000 3.483926ms 100 915.17µs 10 650.166µs DELETE ------------------------------------------------ num of rows time taken ------------------------------------------------ 1000000 6.065949ms ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ________________________________________________ GOLANG with MongoDB ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ INSERT ------------------------------------------------ num of rows time taken ------------------------------------------------ 10 2.067094ms 100 8.841597ms 1000 106.491732ms 10000 998.225023ms 100000 8.98172825s 1000000 1m 29.63203158s SELECT ------------------------------------------------ num of rows time taken ------------------------------------------------ 1000000 5.251337439s FIND & DISPLAY (with index declared) ------------------------------------------------ num of rows time taken ------------------------------------------------ 1000000 21.540603252s UPDATE ------------------------------------------------ num of rows time taken ------------------------------------------------ 1 1.330954ms ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ________________________________________________ PHP5 with MySQL (engine = MyISAM) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ INSERT ------------------------------------------------ num of rows time taken ------------------------------------------------ 10 0.0040680000000001s 100 0.011595s 1000 0.049718s 10000 0.457164s 100000 4s 1000000 42s SELECT ------------------------------------------------ num of rows time taken ------------------------------------------------ 1000000 <1s SELECT & DISPLAY ------------------------------------------------ num of rows time taken ------------------------------------------------ 1000000 20s ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ________________________________________________ PHP5 with MongoDB ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ INSERT ------------------------------------------------ num of rows time taken ------------------------------------------------ 10 0.065744s 100 0.190966s 1000 0.2163s 10000 1s 100000 8s 1000000 78s FIND ------------------------------------------------ num of rows time taken ------------------------------------------------ 1000000 <1s FIND & DISPLAY ------------------------------------------------ num of rows time taken ------------------------------------------------ 1000000 7s UPDATE ------------------------------------------------ num of rows time taken ------------------------------------------------ 1000000 9s

    #6楼

    Here is a little research that explored RDBMS vs NoSQL using MySQL vs Mongo, the conclusions were inline with @Sean Reilly's response. 这是一项使用MySQL vs Mongo探索RDBMS vs NoSQL 的小研究 ,结论与@Sean Reilly的回应一致。 In short, the benefit comes from the design, not some raw speed difference. 简而言之,好处来自设计,而不是一些原始的速度差异。 Conclusion on page 35-36: 结论在第35-36页:

    RDBMS vs NoSQL: Performance and Scaling Comparison RDBMS与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. 在这种情况下,最重要的因素是所使用的查询类型,因为MongoDB可以更快地处理更复杂的查询,这主要是因为它的简单模式牺牲了数据重复,这意味着NoSQL数据库可能包含大量数据重复。 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. 尽管可以使用直接从RDBMS迁移的模式,但这将消除MongoDB子文档的底层数据表示形式的优点,该优点允许在合并表时对数据库使用较少的查询。 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. 尽管在这些复杂查询中MongoDB在性能上优于MySQL,但当基准测试通过使用嵌套SELECT对MySQL查询建模类似于MongoDB复杂查询时,MySQL的表现最佳,尽管在连接数量较多时,两者的行为类似。 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. 基准测试的最后一种查询类型是包含两个JOINS和一个子查询的复杂查询,这表明MongoDB由于使用子文档而具有优于MySQL的优势。 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. 如果此类查询在应用程序中很常见,则重要的是要考虑使用NoSQL数据库作为替代方法,同时要考虑由于数据库较大而导致的存储和内存大小成本。

    Processed: 0.034, SQL: 8