Hallo mulone
About your problem with pseudonyms, Luther is right.
There are two reasons for slow query.
First you convert every point that takes time
Second and probably more importand, you should use ST_Dwithin along with the spatial index instead of using distance in the where clause.
ST_Dwithin uses spatial indexes to sort many calculations.
But projecting your data onto these calculations will give you very inaccurate answers. Why not use geography features.
to try:
SELECT d, name ( SELECT ST_Distance(a.way,b.geom) as d, a.name from (SELECT way::geography, name from ga_osm_latlong_polygon) a, (SELECT 'POINT(-6.2222 53.307)'::geography as geom) b where ST_DWithin(a.way, b.geom, 1000) ) c order by d, name;
But I think I would write this more simply:
SELECT ST_Distance(a.way,b.geom) as d, a.name from (SELECT way::geography, name from ga_osm_latlong_polygon) a, (SELECT 'POINT(-6.2222 53.307)'::geography as geom) b where ST_DWithin(a.way, b.geom, 1000) order by ST_Distance(a.way,b.geom), name;
But the first version may be faster, because avoid ST_Distance to run twice.
But for this to work well, of course, you will need a spatial index. Now that I am writing this, I understand that casting in geography can be an indicator of the index. If so, I would suggest that you instead create a geography column and create an appropriate index. The work index changes what is here day and night.
Update: Or maybe you can directly create an index with a geography type. I have not tried it, but it might be worth a try: like this:
Create index idx_polygon_geog on ga_osm_latlong_polygon using gist(way::geography);
NTN
Niklas