版本:1.1.0b2 |发布日期:2016年7月1日

SQLAlchemy 1.1文档

水平分片

水平分片支持。

定义了一个基本的“水平分割”系统,它允许会话跨多个数据库分发查询和持久性操作。

有关使用示例,请参阅源分布中包含的Horizontal Sharding示例。

API文档

class sqlalchemy.ext.horizontal_shard.ShardedSession(shard_chooser, id_chooser, query_chooser, shards=None, query_cls=<class 'sqlalchemy.ext.horizontal_shard.ShardedQuery'>, **kwargs)

基础:sqlalchemy.orm.session.Session

__init__(shard_chooser, id_chooser, query_chooser, shards=None, query_cls=<class 'sqlalchemy.ext.horizontal_shard.ShardedQuery'>, **kwargs)

构建ShardedSession。

参数:
  • shard_chooser – A callable which, passed a Mapper, a mapped instance, and possibly a SQL clause, returns a shard ID. 该ID可以基于对象内存在的属性或某种循环方案。如果该方案基于选择,则应设置实例上的任何状态以将其标记为参与该分片。
  • id_chooser – A callable, passed a query and a tuple of identity values, which should return a list of shard ids where the ID might reside. 数据库将按照此列表的顺序进行查询。
  • query_chooser – For a given Query, returns the list of shard_ids where the query should be issued. 所有返回的碎片结果将合并成一个列表。
  • shards – A dictionary of string shard names to Engine objects.
class sqlalchemy.ext.horizontal_shard.ShardedQuery(*args, **kwargs)

基础:sqlalchemy.orm.query.Query

set_shard T0> ( T1> shard_id T2> ) T3> ¶ T4>

返回一个新的查询,限于单个分片ID。

无论其他状态如何,返回查询的所有后续操作都将针对单个分片。