所以答案在某种程度上取决于您使用它的方式.通常,您可以假定优化器可以很好地处理输入上的类型转换之类的事情,而这些转换并不依赖于设置.这样想吧.SELECT * FROM mytable WHERE my_geom = ST_GeomFromText(....);这将转换为以下伪代码: private_geom = ST_GeomFromText(....); SELECT * FROM mytable WHERE my_geom = private_geom;然后计划并执行该查询.很显然,您不想为了避免在查询中进行查找而添加往返行程,但是在知道几何形状的地方,也可以通过查询中的ST_GeomFromText(....)指定它.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 解决方案 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. 这篇关于ST_GeomFromText是否比提供直接几何更好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!