Will SQL update affect its subquery during the update run?

后端 未结 4 751
挽巷
挽巷 2020-12-30 11:15

I\'m just composing a complex update query which looks more or less like this:

update table join
    (select y, min(x) as MinX 
     from table
     group by         


        
相关标签:
4条回答
  • 2020-12-30 11:23

    A proper RDBMS uses statement level read consistency, which ensures the statement sees (selects) the data as it was at the time the statement began. So the scenario you are afraid of, won't occur.

    Regards,
    Rob.

    0 讨论(0)
  • 2020-12-30 11:24

    Although its been noted you SHOULDN'T be able to do an update to a table based on its own data, you should be able to adjust the MySQL syntax to allow for it via

    update Table1, 
           (select T2.y, MIN( T2.x ) as MinX from Table1 T2 group by T2.y ) PreQuery
      set Table1.x = Table1.x - PreQuery.MinX
      where Table1.y = PreQuery.y
    

    I don't know if the syntax goes a different route using JOIN vs the comma list version, but by the complete prequery you do would have to be applied first for its result completed ONCE, and joined (via the WHERE) to actually perform the update.

    0 讨论(0)
  • 2020-12-30 11:27

    ** Edited **

    Selecting from the target table

    From 13.2.9.8. Subqueries in the FROM Clause:

    Subqueries in the FROM clause can return a scalar, column, row, or table. Subqueries in the FROM clause cannot be correlated subqueries, unless used within the ON clause of a JOIN operation.

    So, yes, you can perform the above query.

    The problem

    There are really two problems here. There's concurrency, or ensuring that no one else changes the data out from under our feet. This is handled with locking. Dealing with the actual modification of new versus old values is handled with derived tables.

    Locking

    In the case of your query above, with InnoDB, MySQL performs the SELECT first, and acquires a read (shared) lock on each row in the table individually. If you had a WHERE clause in the SELECT statement, then only the records you select would be locked, where ranges would cause any gaps to be locked as well.

    A read lock prevents any other query from acquiring write locks, so records can't be updated from elsewhere while they're read locked.

    Then, MySQL acquires a write (exclusive) lock on each of the records in the table individually. If you had a WHERE clause in your UPDATE statement, then only the specific records would be write locked, and again, if the WHERE clause selected a range, then you would have a range locked.

    Any record that had a read lock from the previous SELECT would automatically be escalated to a write lock.

    A write lock prevents other queries from obtaining either a read or write lock.

    You can use Innotop to see this by running it in Lock mode, start a transaction, execute the query (but don't commit it), and you will see the locks in Innotop. Also, you can view the details without Innotop with SHOW ENGINE INNODB STATUS.

    Deadlocks

    Your query is vulnerable to a deadlock if two instances were run at the same time. If query A got read locks, then query B got read locks, query A would have to wait for query B's read locks to release before it could acquire the write locks. However, query B isn't going to release the read locks until after it finishes, and it won't finish unless it can acquire write locks. Query A and query B are in a stalemate, and hence, a deadlock.

    Therefore, you may wish to perform an explicit table lock, both to avoid the massive amount of record locks (which uses memory and affects performance), and to avoid a deadlock.

    An alternative approach is to use SELECT ... FOR UPDATE on your inner SELECT. This starts out with write locks on all of the rows instead of starting with read and escalating them.

    Derived tables

    For the inner SELECT, MySQL creates a derived temporary table. A derived table is an actual non-indexed copy of the data that lives in the temporary table that is automatically created by MySQL (as opposed to a temporary table that you explicitly create and can add indexes to).

    Since MySQL uses a derived table, that's the temporary old value that you refer to in your question. In other words, there's no magic here. MySQL does it just like you'd do it anywhere else, with a temporary value.

    You can see the derived table by doing an EXPLAIN against your UPDATE statement (supported in MySQL 5.6+).

    0 讨论(0)
  • 2020-12-30 11:30

    Oracle has this in the 11.2 Documentation

    A consistent result set is provided for every query, guaranteeing data consistency, with no action by the user. An implicit query, such as a query implied by a WHERE clause in an UPDATE statement, is guaranteed a consistent set of results. However, each statement in an implicit query does not see the changes made by the DML statement itself, but sees the data as it existed before changes were made.

    0 讨论(0)
提交回复
热议问题