我有一个包含近 100 万行的非常大的表,并且一些查询花费了很长的时间(超过一分钟)。
这是一个让我特别难受的...
EXPLAIN ANALYZE SELECT "apps".* FROM "apps" WHERE "apps"."kind" = 'software' ORDER BY itunes_release_date DESC, rating_count DESC LIMIT 12;
QUERY PLAN
---------------------------------------------------------------------------------------------------------------------------------
Limit (cost=153823.03..153823.03 rows=12 width=2091) (actual time=162681.166..162681.194 rows=12 loops=1)
-> Sort (cost=153823.03..154234.66 rows=823260 width=2091) (actual time=162681.159..162681.169 rows=12 loops=1)
Sort Key: itunes_release_date, rating_count
Sort Method: top-N heapsort Memory: 48kB
-> Seq Scan on apps (cost=0.00..150048.41 rows=823260 width=2091) (actual time=0.718..161561.149 rows=808554 loops=1)
Filter: (kind = 'software'::text)
Total runtime: 162682.143 ms
(7 rows)
那么,我该如何优化呢? PG 版本是 9.2.4,FWIW。
kind
和kind, itunes_release_date
已经有了索引。
最佳答案
看起来您缺少索引,例如关于 (kind,itunes_release_date desc,rating_count desc)
。
关于sql - 我如何在 Postgres 中优化这个 SQL 查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16898778/