2016-03-15 32 views
0

我有一個簡單的查詢,但它顯示了超時::錯誤:執行到期,也是我使用機架::超時軌查詢超時::錯誤:執行到期

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

難道您用您的查詢'EXPLAIN ANALYZE'並將結果發送? –

+0

謝謝,是的,我已經更新了這個結果的問題 – amtest

回答

1

你有相當大的表上運行SUM。我會建議使用一些緩存機制,以避免使用此查詢,因爲14秒是很多。

例如,我會建議創建新表total_orders_checks並在那裏存儲總計檢查。您需要每次更新更新orderstotal_checks值,它可能不適合你的應用程序設計,但你一定會得到total_checks出來的要快得多。

+0

謝謝馬克,你的建議 – amtest

+0

@amtest好吧,如果你需要只是放大超時,你可以,但我強烈建議不要這樣做。您的應用程序不應該等待DB 14秒,這太過分了 –