Postgres ANTI-JOIN 需要表扫描吗?
我需要在同一个表上使用 ANTI-JOIN (不存在从表中选择某些内容.../左连接表 WHERE table.id IS NULL)。实际上,我有一个索引来解决不存在的问题,但查询规划器选择使用位图堆扫描。
该表有 1 亿行,因此进行堆扫描会很混乱......
如果 Postgres 可以与索引进行比较,那将会非常快。 Postgres 是否必须访问此 ANTI-JOIN 的表?
我知道必须在某个时候访问该表才能为 MVCC 提供服务,但为什么这么早呢? NOT EXISTS 只能由表来修复吗,因为否则它可能会丢失一些东西?
I need an ANTI-JOIN (not exists SELECT something from table.../ left join table WHERE table.id IS NULL) on the same table. Acutally I have an index to serve the not exists question, but the query planner chooses to use a bitmap heap scan.
The table has 100 Million rows, so doing a heap scan is messed up...
It would be really fast if Postgres could compare to the indicies. Does Postgres have to visit the table for this ANTI-JOIN?
I know the table has to be visited at some point to serve the MVCC, but why so early? Can NOT EXISTS only be fixed by the table, because it could miss something otherwise?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要提供版本详细信息,正如 jmz 所说的 EXPLAIN ANALYZE 输出以获得任何有用的建议。
弗兰兹 - 不要想是否可能,测试一下就知道了。
这是 v9.0:
您看到的内容将取决于您的版本以及规划器看到的统计信息。
You'll need to provide version details, and as jmz says EXPLAIN ANALYSE output to get any useful advice.
Franz - don't think whether it's possible, test and know.
This is v9.0:
What you see will depend on your version, and the stats the planner sees.
我的(简化的)查询:
像这样的查询计划有效:
但是,如果规划器认为它可能更好,有时 postgresql 9.5.9 会切换到顺序扫描(请参阅 为什么 PostgreSQL 对索引列执行顺序扫描?)。然而,就我而言,这让事情变得更糟。
我的(黑客)解决方案是通过以下方式阻止顺序扫描:
postgresql 文档说执行此操作的正确方法是使用 ALTER TABLESPACE 对 seq_page_cost 进行操作。在索引列上使用 ORDER BY 时这可能是明智的,但我不确定。 https://www.postgresql.org/docs/9.1/静态/runtime-config-query.html
My (simplified) query:
The query plan like this works:
However, sometimes postgresql 9.5.9 would switch to a sequential scan if the planner thought it might be better (see Why does PostgreSQL perform sequential scan on indexed column?). However, in my case it made things worse.
My (hack) solution was to discourage sequential scans by:
The postgresql documentation says the proper way to do this is to the seq_page_cost using ALTER TABLESPACE. This might be advisable when using ORDER BY on indexed columns, but I'm not sure. https://www.postgresql.org/docs/9.1/static/runtime-config-query.html