Keep PostgreSQL from sometimes choosing a bad query plan

后端 未结 5 1751
梦毁少年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:03

    I'm skeptical that this has anything to do with bad statistics unless you consider the combination of database statistics and your custom data type.

    My guess is that PostgreSQL is picking a nested loop join because it looks at the predicates (treenode.location).x >= 8000 AND (treenode.location).x <= (8000 + 4736) and does something funky in the arithmetic of your comparison. A nested loop is typically going to be used when you have a small amount of data in the inner side of the join.

    But, once you switch the constant to 10736 you get a different plan. It's always possible that the plan is of sufficiently complexity that the Genetic Query Optimization (GEQO) is kicking in and you're seeing the side effects of non-deterministic plan building. There are enough discrepancies in the order of evaluation in the queries to make me think that's what's going on.

    One option would be to examine using a parameterized/prepared statement for this instead of using ad hoc code. Since you're working in a 3-dimensional space, you might also want to considering using PostGIS. While it might be overkill, it may also be able to provide you with the performance that you need to get these queries running properly.

    While forcing planner behavior isn't the best choice, sometimes we do end up making better decisions than the software.

提交回复
热议问题