Keep PostgreSQL from sometimes choosing a bad query plan

后端 未结 5 1780
梦毁少年i
梦毁少年i 2020-11-22 11:51

I have a strange problem with PostgreSQL performance for a query, using PostgreSQL 8.4.9. This query is selecting a set of points within a 3D volume, using a LEFT OUT

5条回答
  •  被撕碎了的回忆
    2020-11-22 12:00

    +1 for tuning statistics target & doing ANALYZE. And for PostGIS (for OP).

    But also, not quite related to the original question, but still, if anyone gets here looking for how to deal, in general, with inaccurate planner's row count estimates in complex queries, leading to undesired plans. An option might be to wrap a part of the initial query into a function and to set its ROWS option to something more or less expected. I've never done that but should work apparently.

    Also there are row estimation directives in pg_hint_plan. I would not advice planner hinting in general, but adjusting rows estimate is a softer option.

    And finally, to enforce a nested loop scan, sometimes one might do a LATERAL JOIN with LIMIT N or just OFFSET 0 inside the subquery. That will give you what you want. But note it's a very rough trick. At some point it WILL lead to bad performance IF the conditions change - because of table growth or just a different data distribution. Still this might be a good option just to urgently get some relief for a legacy system.

提交回复
热议问题