开发者

Emulating join behavior with Rails and Mongoid

开发者 https://www.devze.com 2023-04-05 22:54 出处:网络
Just wanted to ask some advice when building a database with mongodb, I have been reading a lot that if you have a database with a lot of joins it\'s better to go with say postgresql.

Just wanted to ask some advice when building a database with mongodb, I have been reading a lot that if you have a database with a lot of joins it's better to go with say postgresql.

So if I wanted flexibility and needed my data to join multiple times, should I go with Postgresql? I know mongodb has fast reads / writes but needs to query multiple times to emulate joins. So when would this become a performance hit? Does mongodb limit your ability to create new complex relationships on开发者_如何学Python your data that did not previously exist?

I guess the attractiveness of mongodb is its javascript syntax and similarity to json :)


I will start from the end:

I guess the attractiveness of mongodb is its javascript syntax and similarity to json :)

Not only this, and json style is not main advantage. Main advantages of mongodb is ability to embedd documents, high performance and full scalability, full index support, map/reduce, etc.

So if I wanted flexibility and needed my data to join multiple times, should I go with Postgresql?

It depends on concrete task, for example if you designing report system i prefer to use some relational database. But sometimes instead of joins and separate collections you can embedd documents + mongodb good fit for the data denormalization ( and in many situations you can denormalize in background to avoid joins )

I know mongodb has fast reads / writes but needs to query multiple times to emulate joins. So when would this become a performance hit?

If you will use mongodb as regular relational database (without embedding and denormaliztion) you never achieve best performance.

Does mongodb limit your ability to create new complex relationships on your data that did not previously exist?

No mongodb not limit you, because of it does not contains any constraints between collections like foreign key in any sql database + it allow embedd and easy denormalize data to fit your business needs and achieve best performance.


Another alternative would be to denormalize your data.

You store copies of data in multiple tables/collections. In doing so, you avoid the need for JOINs and lookups needed to stitch together related pieces of data.

You avoid joins and you’re storing more data - but your overall application can be faster.

In mongoid there are two great gems to make this easier:

Mongoid_alize & Mongoid_denomalize

http://blog.joshdzielak.com/blog/2012/05/03/releasing-mongoid-alize-comprehensive-field-denormalization-for-mongoid/


You can always use: http://www.mongodb.org/display/DOCS/MapReduce

Or http://www.mongodb.org/display/DOCS/Aggregation#Aggregation-Group

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号