Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 964481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:50:03+00:00 2026-05-16T01:50:03+00:00

Lets say I have a database table which consists of three columns: id ,

  • 0

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?

  1. Do a SELECT field1 FROM table (field1 is unique) and store that in a list. Then, for each new row, use list.count() to determine whether to INSERT or UPDATE
  2. For each row, run two queries. Firstly, SELECT count(*) FROM table WHERE field1="foo" then either the INSERT or UPDATE.

In other words, is it more efficient to perform n+1 queries and search a list, or 2n queries and get sqlite to search?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T01:50:05+00:00Added an answer on May 16, 2026 at 1:50 am

    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:

    INSERT OR REPLACE INTO table VALUES (...)
    

    The following syntax is also supported (identical semantics):

    REPLACE INTO table VALUES (...)
    

    EDIT: I realise that I am not really answering your question, just providing an alternative solution which should be faster.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say we have a database table with two columns, entry_time and value. entry_time
I have multiple tables in a MySQL database. Lets say they look like this:
Lets say I have a table with just two columns: name and mood .
Let's say I have an Oracle database table of 40MM 9-digit ZIP codes which
I have a mysql database table filled with 1000+ records, lets say 5000 records.
i have this table in database which called data and i want make this
I have a Firebird database, which let's say has Tables A and B which
Let's say I have the following database table: record_id | record_date | record_value -----------+-------------+--------------
Let's say you have a database with a single table like... --------------------------------------------- | Name
Let's say i have a table in a database with 10k records. I dont

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.