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 8166827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T20:00:51+00:00 2026-06-06T20:00:51+00:00

Let’s say I have a list of primary keys, for each row one value

  • 0

Let’s say I have a list of primary keys, for each row one value needs updating. Is it better to run:

-- run 10,000 of these queries
UPDATE mytable SET myflag = 1 WHERE id = [each_id]

Or combine updates into batch queries like this:

-- run 100 of these queries, where the IN () list contains about 100 elements
UPDATE mytable SET myflag = 1 WHERE id IN (3, 4, 5, 9, 99, ... 7887 )

How about 100 queries with 100 IN () items?

  • 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-06-06T20:00:52+00:00Added an answer on June 6, 2026 at 8:00 pm

    Neither. In PostgreSQL I would instead:

    WITH x AS (
       SELECT unnest('{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20
                     ,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40
                     ,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60
                     ,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80
                     ,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100
                     }'::int[]) AS id
       )
    UPDATE mytable t
    SET    myflag = 1
    FROM   x
    WHERE  t.id = x.id;
    

    I put so many IDs in my example to give a visual clue that 10000 IDs is a lot. The two ideas presented in the question would either:

    1. have to parse the list and put together 10000 statements and send them to the server, which may very well take longer than the UPDATEs themselves.

    2. have to search in a list (array) of 10000 items for each individual id in mytable for a matching id. Standard indexes can’t be used. This will be very slow. Performance degrades with the size of mytable.

    An index on mytable.id is all the presented alternative needs to outperform both variants by an order of magnitude.

    The CTE parses the array once (subquery works, too – MySQL has no CTEs) – and unnest() is rather fast with that. Doing it all in one statement beats 10000 statements by an order of magnitude. Add another order of magnitude if those statements are run in individual transactions. Add another one if you should use individual sessions.

    Rare exceptions apply for databases with locking issues under heavy write load. Just benchmark as has been advised. EXPLAIN ANALYZE is your friend in PostgreSQL.

    If the operation grows huge, and most of the table is updated and / or you are running low on disk space or RAM, it may still be a good idea to split the operation into several logical chunks – just not too many, find the sweet spot. Mostly to let HOT updates recycle table bloat from previous UPDATE runs. Consider this related question.

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

Sidebar

Related Questions

let's say I have a select list as follows: <select name='languages'> <option value='german'>German</option> <option
Let's say, we have and list of objects in variable called articles, each object
Let's say I have a sortable list like this: $(.song-list).sortable({ handle : '.pos_handle', axis
Let's say I have a C++ Visual Studio 2010 solution with 2 projects: one
Let's say you have a method that expects a numerical value as an argument.
Let's say you have two different categories of users with different profiles: one for
Let's say we have a simple function defined in a pseudo language. List<Numbers> SortNumbers(List<Numbers>
Let's say I don't have photoshop, but I want to make pattern files (.pat)
Let's say I have a method in java, which looks up a user in
Let me explain best with an example. Say you have node class that can

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.