Saturday, July 24, 2010

Re: How to use the Database Router?

On Sat, Jul 24, 2010 at 1:40 PM, Andy <selforganized@gmail.com> wrote:
>
>> Ah - this is possibly the source of confusion. Django's Routers can
>> shard to different databases, but not to different tables in the same
>> database.
>>
>> Why do you want to shard into different *tables*? The usual
>> interpretation of sharding (at least, the usual interpretation that
>> *I'm* familiar with) is to shard into different *databases*. I'm not
>> sure I see what you gain by using different tables in the same
>> database.
>>
>
> I probably didn't make it clear.
>
> I used the term "databases/tables" because (to me) if those tables
> lives in different databases then they are different tables.
>
> Anyway let me rephrase.
>
> I have a model "Tweet". I want to shard it into N databases based on
> the modulo N of the Tweet's author's id. So each database would have a
> table "Tweet" with the exact same structure.
>
> How do I do that with the Database Router?

The db_for_read() and db_for_write() methods accept a model, and
usually a hint. If provided, the hint will be the instance the
database operation is acting on (or an instance related to the
operation). These methods return the alias of the database that should
be used to perform the read/write operation.

To shard based on model properties, you inspect the hint object, and
use the details of that object to determine which database alias
should be used. The db_for_read() and db_for_write() methods will have
the same implementation, returning an alias that is computed based on
the modulo of the object 'owner', or whatever sharding behavior your
want to implement.

Yours,
Russ Magee %-)

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment