Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (47.6k points)

I have been very excited about MongoDb and have been testing it lately. I had a table called posts in MySQL with about 20 million records indexed only on a field called '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. I ran the query about 1,000 times each for MySQL and MongoDB and I am surprised that I do not notice a lot of difference in speed. Maybe MongoDB is 1.1 times faster. That's very disappointing. Is there something I am doing wrong? I know that my tests are not perfect but are MySQL on par with MongoDb when it comes to reading-intensive chores.

 

Note:

  • I have dual core + ( 2 threads ) i7 cpu and 4GB ram

  • I have 20 partitions on MySQL each of 1 million records

Sample Code Used For Testing 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; 

?>

1 Answer

0 votes
by (106k points)

You should be 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>"; 

}

Related questions

0 votes
1 answer
asked Jul 13, 2019 in SQL by Tech4ever (20.3k points)
0 votes
1 answer
+1 vote
2 answers
asked Oct 18, 2019 in Web Technology by Sammy (47.6k points)
0 votes
1 answer

Browse Categories

...