併發訪問:併發
當多個線程訪問同一個資源,會產生併發性問題ide
併發控制與處理:性能
樂觀併發控制:一種方式是「後來的更新者獲勝」 這意味着先來的用戶提交的值會在沒有察覺的狀況下丟失。線程
爲記錄加鎖以阻止其餘事物訪問某些記錄,是避免產生併發衝突的一種技術blog
悲觀併發控制:排序
1.一個線程操做表,形成整個表被鎖定索引
2.其餘線程訪問與操做任何記錄都被阻止資源
3.其餘線程能夠添加記錄it
4.最小的吞吐量、最差的性能table
事物恢復與檢查點:
事物指南:
1.事物儘可能簡單
2.事物儘可能只包含必要的語句;驗證與查詢等語句放置在事物以外
3.避免事物與用戶的交互
避免鎖的問題:
1.丟失的更新
2.髒讀
3.不一致性分析
4.幻象集
鎖的粒度:
鎖的類型:
平衡樂觀與悲觀併發訪問:
1.創建合適的索引
2.操做語句儘可能放到短事物中
3.操做語句儘可能指定特定的篩選條件、窄的訪問列
4.索引查詢提示
5.應用程序訪問模式
建立表:
create table Employee(id int identity(1,1),name varchar(500),age int) insert Employee values('caochao',34) insert Employee values('ligang',28) insert Employee values('zhangqing',36) insert Employee values('huang',23) go begin tran update Employee set age=age+1 where age>=30
新建一個查詢窗口:
select *from Employee
訪問就被阻塞掉了。沒有結果。
執行刪改查的方法都沒什麼用。
select * from Employee where age>30 select * from name,age from employee where age<30 update employee set age=age+1 where age<30 delete employee where age=20
添加是能夠的:
insert Employee values('xili',50)
查看鎖的命令:
sp_lock
進行回滾把鎖釋放:
rollback tran
沒有排他鎖:
建立非聚簇索引:
create nonclustered index nc_Employee_age on Employee(age) include(name)
模擬開啓事物不結束:
begin tran update Employee set age=age+1 where age>=30
鎖的狀況:
訪問以下兩個語句不行:
select *from Employee select * from Employee where age>30
這條語句能夠訪問(鎖住的行不能訪問,不鎖的是能夠訪問的):
select name,age from employee where age<30
在非彙集的索引頁面進行了age進行了物理排序,訪問的是在被鎖住行排序的上面。並不須要穿透鎖住的行
這句語句是不能執行的:(沒法穿透>30的記錄)
select * from Employee where age<20
在執行跟新語句:
update employee set age=age+1 where age<30
沒法執行,查看執行計劃,直接進行了表掃描:
在執行一條查詢語句:執行的表掃描 不能進行查詢
select * from Employee where age<30
執行刪除語句:(能夠) 執行的是非彙集索引
delete employee where age=20
應用索引提示的方法:
select * from employee with(index=nc_Employee_age) where age<30
查看執行計劃:
update 不能用索引提示:
硬性訪問: readpast 繞過被排他鎖鎖住的行,直接往下面進行訪問
select * from Employee with(readpast)
結果:(只能訪問不被鎖住的)