目前是否可以在 SQLAlchemy 中的 ORM 查询中的某些表上TABLESAMPLE
?
我能找到的最接近的是 Query.suffix_with(*suffixes)
,但这会将后缀放在查询的末尾,而不是放在表的末尾。
如果有帮助,这是我的用例:
目前我的查询是:
query = session.query(A).options(
subqueryload(A.rel1),
subqueryload(A.rel2)
).filter(A.id >= min_id, A.id < max_id, [...])
我想在表 A
上TABLESAMPLE
,这样我就可以有效地只加载 A
的一个子集及其对应的关系,带有一些过滤器。我使用 PostgreSQL 9.5 作为我的数据库。
最佳答案
更新
SQLAlchemy 1.1 and newer support TABLESAMPLE
对于可选择项。 FromClause
元素有一个方法 FromClause.tablesample()
(想想 Table
)和 sqlalchemy.tablesample()
函数可以与所有可选对象一起使用,例如声明性模型类:
from sqlalchemy import tablesample, func
from sqlalchemy.orm import aliased
# Create an alias for A that uses SYSTEM sampling method (default)
a_sampled = aliased(A, tablesample(A, 2.5))
# Create an alias for A that uses BERNOULLI sampling method
a_bernoulli = aliased(A, tablesample(A, func.bernoulli(2.5)))
在 tablesample(A, ...)
中有一点不对称,其中 A
是一个声明类,返回一个 TableSample
-clause,如果用作模型,则必须使用别名。否则它就像一个 FromClause
,例如Table
是模型类的基础。
之前
好像有no discussion关于TABLESAMPLE
撰写本文时的支持。由于 SQLAlchemy 非常 extensible ,这是一个非常简单的(阅读:可能不适用于所有用例)实现的 tablesample 支持:
from sqlalchemy.ext.compiler import compiles
from sqlalchemy.sql import Alias, FromClause
from sqlalchemy.orm import aliased
class TableSample(Alias):
__visit_name__ = 'tablesample'
def __init__(self, selectable, argument=None, method=None,
seed=None):
super(TableSample, self).__init__(selectable)
self.method = method
self.argument = argument
self.seed = seed
def tablesample(element, argument=None, method=None, seed=None):
if isinstance(element, FromClause):
return TableSample(element, argument=argument, method=method, seed=seed)
else:
return aliased(element, TableSample(element.__table__,
argument=argument, method=method, seed=seed))
@compiles(TableSample)
def compile_tablesample(element, compiler, **kwargs):
s = "%s TABLESAMPLE %s(%s)" % (
compiler.visit_alias(element, **kwargs),
element.method or 'SYSTEM',
element.argument)
if element.seed:
s += " REPEATABLE (%s)" % compiler.process(element.seed, **kwargs)
return s
为方便起见,参数
暂时应该只是一个表示 0 到 100 之间的百分比的 float ,尽管 PostgreSQL 会接受任何实值表达式。另一方面,seed
是经过编译的,因此必须用 literal()
包裹文字 python 值。等等。
它的使用方式应类似于 aliased
:
a_sampled = tablesample(A, 2.5)
query = session.query(a_sampled).options(
subqueryload(a_sampled.rel1),
subqueryload(a_sampled.rel2)
).filter(a_sampled.id >= min_id, a_sampled.id < max_id, [...])
关于python - 如何使用 SQLAlchemy 进行 TABLESAMPLE?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37429476/