Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 大约175k个整数。但是我怀疑如果这个列表需要显著增加,那么就有必要使用temp表。我不打算对此进行否决,但您绝对不需要使用binds。事实上,这个问题的全部推动力是SQLAlchemy在构建表达式中的时自动生成要绑定的参数,并且一旦列表大小增加到1000_Python_Sqlalchemy - Fatal编程技术网

Python 大约175k个整数。但是我怀疑如果这个列表需要显著增加,那么就有必要使用temp表。我不打算对此进行否决,但您绝对不需要使用binds。事实上,这个问题的全部推动力是SQLAlchemy在构建表达式中的时自动生成要绑定的参数,并且一旦列表大小增加到1000

Python 大约175k个整数。但是我怀疑如果这个列表需要显著增加,那么就有必要使用temp表。我不打算对此进行否决,但您绝对不需要使用binds。事实上,这个问题的全部推动力是SQLAlchemy在构建表达式中的时自动生成要绑定的参数,并且一旦列表大小增加到1000,python,sqlalchemy,Python,Sqlalchemy,大约175k个整数。但是我怀疑如果这个列表需要显著增加,那么就有必要使用temp表。我不打算对此进行否决,但您绝对不需要使用binds。事实上,这个问题的全部推动力是SQLAlchemy在构建表达式中的时自动生成要绑定的参数,并且一旦列表大小增加到1000个,构建字符串参数所花费的时间就开始变得非常重要。一切都很重要。@wst在谈论性能时,带上数字。使用mogrify(请参阅,我得到了a)解析查询+绑定的平均时间,然后b)mogrify.execute。对于10K项目列表:0.3秒。10万个项目


大约175k个整数。但是我怀疑如果这个列表需要显著增加,那么就有必要使用temp表。我不打算对此进行否决,但您绝对不需要使用binds。事实上,这个问题的全部推动力是SQLAlchemy在构建表达式中的
时自动生成要绑定的参数,并且一旦列表大小增加到1000个,构建字符串参数所花费的时间就开始变得非常重要。一切都很重要。@wst在谈论性能时,带上数字。使用mogrify(请参阅,我得到了a)解析查询+绑定的平均时间,然后b)mogrify.execute。对于10K项目列表:0.3秒。10万个项目列表上的时间为3.5秒。这与实际的数据库执行不同,但这一点仍然存在。不知道这与你的数字相比如何,我就不说了。硬件:MBP 2011编号与问题范围无关。问题是如何扩展SQLAlchemy的一部分。我故意避免谈论性能,因为我已经知道如何解决性能问题。这是一个奇怪的说法,因为你一直在和我谈论性能现实。无论什么
from sqlalchemy.types import TypeEngine

class in_list_(TypeEngine.Comparator):
  pass

@compiles(in_list_)
def in_list_impl(element, compiler, **kwargs):
  return "IN ('Now', 'I', 'can', 'inline', 'the', 'list')"

select([mytable.c.x, mytable.c.y]).where(mytable.c.x.in_list_(long_list))
from sqlalchemy.ext.compiler import compiles
from sqlalchemy.sql.elements import BinaryExpression
from sqlalchemy.sql.operators import in_op, notin_op

def visit_in_op_binary(compiler, binary, operator, **kw):
    return "%s IN %s" % (
        compiler.process(binary.left, **kw),
        compiler.process(binary.right, **{**kw, "literal_binds": True}))

def visit_notin_op_binary(compiler, binary, operator, **kw):
    return "%s NOT IN %s" % (
        compiler.process(binary.left, **kw),
        compiler.process(binary.right, **{**kw, "literal_binds": True}))

@compiles(BinaryExpression)
def compile_binary(binary, compiler, override_operator=None, **kw):
    operator = override_operator or binary.operator

    if operator is in_op:
        return visit_in_op_binary(
            compiler, binary, operator, override_operator=override_operator,
            **kw)

    if operator is notin_op:
        return visit_notin_op_binary(
            compiler, binary, operator, override_operator=override_operator,
            **kw)

    return compiler.visit_binary(binary, override_operator=override_operator, **kw)
In [15]: %%time
    ...: session.query(Foo).\
    ...:     filter(Foo.data.in_(range(250000))).\
    ...:     all()
    ...: 
CPU times: user 5.09 s, sys: 91.9 ms, total: 5.18 s
Wall time: 5.18 s
Out[15]: []

In [16]: %%time
    ...: session.query(Foo).\
    ...:     filter(Foo.data.in_(bindparam('xs', range(250000), expanding=True))).\
    ...:     all()
    ...: 
CPU times: user 310 ms, sys: 8.05 ms, total: 318 ms
Wall time: 317 ms
Out[16]: []
In [4]: session.query(Foo).\
   ...:     filter(Foo.data.in_(
   ...:         bindparam('xs', range(10), expanding=True, literal_execute=True))).\
   ...:     all()
2019-09-07 20:35:04,560 INFO sqlalchemy.engine.base.Engine BEGIN (implicit)
2019-09-07 20:35:04,561 INFO sqlalchemy.engine.base.Engine SELECT foo.id AS foo_id, foo.data AS foo_data 
FROM foo 
WHERE foo.data IN (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
2019-09-07 20:35:04,561 INFO sqlalchemy.engine.base.Engine ()
Out[4]: []