Search code examples
mysqlsqldatabasesql-update

Do you need to update the data when it doesn't change


I want to update a row in the table,before updating, do I need to check if there is any change in each column?


Solution

  • In MySQL, you do not need to check the value. MySQL does not update the record if there is no change. That means that MySQL does not incur the overhead of logging or writing the data. There is a slight cost of checking if any values changed, and I think it does run the triggers.

    Other databases behave differently.

    This is in an arcane location in the documentation, where mysql_affected_rows() is described:

    For UPDATE statements, the affected-rows value by default is the number of rows actually changed.

    . . .

    An integer greater than zero indicates the number of rows affected or retrieved. Zero indicates that no records were updated for an UPDATE statement, no rows matched the WHERE clause in the query or that no query has yet been executed.