简体   繁体   中英

mysql_query equivalent to mongoDB

I'm trying to write a php benchmark in order to compare some RDBMS, NewSQL and NoSQL. This script just execute queries and measure the execution time.

For mysql-like, I simply use:

$start = microtime(true);
$result = mysql_query($SQL);
$end = microtime(true);

I did not fetch data for my benchmark.

But with mongodb-php, function find() return a cursor

$start = microtime(true);
$collection = $this->_db->selectCollection($collection);
$cursor = $collection->find($query);
$end = microtime(true);

Does $cursor and $result are equivalent (time/data cost)? Cursor does not load data, I have to iterate cursor in order to load data.. That why time to execute query between MySQL and mongoDB are so different or just mongoDB rocks ...

I wondering if it would be fairer to change my code to:

$start = microtime(true);
$result = mysql_query($SQL);
while ($row = mysql_fetch_row($result)) {}
$end = microtime(true);

and

$start = microtime(true);
$cursor = $collection->find($query);
foreach ($cursor as $doc) {}
$end = microtime(true);

And finally, is it true to say that each time you iterate on mongodb cursor data are fetch directly from mongodb server and not from computer memory?

Running ->find() indeed just returns the cursor. The query doesn't executed til you fetch the first result. At that moment, the driver will issue the query. This allows you to modify the cursor with things like ->limit() and ->skip(). So yes, it'd be fairer to iterate over the result set as well.

And yes, iterating over the mongoDb cursor, the data is fetch from the MongoDB server. There are no unbuffered queries such as with MySQL (which allows both). Of course, MongoDB will have this data in memory as well-especially if you do it two times just after each other.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM