[英]How can I make this query against a table with over 3 million records faster than 15+ seconds?
我有以下查詢:
EXPLAIN ANALYZE
SELECT
customer_id
FROM
orders
WHERE
"status" IN ( 'authorized', 'paid', 'partially_paid', 'pending')
GROUP BY
customer_id
HAVING
COUNT(customer_id) >= 2
這會產生以下查詢計划:
Finalize GroupAggregate (cost=440054.50..516225.43 rows=252557 width=33) (actual time=12206.961..17389.057 rows=457301 loops=1)
Group Key: customer_id
Filter: (count(customer_id) >= 2)
Rows Removed by Filter: 592730
-> Gather Merge (cost=440054.50..511174.29 rows=505114 width=41) (actual time=12206.945..16674.249 rows=1615901 loops=1)
Workers Planned: 2
Workers Launched: 2
-> Partial GroupAggregate (cost=439054.47..451871.57 rows=252557 width=41) (actual time=12101.661..14862.466 rows=538634 loops=3)
Group Key: customer_id
-> Sort (cost=439054.47..442484.98 rows=1372204 width=33) (actual time=12101.648..14344.507 rows=1097122 loops=3)
Sort Key: customer_id
Sort Method: external merge Disk: 45448kB
-> Parallel Seq Scan on orders (cost=0.00..224124.56 rows=1372204 width=33) (actual time=0.014..1205.188 rows=1097122 loops=3)
Filter: ((status)::text = ANY ('{authorized,paid,partially_paid,pending}'::text[]))
Rows Removed by Filter: 24092
Planning time: 0.101 ms
Execution time: 17434.175 ms
該表本身有超過 300 萬條記錄。
在一天結束的時候,我試圖找到所有已經下過 2 個或更多訂單的客戶,並試圖讓這個查詢快速響應,最好在幾秒鍾內。
我嘗試了幾種方法,但似乎無法讓執行時間變快。
關於如何改進這一點的任何想法?
即使您正在執行順序掃描,掃描實際上也非常快( actual time=0.014..1205.188
毫秒)。 你真正受到打擊的是那種,它需要actual time=12101.648..14344.507
。 您使用Sort Method: external merge Disk: 45448kB
嘗試將work_mem
增加到高於 48MB 的值,看看是否有幫助。
我想知道過濾索引是否會加快查詢速度:
create index idf_orders_customer_id_status on (customer_id)
where "status" IN ( 'authorized', 'paid', 'partially_paid', 'pending') ;
SELECT customer_id
FROM orders
WHERE "status" IN ( 'authorized', 'paid', 'partially_paid', 'pending')
GROUP BY customer_id
HAVING COUNT(*) >= 2;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.