Lets say I have a database table which consists of three columns: id, field1 and field2. This table may have anywhere between 100 and 100,000 rows in it. I have a python script that should insert 10-1,000 new rows into this table. However, if the new field1 already exists in the table, it should do an UPDATE, not an INSERT.
Which of the following approaches is more efficient?
- Do a
SELECT field1 FROM table(field1is unique) and store that in a list. Then, for each new row, uselist.count()to determine whether toINSERTorUPDATE - For each row, run two queries. Firstly,
SELECT count(*) FROM table WHERE field1="foo"then either theINSERTorUPDATE.
In other words, is it more efficient to perform n+1 queries and search a list, or 2n queries and get sqlite to search?
If I understand your question correctly, it seems like you could simply use SQLite’s built in conflict handling mechanism.
Assuming you have a UNIQUE constraint on field1, you could simple use:
The following syntax is also supported (identical semantics):
EDIT: I realise that I am not really answering your question, just providing an alternative solution which should be faster.