Ruby on rails rails查询超时::错误:执行已过期
我有一个简单的查询,但它显示了超时::错误:执行已过期,而且我使用的是rack::TimeoutRuby on rails rails查询超时::错误:执行已过期,ruby-on-rails,ruby,ruby-on-rails-3,postgresql,rack,Ruby On Rails,Ruby,Ruby On Rails 3,Postgresql,Rack,我有一个简单的查询,但它显示了超时::错误:执行已过期,而且我使用的是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
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)
----------
限制(成本=153.76..153.77行=1宽度=5)(实际时间=14622.323..14622.324
行=1(循环=1)
->骨料(成本=153.76..153.77行=1宽=5)(实际
时间=14622.322..14622.322行=1循环=1)
->使用idx命令、检查命令和传递命令进行索引扫描
(成本=0.43..153.76行=1宽=5)(实际时间=2739.717..14621.649行=141
循环=1)
索引条件:((检查id=36)和(通过id=49))
筛选器:((订单状态id ALL({15,17})::整数[])和
(创建时间>='2016-02-29 22:00:00'::无时区的时间戳)和
(在创建时,您有相当大的表要运行SUM
。我建议使用一些缓存机制来避免使用此查询,因为14秒太多了
例如,我建议创建新的表total\u orders\u checks
并将总检查数存储在那里。每次更新orders
tabletotal\u checks
值时,您都需要更新它,它可能不适合您的应用程序设计,但您肯定会更快地从中获得total\u checks
。您可以吗使用EXPLAIN ANALYZE
运行查询并发送结果?谢谢,是的,我已经用它更新了问题result@amtest好吧,如果你需要扩大超时时间,你可以,但我强烈建议不要这样做。你的应用程序不应该等待数据库14秒,那太多了