T
T
TheZinter2022-02-23 14:42:31
Python
TheZinter, 2022-02-23 14:42:31

How to optimize bulk update in msssql?

SQL Server 2019. Up to 500k rows are processed every day. You need to update the lines according to the found parameters from other files.
My implementation in python:

  1. I create a list in which I put the result of select id from table_name where date = datetime.now() This way I get the keys to all the necessary records in the table.
  2. I start a cycle in which for each id:
    1. I do a select of the required column from table_name where id = id
    2. I look for the data I need in another file and return a tuple
    3. I do update table_name set data = my data where id = id


An iteration of 1k rows takes an average of 12 minutes, i.e. it takes about 100 hours to bust 500k. How can the process be optimized?
I considered the option to unload the id and data at once in the form of 500k lines, but then the RAM suffocates when sorting through such a list.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
vilinyh, 2022-02-23
@TheZinter

Load data into a "temporary" table and update the required rows in table_name.

update table-name set
        data = temp.data
    from temp
    where temp.id = table-name.id

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question