Sunday, November 14, 2010

MongoDB for a MySQL user

Over the past few years NO SQL has become very popular, specially with a capability to boast about an illustrious reference list like twitter, facebook and foursquare.

Being a MySQL user I was curious to explore into this and see what it has and what it offers. Certainly there are plenty of NO SQL implementations, first wanted to check casandra but the first few lines of its documentation sounded like a no go zone, hence decided to try Mongo DB. Going through it I realized that there were alot of similarities. So listed them here for quick reference.


Criteria
MySQL
MongoDB
Type of database
Relational
Document
Installation
On Linux you need to go through a certain amount of steps like user creation, permission setting, creating the data directory
Pretty stright forward create the the data directory at the default location /data/db. It was pretty simpler than MySQL
Starting Up
starting up using safe_mysqld (techincally it invokes mysqld)
pretty straight forward run mongod for a basic start up
Queries
Using SQL
No SQL

Need to execute create statements
ex. When you wanted to create a table with columns x, y
you need to do the following.
1. Create database mine;
2. create table a(x int, name varchar(50))engine = MyISAM;
3. Then seperately do an insert
insert into a values(10, "hiii");
No create statements for DBs, collections, etc
Updated and created as and when needed(lazy loading), so to do the same we need to,
db.mine.insert({x: 10, y: "hiii"})
similarly you have functions for update, save, remove, find

if you want to select data; we can select * from a;
On MongoDB its,
db.mine.find()
Storage Engines
A lot of Storage Engines to choose from
Upto now a single storage engine implementation
Operations/ Transactions
When you fire a query to the MySQL server the client needs to wait till the MySQL server returns the result.
In contrast Mongo DB uses 'Fire and forget'. So technically speaking client is not aware whether the query executed successfully or not. It doesn’t return OK.
For system where you need safe operations immediately following the query getLastError command is issued and the exception handled.
For Requirements like analytics , status message updates, pics comments this fire and forget is perfect. Anyhow for Financial type of system we need to use the work around.

storage engine like INNODB supprts transactions and has ACID compliance
Not typically made for transactional systems
Indexing
Supports mutiple types of indexes. For optimzation EXPLAIN is used with the select statements
Very similar to MySQL indexing.
For optimzation similar to MySQL this has the EXPLAIN statement and another statement called as hint
Main difference is that you can define the order of indexing for composite indexes. Which gives more control to the query definition Eg. db.mine.ensureIndex({"x" : 1}) 1 indicates the direction
Geo Spatial Indexing
MyISAM storage engine supports geo spatial indexing.
Is created by passing "2d" instead of passing 1 or -1 to the ensureIndex  function.
Pretty handy stuff with  straight forward support for querying nerarest locations, find entries within a shape and find distances.
Compound geo spatial indexing also supported, this will be handy for finding nearest ATM etc. I think this is a must check for people developing location based systems
Back Up
MySQL back up is more complex, specific to the storage engines.
Snapshot based back ups on LVM2 volumes is similar to the fsync command in mongo
supports fsync command which is a warm back up with point of time support.
Supports mongodump - which does a hot back up but without point of time support
Replication


Mode of Replication
asynchorouns
asynchronous

Replication is based on Binary log - which has all the writes
Replication is based on OpLog, similar to the binary log but has idompotent operations.
Which basically means if the same statement is executed mutiple times it will not make the date inconsistant .
Anyhow the statements shoule be executed in order

set up is straight forward after having a master and slave in place
quiet similar - at start up itself the server can be specified as slave in the start up line,
./mongod --dbpath ~/data/slave --port 10001 --slave --source localhost:10000

supports complex replication set ups, like two way replication, circular replication, etc
does not support replication from the slave
Replica Set
MySQL cluster supports Replica sets- anyhow MySQL cluster replica sets has synchrous replication
Similarly for HA support MondoDB supports replica sets - which is a master slave cluster with automatic failover.
Similar to MySQL cluster in the replica sets. There are arbiter nodes which decide on the primary and secondary node election in case of failures
Sharding
MySQL doesn’t have inbuilt sharding support. Sharding is usually handled at application or ORM level for MySQL
sharding support is given - mongos is a process that will interface multiple mongods which will have the data distributed.
Very good feature for scalability


For those who want to try out mongodb check this book, http://oreilly.com/catalog/0636920001096

Next up I want to try Couch DB, which is another No SQL database and implemented on Erlang!

3 comments:

Anonymous said...

