What determines the locking order for a multi-table query?

后端 未结 2 2099
广开言路
广开言路 2020-12-16 16:49

Does the SQL standard specify the locking order for a multi-table query?

For example, given:

SELECT department.id FROM permissions, terminals, departme

相关标签:
2条回答
  • 2020-12-16 17:13

    I can give you an answer for DB2, but I think that this should be similar for other databases as well. First of all, everything depends on the locksize parameter of your tables. This parameter defines what is being locked. You can have locksize = table, page or row. So, depending on locksize of each table, the database will lock the object (table, page or row) that is used to fetch data for the cursor. So the order of locks being created will be specified by the access path, which depends on the optimizer.

    0 讨论(0)
  • 2020-12-16 17:22

    According to https://stackoverflow.com/a/112256/14731 lock order is determined by the implementation-specific execution order. The answer further goes on to say that there isn't a deterministic way to prevent deadlocks. Whereas in imperative programming we can prevent deadlocks by acquiring locks in the same order, it seems that in declarative systems we have to work around them by retrying the operation when a deadlock is detected.

    Furthermore, I argue that since database execution plans change over their lifetime it is technically impossible to prevent deadlocks.

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