2

I'm applying this Query to 8257800 point but it's taking 15 hours and it didn't return a results.

SELECT a.gid , b.gid, st_distance(a.geom,b.geom)
FROM shp1 a, shp2 b
where st_intersects(st_difference(st_buffer(a.geom,2000),st_buffer(a.geom,500)),b.geom) and abs(a.value - b.value)>400

Is there any suggestions to render this query fast?

2 Answers 2

2

Go with steps: -Create spatial indexes for your tables.

-Create Buffers

CREATE OR REPLACE VIEW b1 as 
SELECT ST_Buffer(the_geom,500) as the_geom, attributes, fid 
FROM stores;

CREATE OR REPLACE VIEW b2 as 
SELECT ST_Buffer(the_geom,2000) as the_geom, attributes, fid 
FROM stores;

(I am not sure, but maybe you should reduce here you 8 million point to less whith a materialized view like this:

select a.point_attributes
from b2 b, spoints a
where st_within(a.the_geom, b.the_geom)

)

-Create ring view r2:

CREATE OR REPLACE VIEW r2 as 
SELECT ff.fid, ff.attributes, ST_Difference(ff.the_geom, g.the_geom) as new_geom  
FROM b2 ff
JOIN b1 g ON (g.fid= ff.fid)
GROUP BY ff.fid, ff.attrubets, g.the_geom, ff.the_geom;

-Select those rings where the altitude is bigger than 400 meters.

-And the slow bit the intersection:

SELECT ff.fid, ff.attributes FROM r2 ff
JOIN points_table g ON ST_Intersects(g.geom, ff.the_geom)
GROUP BY ff.fid, ff.attributes, ff.the_geom;
3
  • thanks a lot, you answer looks like great but please what do you mean by 'ff' and 'g' ?
    – elmo
    May 22, 2015 at 8:23
  • ff is just the r2. If you have a long table name like "myspatialtable" you can use shortnames (in this case I used ff because that was in my original query)
    – Tamas Kosa
    May 22, 2015 at 8:29
  • okey man, it's an alias thakns a lot
    – elmo
    May 22, 2015 at 8:35
2

Your query looks rather strange. What are you trying to achieve with two buffers around the same point?

You can replace ST_Distance with operator, but still we have to overthink the buffer thing.

Could you specify your goal a little?

PS: Did you build spatial index?

UPDATE:

I would try

SELECT * FROM (
    SELECT
        a.gid,
        b.gid,
        a.geom <-> b.geom distance
    FROM 
        shp1 a, shp2 b
    WHERE
        abs(a.value - b.value) > 400 AND
        ST_DWithin(a.geom, b.geom, 2000)
    ) sq
WHERE
    distance > 500;
6
  • i'm trying to generate a circle ring with the two buffers. My goal is returning all points wish intersects with the buffer ring of a point ans the conditions are : 500<distance<2000 and the altitude difference is upper than 400 meters
    – elmo
    May 22, 2015 at 7:35
  • my answer updated May 22, 2015 at 7:41
  • thanks for your answer but the distance column does not exist, i want calculate and return it
    – elmo
    May 22, 2015 at 7:47
  • distance is not a column, it is just an alias for a.geom <-> b.geom. Answer update - column alias from having clause removed. May 22, 2015 at 7:53
  • If it works as expected, please mark the answer as correct. Thanks. May 22, 2015 at 8:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.