【问题标题】:How to add a large array to an SQL query?如何将大数组添加到 SQL 查询?
【发布时间】:2010-01-29 12:34:15
【问题描述】:

这是我的风暴代码:

items = store.find((Delivery, Product, Sum(Delivery.quantity)),
                    Delivery.id.is_in(order.id for order in self.orders),
                    Product.id == Delivery.product_id) \
                    .group_by(Delivery.date, Product.id, Delivery.discount) \
                    .order_by(Delivery.date, Product.name, Delivery.discount)

这转换为大约以下 SQL:

   SELECT deliveries.id, products.id, SUM(deliveries.quantity)
   FROM deliveries, products
   WHERE products.id = deliveries.id AND
->       deliveries.id IN (1,10,5,24,122, ...)
   GROUP BY deliveries.date, product.id, delivery.discount
   ORDER BY deliveries.date, product.id, delivery.discount

如果self.orders 太长,则会引发Too many SQL variables异常。

有没有其他方法可以实现这一点?

我唯一的想法是为每个订单创建一个查询并使用字典手动组合它们。

【问题讨论】:

    标签: sql storm-orm


    【解决方案1】:
    • 将其添加到临时表/表变量并链接到它。
    • 根据您的 SQL 版本,您可能能够使用 XML 对象

    【讨论】:

      【解决方案2】:

      也许只是将这个目标数组拆分成更小的数组?大约是其大小的 1/3,然后将其合并到脚本端。

      【讨论】:

        猜你喜欢
        • 2016-01-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-12-04
        • 2019-03-03
        相关资源
        最近更新 更多