Is ST_GeomFromText better than providing direct geometry?

♀尐吖头ヾ 提交于 2019-12-12 13:32:15

问题


I have been working with postgis recently,and in my query if I use ST_GeomFromText it execute faster than running a sub-query to get geom.

I thought ST_GeomFromText will be more expensive but after running many tests every time I got the result faster, my question Is there any explanation behind this? because for me getting the geom directly in sub-query is better than getting geom as text then added as GeomFromText.

Thanks, Sara


回答1:


Your issue is that the issues are different. ST_GeomFromText is going to be an immutable function, so the output depends on the input only. This means the planner can execute it once at the beginning of the query. Running a subquery means you are having to look up the geometry, which means disk access, etc. In the first, you have a little bit of CPU activity, executed once for the query, and on the second you have disk lookups.

So the answer to some extent depends on what you are doing with it. In general, you can assume that the optimizer will handle things like type conversions on input, where those are not dependent on settings, quite well.

Think about it this way.

SELECT * FROM mytable WHERE my_geom = ST_GeomFromText(....);

This gets converted into something like the following pseudocode:

 private_geom = ST_GeomFromText(....);
 SELECT * FROM mytable WHERE my_geom = private_geom;

Then that query gets planned and executed.

Obviously you don't want to be adding round trips just to avoid in-query lookups, but where you know the geometry, you might as well specify it via ST_GeomFromText(....) in your query.



来源:https://stackoverflow.com/questions/10086684/is-st-geomfromtext-better-than-providing-direct-geometry

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!