Using “IN” in a WHERE clause where the number of items in the set is very large

前端 未结 10 2370
梦毁少年i
梦毁少年i 2021-02-13 05:30

I have a situation where I need to do an update on a very large set of rows that I can only identify by their ID (since the target records are selected by the user and have noth

10条回答
  •  囚心锁ツ
    2021-02-13 05:59

    How do you generate the IN clause?

    If there is there another SELECT statement that generates those values, you could simply plug that into the UPDATE like so:

    UPDATE TARGET_TABLE T
    SET
      SOME_VALUE = 'Whatever'
    WHERE T.ID_NUMBER IN(
                        SELECT ID_NUMBER  --this SELECT generates your ID #s.
                        FROM SOURCE_TABLE
                        WHERE SOME_CONDITIONS
                        )
    

    In some RDBMses, you'll get better performance by using the EXISTS syntax, which would look like this:

    UPDATE TARGET_TABLE T
    SET
      SOME_VALUE = 'Whatever'
    WHERE EXISTS (
                 SELECT ID_NUMBER  --this SELECT generates your ID #s.
                 FROM SOURCE_TABLE S
                 WHERE SOME_CONDITIONS
                   AND S.ID_NUMBER =  T.ID_NUMBER
                 )
    

提交回复
热议问题