which concurrency control is more efficient pessimistic or optimistic concurrency control

前端 未结 2 1188
猫巷女王i
猫巷女王i 2021-01-27 03:43

I would like to know which concurrency control is more efficient, pessimistic or optimistic concurrency control? Is there a special reason why SQL Server 2005 uses pessimistic c

相关标签:
2条回答
  • 2021-01-27 04:09

    I am not sure what do you mean by "SQL Server 2005 uses pessimistic concurrency control as default". IMO SQL Server 2005 provides us with tools that allow us to implement optimistic or pessimistic ourselves. I wrote a few examples on simple-talk: Developing Modifications that Survive Concurrency

    Edit: I do not think that the default behavior of SQL Server is exactly "pessimistic concurrency control". Let us consider the following simple example, which runs under default isolation level, READ COMMITTED:

    -- Connection one
    
    BEGIN TRANSACTION;
    
    SELECT * FROM Schedule 
    WHERE ScheduledTime BETWEEN '20110624 06:30:00' 
    AND '20110624 11:30' ;
    
    -- Connection two
    UPDATE Schedule 
    SET Priority = 'High'
    WHERE ScheduledTime ='20110624 08:45:00'
    -- nothing prevent this update from completing, 
    -- so this is not exactly pessimistic
    
    -- Connection one
    DELETE FROM Schedule 
    WHERE ScheduledTime ='20110624 08:45:00' ;
    COMMIT ;
    -- nothing prevents us from deleting 
    -- the modified row
    
    0 讨论(0)
  • 2021-01-27 04:27

    This depends entirely on what concurrency needs your application has. If you are developing an OLTP app, pessiimistic would probably be best... If you are developing a single-user database, optimistic is fine.

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