1

In a SpatiaLite DB, I am trying to update the value of a set of entities of a layer (table1) selected by a spatial relationship (equals) with entities from another layer (table2)

I can perform a simple selection that take about 1 minute with this request :

SELECT table1.* FROM table1, table2
WHERE EQUALS(table1.geom, table2.geom);

I can perform the update but it takes about 1 hour with this request :

UPDATE table1
SET field1 = 1
WHERE table.id IN (
SELECT table.id FROM table1, table2
WHERE EQUALS(table1.geom, table2.geom));

When I try this request which I hope could take the same time as the first request :

UPDATE table1
SET field1 = 1
WHERE EQUALS(table1.geom, table2.geom);

I get this error :

SQL error: no such column: table2.geom

I wonder if I can do the update faster than the second request. If I do it with QGIS, first the selection, then the update it very fast but I need to do it with SpatiaLite.

2
  • For information, I use spatialite-gui
    – Charles
    Commented Jul 8, 2017 at 13:33
  • Taking a minute is a string indicator you are not using a spatial index. Indexing is not automatic in spatialite - you need to include it in your query. Check the spatialite cookbook documentation.
    – BradHards
    Commented Jul 9, 2017 at 6:44

1 Answer 1

1

The reason that your update query is taking so long is that you are running the first query (the SELECT table1.*...) again and again for each row of table1.

Why not just create a new table with your first query?

CREATE TABLE table3 as SELECT table1.* FROM table1, table2
WHERE EQUALS(table1.geom, table2.geom);
SELECT RecoverGeometryColumn(table3, ...);
1
  • Thank you, I tried this solution a few time after asking my question but it also took 1 hour
    – Charles
    Commented Jul 9, 2017 at 13:23

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.