update x set y = null takes a long time

后端 未结 5 725
孤城傲影
孤城傲影 2021-01-04 03:59

At work, I have a large table (some 3 million rows, like 40-50 columns). I sometimes need to empty some of the columns and fill them with new data. What I did not expect is

5条回答
  •  说谎
    说谎 (楼主)
    2021-01-04 04:28

    That's because it deletes from blocks that data.

    And delete is the hardest operation. If you can avoid a delete, do it.

    I recommend you to create another table with that column null(Create table as select for example, or insert select), and fill it(the column) with your procedure. Drop old table and then rename the new table with current name.

    UPDATE:

    Another important thing is that you should update the column as is, with new values. It is useless to set them null and after that refill them. If you do not have values for all rows, you can do the update like this:

    udpate table1 
    set y = (select new_value from source where source.key = table1.key)
    

    and will set to null those rows that does not exists in source.

提交回复
热议问题