我在PGPostgres的缓慢限制查询
EXPLAIN ANALYZE
select * from email_events
where act_owner_id = 500
order by date desc
limit 500
这个简单的查询第一个查询的执行需要很长的时间约7秒钟。
"Limit (cost=0.43..8792.83 rows=500 width=2311) (actual time=3.064..7282.497 rows=500 loops=1)"
" -> Index Scan Backward using email_events_idx_date on email_events (cost=0.43..233667.36 rows=13288 width=2311) (actual time=3.059..7282.094 rows=500 loops=1)"
" Filter: (act_owner_id = 500)"
" Rows Removed by Filter: 1053020"
"Total runtime: 7282.818 ms"
第一次执行后,我猜的查询被缓存,并在20-30毫秒。
为什么LIMIT在没有缓存时很慢?我怎样才能解决这个问题?
该表有2.5 mil行 –
您有关于(act_owner_id,date)的索引吗? – DRapp
是的,即使按照act_owner_id排序,结果也是一样的 –