我有一个简单的查询,但它显示超时::错误:执行已过期,我也在使用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)
答案 0 :(得分:1)
你有一个很大的表来运行SUM
。我建议使用一些缓存机制来避免使用此查询,因为14秒很多。
例如,我建议创建新表total_orders_checks
并在那里存储全部检查。每次更新orders
表total_checks
值时都需要更新它,它可能不适合您的应用设计,但您肯定会更快地获得total_checks
。