I have a very slow query and isolated the problem to casting my geometry type to geography. Here is my query, which basically calculates a bounding box of 15 meters (hence the need for casting to geography) around a LineString:
select st_buffer(cast(st_setsrid(lines, 4326) as geography), 15) as geometry from linesTable where id = 1
The table linesTable contains the geometries in the column lines. The cast is fine with relatively small LineStrings, but if they contain something like 500 points or more, then it takes half a second for the casting alone. The rest of the query is quick.
I need to speed this up by a factor of five or so. My concrete and to the point question is:
How can I speed up casting a complex linestring from geometry to geography? In the above query this would be the innermost part only:
cast(st_setsrid(lines, 4326) as geography
Note that st_setsrid is added for clarification only. If I drop it, then the query does not become any faster.