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?