Nice write up.
A small correction!
MongoDB supports fire and forget, which is the default mechanism for any writes, but it is not the only implementation; you have safe and replica safe too.

Anonymous said...

[url=http://securerucasino.ru][img]http://securerucasino.ru/ia.php?q=1[/img][img]http://securerucasino.ru/ia.php?q=2[/img][img]http://securerucasino.ru/ia.php?q=3[/img][/url]



http://bestplaceopa0194.hostingsiteforfree.com/sun-light-casino-igrovye-avtomaty.html рулетка онлайн скайп Бесплатный игровой автомат иллюзионист самые лутшие русские онлайн казино с момнтальным выводом выигранных средств [url=http://bestplaceopa0194.hostingsiteforfree.com/igrovye-avtomati-eshki.html]Игровые автомати ешки[/url] http://bestplacehjx7606.boxhost.me/virtualnoe-kazino-bestforplaynet.html Казино с бездепозитным бонусом при регистрации Казино онлайн бесплатно играть gaminator Вулкан северный черпук камчатка видео [url=http://bestplaceopa0194.hostingsiteforfree.com/rabota-na-nemeckih-laynerah-v-kazino.html]работа на немецких лайнерах в казино[/url] http://bestplacekva1142.hostingsiteforfree.com/kak-vzlomat-kazino-v-samprp.html Онлфйн игровые автоматы бес платно играть Игровые автоматы вулкан играть бесплатно онлайн смотреть короли рулетки онлайн [url=http://bestplaceopa0194.hostingsiteforfree.com/lazernye-ruletki-bosch.html]Лазерные рулетки bosch[/url] http://bestplaceopa0194.hostingsiteforfree.com/vulkan-udachi-klubnichka.html Знакомства через веб камеру рулетка скачать программу игровых автоматов бесплатно В каком казино играть больше-меньше [url=http://bestplacehjx7606.boxhost.me/cumki-kupit-v-kazini.html]Cумки купить в казини[/url]
[url=http://www.nationaledinnovationsummit.com/forums/index.php/topic,176690.new.html#new] Игровые автоматы безплатно онлайн[/url]
http://bestplaceopa0194.hostingsiteforfree.com/forumy-o-zarobotke-v-kazino.html [url=http://bestplacehjx7606.boxhost.me/piramida-igrovie-avtomati.html]Пирамида игровие автомати[/url] Беляков евгений казино

http:/www.liveinternet.ru/users/rhymelictia1980d8c2/post264886020/
http:/www.liveinternet.ru/users/rhymelictia1980d8c2/post264894563/
http:/www.liveinternet.ru/users/unjjelepsi1977kcfc/post264935411/
http:/www.liveinternet.ru/users/etintrapget1986i6n64/post264919912/
http:/www.liveinternet.ru/users/trafeclitet1986286k5/post264938549/

[url=http:/www.liveinternet.ru/users/mahenruiholz1985k5ctm/post264625206/]Крепление на оптику к карабину винчестер вулкан 30-06[/url][url=http:/www.liveinternet.ru/users/unjjelepsi1977kcfc/post264942047/]Играть бесплатно в игровые автоматы онлайн[/url]
[url=http:/www.liveinternet.ru/users/unjjelepsi1977kcfc/post264882131/]форум игроков в казино[/url]

Anonymous said...

http://twitter.com/Dvikw4YNLV Игровые автоматы играть бесплатно без регистрации скачать [url=http://twitter.com/Pmo6ujMLE1] Бигазарт казино [/url] Как выиграть в реальную рулетку скачать на андроид игры с казино mamoleptino321 http://twitter.com/jurij_po_557094 Выиграть у интернет-казино [url=http://twitter.com/anton_ku_116057] Вулкан ракета [/url] Игровые автоматы бесплатно играть capitan trorsure Зал детских игровых автоматов открыть
http://twitter.com/hmWUfNbA9w Игра авто вулкан [url=http://twitter.com/nikita_za_47424] Игровой автомат вулкан [/url] Интим чат рулетка Elencasino.com играть бесплатно в игровые автоматы


[url=http://ravta.ru/]хорошее казино где реально заработать на рулетке[/url]
http://gauragorsk.ru/index.php/klad/knigi/item/%D0%B1%D1%85%D1%83%D0%B2%D0%B0%D0%BD%D0%B5%D1%88%D0%B2%D0%B0%D1%80#comment-222
http://www.youz.tv/index.php/drag-racing-zakrytie-sezona-kubka-ukrainy/comment-page-1/#comment-5227