我遇到了一个问题,SELECT
查询很慢,因为当最终结果的数量为 0并且LIMIT
指定了一个子句时,它不使用索引。
如果结果数大于 0,则 Postgres 使用索引并在 ~ 1ms 内返回结果。据我所知,这似乎总是正确的。
如果结果数为 0 且没有LIMIT
使用,则 Postgres 使用索引,结果在 ~ 1ms 内返回
如果结果数为 0 并且LIMIT
指定了 a,则 Postgres 会执行顺序扫描,结果大约需要 13,000 毫秒。
为什么 PostgreSQL 在最后一种情况下不使用索引?
基数:
总计约 2100 万行。
~ 300 万行WHERE related_id=1
~ 300 万行WHERE related_id=1 AND platform=p1
2 行WHERE related_id=1 AND platform=p2
0 行WHERE related_id=1 AND platform=p3
~ 800 万行WHERE platform=p2
Postgres 版本:9.4.6
表架构:
CREATE TYPE platforms AS ENUM ('p1', …
Run Code Online (Sandbox Code Playgroud) postgresql performance index statistics postgresql-9.4 postgresql-performance