Nov
15
2009

Click to view the full size
I haven’t used PostgreSQL and TokyoTyrant so, can’t say much about them. And technically, I really don’t think that one should compare MySQL which is relational database with document based non-relational databases like: CouchDB and MongoDB.
In my opinion, MongoDB out-performs CouchDB in terms of querying, insertion and ease of usage but CouchDB’s support for MVCC and transaction are quite interesting. One of the crons of MongoDB is it’s data size grow at freaking high rate.
Thoough great to see that, NOSQL (NOt Only SQL) is on full swing.
Download OpenSQL comparison PDF (Don’t forget to read the conclusion though) via HackerNews.
Nov
15
2009
Currently, I’ve been doing some stuffs using MongoDB. If you don’t know or haven’t use it, it’s a document based key-value database systems, that means it’s fundamentally different from traditional DBMS like MySQL, Oracle.
Systems like MongoDB along with similar technologies like CouchDB make significant use of MapReduce. MapReduce is basically a two step process consisting of Map and Reduce where Map is used for reducing a dataset to smaller sub-sets while Reduce is used for for some specific operations into that mapped or grouped data. You can find more information about it all over the web.
Since, PHP driver MongoDB does not provide any specific MapReduce API, I’ve created mine own using MongoDB::command. You can find it @ Github.
Simple Usage:
<?
$db_name = "test_dbs";
$mongodb = new MongoDB(new Mongo(), $db_name);
$map = <<<MAP
function() {
this.tags.forEach(
function(x) {
emit(x, 1);
}
);
}
MAP;
$reduce = <<<REDUCE
function(key, values) {
return {count: values.length };
}
REDUCE;
$map_reduce = new MongoMapReduce($map, $reduce);
$collection_name = "animal_tagsaa";
$response = $map_reduce->invoke($mongodb, $collection_name);
print_r($response->getRawResponse());
if ($response->valid()) {
echo "Total Execution Time: {$response->getTotalExecutionTime()} Milli Seconds\n";
$count_data = $response->getCountsData();
echo "Count Data\n";
foreach ($count_data as $key=>$value) {
echo "{$key}: {$value}\n";
}
echo "********************\n";
foreach ($response->getResultSet() as $tag) {
echo "{$tag["_id"]}\n";
echo "Count: {$tag["value"]["count"]}\n";
echo "****************\n";
}
}
Usage with Mongo Collections
<?php
function __autoload($class_name) {
require_once "../lib/".$class_name . '.php';
}
$db_name = "test_dbs";
$mongodb = new MongoDB(new Mongo(), $db_name);
class AnimalTag extends XMongoCollection {
const COLLECTION_NAME = "animal_tags";
public function __construct(MongoDB $mongoDB) {
$this->collectionName = self::COLLECTION_NAME;
parent::__construct($mongoDB, $this->collectionName);
}
}
$animal_tags = new AnimalTag($mongodb);
$map = <<<MAP
function() {
this.tags.forEach(
function(x) {
emit(x, 1);
}
);
}
MAP;
$reduce = <<<REDUCE
function(key, values) {
return {count: values.length };
}
REDUCE;
$response = $animal_tags->mapReduce(new MongoMapReduce($map, $reduce));
if ($response->valid()) {
foreach ($response->getResultSet() as $tag) {
echo "{$tag["_id"]}\n";
echo "Count: {$tag["value"]["count"]}\n";
echo "****************\n";
}
}
Enjoy!!!