我有一个简单的查询,但是它显示了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)
最佳答案
你有很大的桌子要放。我建议使用一些缓存机制来避免使用这个查询,因为14秒是很多的。
例如,我建议创建新的表SUM
并在其中存储总检查数。你需要在每次更新total_orders_checks
tableorders
值时更新它,它可能不适合你的应用程序设计,但你肯定会更快地从中获得total_checks
值。