我有一个简单的查询,但它显示了Timeout::Error: execution expired,而且我正在使用 rack::timeout
SELECT SUM(total_checks) as totalcheck FROM "orders" WHERE
(orders.order_status_id NOT IN (15, 17)) AND (orders.check_id = 36) AND
(orders.pass_id = '49') AND (orders.created_at BETWEEN '2016-02-29
22:00:00.000000' AND '2016-03-02 22:00:00.000000') LIMIT 1
此外,我的订单总数约为 9762797,此查询有任何问题吗?
什么时候搞定的解释分析
----------
Limit (cost=153.76..153.77 rows=1 width=5) (actual time=14622.323..14622.324
rows=1 loops=1)
-> Aggregate (cost=153.76..153.77 rows=1 width=5) (actual
time=14622.322..14622.322 rows=1 loops=1)
-> Index Scan using idx_orders_check_and_pass on orders
(cost=0.43..153.76 rows=1 width=5) (actual time=2739.717..14621.649 rows=141
loops=1)
Index Cond: ((check_id = 36) AND (pass_id = 49))
Filter: ((order_status_id <> ALL ('{15,17}'::integer[])) AND
(created_at >= '2016-02-29 22:00:00'::timestamp without time zone) AND
(created_at <= '2016-03-02 22:00:00'::timestamp without time zone))
Rows Removed by Filter: 42396
Total runtime: 14622.524 ms
(7 rows)
最佳答案
您有相当大的表来运行 SUM
。我建议使用一些缓存机制来避免使用此查询,因为 14 秒很多。
例如,我建议创建新表 total_orders_checks
并在其中存储总支票。每次更新 orders
表 total_checks
值时都需要更新它,它可能不适合您的应用程序设计,但您肯定会得到 total_checks
快得多。
关于ruby-on-rails - rails 查询超时::错误:执行已过期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36006578/