SqlServer性能优化 提升并发性能(八)

并发访问:并发

   当多个线程访问同一个资源,会产生并发性问题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)

 结果:(只能访问不被锁住的)

相关文章
相关标签/搜索