Updated on 2025-03-13 GMT+08:00

UPDATE

  • [Rule] The UPDATE statement must contain the WHERE clause to avoid full table scan.
  • [Rule] Do not use the updated columns as the update source when the UPDATE clause updates multiple columns simultaneously.
    Even if multiple columns are updated simultaneously from the same source, the behavior varies depending on the database. To avoid compatibility issues, avoid the preceding operations at the service layer. Example:
    UPDATE table SET col1 = col2, col3 = col1 WHERE col1 = 1;

    In , the value of col3 is the original value of col1. In MySQL, the value of col3 is the value of col2 (because the value of col2 is assigned to col1).

  • [Rule] Do not use ORDER BY or GROUP BY in the UPDATE statement to avoid unnecessary sorting.
  • [Recommendation] If a table has a primary key or index, the WHERE condition must be used together with the primary key or index during update.