0

I am trying to write a new column based on the intersection of two polygon features which have 50m rows in one table, I have created the "gist" index before running them but it takes forever and I cannot get the result in Postgresql. The gist index is not used in the computation. How can I use the index for the operation?

The model is attached in the fiddle below,

dbfiddle

CREATE INDEX idx_operation_geom ON public."Operation" USING gist (geom);

CREATE INDEX idx_operation_buffer ON public."Operation" USING gist (buffer);
CREATE INDEX idx_residential_geom ON public."Residential" USING gist (geom);

SELECT 
  ST_Intersection(ST_MakeValid(r.geom),o.buffer) AS intersection,   
  ST_Area(ST_Intersection(ST_MakeValid(r.geom),o.buffer))/ST_Area(r.geom)*100 
FROM public."Residential" r, public."Operation" o 
WHERE ST_Intersects(o.buffer,ST_MakeValid(r.geom));

I have tried to check the index using "Explain" but I am not sure how it works,

ERROR: operator does not exist: geometry = integer LINE 1: EXPLAIN SELECT * FROM public."Operation" where buffer = 2;

Any input will be very helpful.

3
  • Your buffer is of type buffer geometry(polygon,4326),You can't do an integer comparison, this is why your explain is failing. Fix this then you will see correct Explain output and you can go from there. Commented Jun 22, 2021 at 9:32
  • @VynlJunkie, Thank you for the explanation, but I am not sure how to use the Explain for the buffer geometry column, what can be done there ? Commented Jun 22, 2021 at 9:35
  • Haven't used geometry types. But look here. postgresql.org/docs/current/datatype-geometric.html According to this you would need something like "buffer=((x1,y1),...)" In your same fiddle you even use it correctly. "buffer = ST_Buffer(geom::geography,100)::geometry;" Commented Jun 22, 2021 at 9:55

1 Answer 1

2

The index is not used because you have indexed geom

CREATE INDEX idx_residential_geom ON public."Residential" USING gist (geom);

but in the function that could call the index, you are using ST_MakeValid(geom)

Either you fix your geometries when you insert them, and you can drop the st_makeValid() statements in all other queries, or you would have to index the output of ST_MakeValid()

CREATE INDEX idx_residential_valid_geom ON public."Residential" USING gist (ST_MakeValid(geom));

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.