compound Shard key in MongoDB!!!

254 views
Skip to first unread message

bth...@gmail.com

unread,
Jul 23, 2012, 6:22:39 AM7/23/12
to mongod...@googlegroups.com
Hj, can any body explain for me!!!
i have one example
i have collection with shard key {month :1, user : 1}
First: i have one chunk ((-∞,-∞), (∞,∞)) => all writes and read will be go into this chunk because
minkey_month<month<maxkey_month
minkey_user <user1<maxkey_user
Then: when chunks is too big=> it's slipt into 2 chunks
chunk1: ((-∞, -∞),(“2012-04”, “susan”))
chunk2: [(“2012-04”, “susan”), (∞, ∞))


if i insert document a1:{month:"2012-04", user: "susan"}=>which chunks to choose? why?
if i insert document a1:{month:"2012-04", user: "tusan"}=>which chunks to choose? why?
if i insert document a1:{month:"2012-04", user: "busan"}=>which chunks to choose? why?
if i insert document a1:{month:"2012-05", user: "tusan"}=>which chunks to choose? why?
if i insert document a1:{month:"2012-05", user: "susan"}=>which chunks to choose? why?

can any body give me general syntax to choose chunks with compound shard key such as
with single shard key: for example chunk: (minkey,maxkey)
the chunk will choose when minkey<=keysearch<maxkey

Hope to see reply soon!!!
Thanks so much!!
Phuc

acl

unread,
Jul 23, 2012, 12:16:26 PM7/23/12
to mongod...@googlegroups.com
Hi Phuc,

for the 2012-04 "susan" insert, the data will be inserted into the second chunk
with the "2012-04 susan" key. The "2012-04 susan" key was split so any
new inserts with the same key will be in the new chunk created with
the key.

for the "2012-04 tusan" insert, the data will be inserted into the second chunk
as the chunk has been split at: "2012-04 susan" and "2012-04 tusan" is sorted
after "2012-04 susan".

for the "2012-04 busan" insert, the data will be inserted into the first chunk
as the chunk has been split at: "2012-04 susan" and "2012-04 busan" is sorted
before "2012-04 susan".

for the "2012-05 tusan" and "2012-05 susan" inserts, the data will be
inserted into the second chunk as the entries are after "2012-04
susan".

I'm not clear on the general syntax part of your question. Could you
please clarify? What are you trying to accomplish?

Thanks,
acl

bth...@gmail.com

unread,
Jul 23, 2012, 1:12:20 PM7/23/12
to mongod...@googlegroups.com
thanks so much!!
general syntax is the way which MongoDB use to decide document belong which shard
It's very simple with chunk with single key such as
 chunk1: [minkey1,maxkey1)
chunk2: [maxkey1,maxkey)
the chunk will choose when minkey1<=keysearch<maxkey1
so how compound shard key?
 chunk1: [(minkey1,minkey2),(maxkey1,maxkey2)
chunk2: [((maxkey1,maxkey2),(maxkey3,maxkey3)

document {key1:value1, key2:value2} will be belong
chunk1 when: minkey1<=key1<maxkey1 and (or) minkey2<=key2<maxkey2
chunk2 when: maxkey1<=key1<maxkey3 and (or) maxkey2<=key2<maxkey3

thanks so much!!
Phuc

Vào 23:16:26 UTC+7 Thứ hai, ngày 23 tháng bảy năm 2012, acl đã viết:

acl

unread,
Jul 23, 2012, 2:36:41 PM7/23/12
to mongod...@googlegroups.com
Hi Phuc,

When using a compound shard key, MongoDB treats the compound key as a
single key. So if you have a compound key { key1 , key2 }, MongoDB
will treat the key as: {key1key2}. (i.e. in your example, the { Date,
User } compound key will become { Date-User } key )

So, chunks will be split up looking like this:

chunk1: (minkey1key2, mid-key1key2)
chunk2: (mid-key1key2, maxkey1key2)

So documents will be inserted depending on where the document is
relative to minkey1key2 and maxkey1key2.

Another thread with more info on compound shard keys can be found
here:


Does this help you understand compound shard keys better?

acl
Reply all
Reply to author
Forward
0 new messages