补充上一篇修改用非聚集索引:

    update Employee set age=age+1 from Employee with(index=nc_Employee_Age) where age<30

执行计划:

并发访问控制隔离级别:

1.读提交:默认行为,读取时请求S锁

	set transaction isolation level read committed
select * from Employee where age=34

2.   脏读:读取时不请求S锁,不会受到其他X锁限制

	set transaction isolation level read uncommitted
select * from Employee

3. 已提交快照读:更新时将老的数据复制到 Tempdb:read_committed_snapshot

        alter database HRDB
set read_committed_snapshot on

设置成单用户模式:

设置事物:

	begin tran
update Employee set age=age+1 where age>=30

执行语句:

select * from Employee

这条语句也可以执行:

select * from Employee where age>30

这条可以执行:

update Employee set age=age+1 from Employee with(index=nc_Employee_Age) where age<30

没有复制的还是从索引或基表中读取。所以可以读取出数据

这条不能执行:

update Employee set age=age+1 from Employee with(index=nc_Employee_Age) where age>30

打开数据库的读提交快照。 对记录进行操作时,会把排他锁的数据放到Tempdb数据库中,访问的时候直接读出Tempdb的数据。

4.可重复读:事物结束前,不释放获取的S锁,可能会形成死锁

create table Products(id int identity(1,1),name varchar(500),UnitPrice money)

 delete from Products where id=2
insert Products values ('p1',13)
insert Products values('p2',5) --业务逻辑:单价大于10的优惠10
begin tran
declare @UnitPrice money
set @UnitPrice=(select @UnitPrice from Products where id=1) --执行等待的时间
waitfor delay '00:00:20'
if @UnitPrice>10
update Products set UnitPrice=UnitPrice-10 where id=1
commit tran

第二个人执行打六折的业务:

update Products set UnitPrice=UnitPrice*0.6

结果:

事务中尽量不要放查询语句:13*0.6=7.8      7.8-10=-2.2

实在要查询语句,如何解决呢?

删除上述表:drop table Products   重新创建

1. --在开启事务之前  设置事务的级别  可重复读

 --在开启事务之前  设置事务的级别  可重复读
set transaction isolation level repeatable read
--业务逻辑:单价大于10的优惠10
begin tran
declare @UnitPrice money
set @UnitPrice=(select @UnitPrice from Products where id=1) --执行等待的时间
waitfor delay '00:00:20'
if @UnitPrice>10
update Products set UnitPrice=UnitPrice-10 where id=1
commit tran

2.打六折:

update Products set UnitPrice=UnitPrice*0.6

结果:

with(updlock) 可重复读的方式 可以保护线程。代码如下:

begin tran
declare @UnitPrice money
set @UnitPrice=(select @UnitPrice from Products with(updlock) where id=1) --执行等待的时间
waitfor delay '00:00:20'
if @UnitPrice>10
update Products set UnitPrice=UnitPrice-10 where id=1
commit tran update Products set UnitPrice=UnitPrice*0.6

5. 串行化:访问的行和按顺序下一行放置范围锁,防止不必要操作与插入数据

业务背景:给分组为:‘group1’的员工发奖金,加入了新的员工

  create table Employees(id int identity(1,1),name varchar(500),groups varchar(500),salary money)
insert Employees values('caojian','grouup1',3000)
insert Employees values('ligang','grouup1',1000)
insert Employees values('huang','grouup2',1500)
insert Employees values('sunliyuan','grouup2',2000)

业务逻辑的事务语句:

  begin tran
declare @count int
set @count=(select COUNT(*) from Employees where groups='grouup1')
declare @avgsalary money
set @avgsalary=20000/@count waitfor delay '00:00:20'
update Employees set salary=salary+@avgsalary where groups='grouup1'
commit tran

第二个线程执行的语句:

insert Employees values ('newemployee','grouup1',0)

这种结果是不对的:

--设置串行化

  set transaction isolation level  serializable
begin tran
declare @count int
set @count=(select COUNT(*) from Employees where groups='grouup1')
declare @avgsalary money
set @avgsalary=20000/@count waitfor delay '00:00:20'
update Employees set salary=salary+@avgsalary where groups='grouup1'
commit tran

进行添加:

insert Employees values ('newemployee','grouup1',0)

执行查询语句:

针对group创建索引:

 --针对group创建聚集索引
create clustered index c_Employees_group on Employees (groups)

执行事务:

 --设置串行化
set transaction isolation level serializable
begin tran
declare @count int
set @count=(select COUNT(*) from Employees where groups='grouup1')
declare @avgsalary money
set @avgsalary=20000/@count waitfor delay '00:00:20'
update Employees set salary=salary+@avgsalary where groups='grouup1'
commit tran

执行以下三条语句:

insert Employees values ('newemployee','grouup1',0)
insert Employees values ('newemployee','grouup2',0)
insert Employees values ('newemployee','grouup3',0)

grouup1有影响,grouup2和grouup3无影响。

6. 快照:比已提交快照读取更严格,试图对修改数据应用X(排他锁),如果已发生改变,事物失败 allow_snapshot_isolation

创建表:

create table SnapShotTB(id int identity(1,1),name varchar(500),age int)
insert SnapShotTB values('caojian',33)

给数据库进行配置:

--打开配置数据库的一个选项
alter database HRDB
--允许快照隔离
set allow_snapshot_isolation on

设置事物的隔离级别:

--设置事物的隔离级别为快照
set transaction isolation level snapshot
begin transaction
declare @age int
set @age=(select age from SnapShotTB where name='caojian')
waitfor delay '00:00:20'
update SnapShotTB set age =age+1 where name='caojian'
commit tran

第二个线程:

   update SnapShotTB set age =age-1 where name='caojian'

报的错误:

减少阻塞与死锁的建议:

1.合适的索引

2.合适的分区

3.调整合适的隔离级别

4.查询条件的有限性

5.相同的顺序操作资源

6.短的事务

.NET 调用的案例:(EF CodeFirst)

1.连上数据库。

2.引入命名空间。

3.在领域层引入命名空间:

    using System.Transactions;
using System.Data;
using System.Linq;

4.代码:

/// <summary>
/// 奖金处理的业务
/// </summary>
public void ProcessSalary()
{
TransactionOptions option = new TransactionOptions();
//指定的隔离级别(串行化)
option.IsolationLevel = System.Transactions.IsolationLevel.Serializable;
using (TransactionScope scope=new TransactionScope(TransactionScopeOption.Required,option))
{
//连到数据访问的上下文
HRUser dbcontext = new HRUser();
var employees = dbcontext.Set<Employees>().Where(p => p.groups == "grouup1").ToList();
//取得groupp1组的人数
int count = employees.Count;
//把奖金进行employees平分
decimal salary = 20000 / count;
//对每个人的值进行跟新
foreach (var emoloyee in employees)
{
dbcontext.Set<Employees>().Attach(emoloyee);
//状态是可修改的
dbcontext.Entry<Employees>(emoloyee).State = System.Data.Entity.EntityState.Modified;
emoloyee.salary = emoloyee.salary + salary;
}
dbcontext.SaveChanges();
//事物的完成
scope.Complete();
}
}

调用:

         protected void Button1_Click(object sender, EventArgs e)
{
Employees es = new Employees();
es.ProcessSalary();
}

点击button数据库更新成功。

7. 索引对隔离级别的影响、阻塞的监视

SqlServer性能优化 提高并发性能二(九)的更多相关文章

  1. SqlServer性能优化 提高并发性能(八)

    并发访问: 当多个线程访问同一个资源,会产生并发性问题 并发控制与处理: 乐观并发控制:一种方式是"后来的更新者获胜"   这意味着先来的用户提交的值会在没有察觉的情况下丢失. 为 ...

  2. 性能优化——Web前端性能优化

    核心知识点: 1.排查网站性能瓶颈的手法:分析各个环节的日志,找出异常部分 2.Web前端:网站业务逻辑之前的部分(浏览器.图片服务.CDN) 3.优化手段 1)浏览器优化 (1)减少http请求 a ...

  3. Android App性能优化笔记之一:性能优化是什么及为什么?

    By Long Luo   周星驰的电影<功夫>里面借火云邪神之口说出了一句至理名言:“天下武功,唯快不破”. 在移动互联网时代,同样如此,留给一个公司的窗口往往只有很短的时间,如何把握住 ...

  4. 微擎开启性能优化里面的性能优化memcache内存优化及数据库读写分离

    http://www.mitusky.com/forum.php?mod=viewthread&tid=3135 [微擎 安装使用] 微擎开启性能优化里面的性能优化memcache内存优化及数 ...

  5. nginx配置优化提高并发量

    1 nginx配置优化提高并发量 worker_processes 2; 这个按照CPU的核数来决定 2 worker_connections 65535; 这个一般设置65535即可 每个进程允许的 ...

  6. web性能优化-网络传输性能优化

    浏览器工作原理:https://www.cnblogs.com/thonrt/p/10008220.html 浏览器渲染原理: https://www.cnblogs.com/thonrt/p/100 ...

  7. Web前端性能优化——提高页面加载速度

    前言:  在同样的网络环境下,两个同样能满足你的需求的网站,一个“Duang”的一下就加载出来了,一个纠结了半天才出来,你会选择哪个?研究表明:用户最满意的打开网页时间是2-5秒,如果等待超过10秒, ...

  8. JVM性能优化,提高Java的伸缩性

    很多程序员在解决JVM性能问题的时候,花开了很多时间去调优应用程序级别的性能瓶颈,当你读完这本系列文章之后你会发现我可能更加系统地看待这类的问题.我说过JVM的自身技术限制了Java企业级应用的伸缩性 ...

  9. SQL Server查询性能优化——覆盖索引(二)

    在SQL Server 查询性能优化——覆盖索引(一)中讲了覆盖索引的一些理论. 本文将具体讲一下使用不同索引对查询性能的影响. 下面通过实例,来查看不同的索引结构,如聚集索引.非聚集索引.组合索引等 ...

随机推荐

  1. 简述alert和console.log的区别

    生活中还是得有发现美好和差别的眼睛~~ 学习前端那么久既然还不知道alert和console.log的差别~~~~ 蓝瘦,香菇~~~ 本菜鸟一直以为alert和console.log其实是一样的用法, ...

  2. 2015-01-19 .Net 软件工程师 笔试题

    填空题 1.String是__类型(值类型或引用类型) 2.int的基类是__ 3.实现__的类,可以应用foreach语句 4.要输出一段XML文档,用__类来实现能够确保输出正确格式的XML,输出 ...

  3. 关于小组所要做的APP的想法

    关于小组所要做的app,我们敲定下来是做关于在线做题的app,但是,纯粹的做题目的app我认为并没有什么大的吸引力,尤其是拿手机做题.所以,我们考虑准备在以下几个方面做功夫以增加吸引力.第一,我们的题 ...

  4. 一个网络传输框架——zeroMQ 调研笔记

    一.它是什么 zeroMQ,一个处理消息传输的库,重点在传输上,看起来它像是在socket上面封装了一层,让我们可以很容易的利用它来做N对M的数据传输,在分布式系统中很方便,在接收端它有round-r ...

  5. 使用Kettle抽取数据时,出现中文乱码问题解决方案

    使用Kettle在不同的数据库抽取数据时,有时会出现中文乱码问题:其解决方案如下: 1.查看数据库的字符集是否是UTF-8(最常用的字符集) 2.如果数据库设置正确仍然存在中文乱码,则可能是因为有的客 ...

  6. 安装 node-sass 的正确姿势

    SASS_BINARY_SITE=https://npm.taobao.org/mirrors/node-sass/ npm install node-sass

  7. ASP.NET MVC 4 (三) 过滤器

    先来看看一个例子演示过滤器有什么用: public class AdminController : Controller { // ... instance variables and constru ...

  8. app上传到App Store的快捷方法及步骤

    跳过证书的申请及配置概要文件的设置, 现在根据已有的配置概要文件及发布证书开始: 1.先在Xcode上的PROJECT和TARGETS->Build Setting->Code Signi ...

  9. 1171. Lost in Space

    http://acm.timus.ru/problem.aspx?space=1&num=1171 一天的时间,WA了N遍,居然是因为数组开小了呀,我勒个去!鄙视自己...... 我是从第 1 ...

  10. 《JavaScript高级程序设计》读书笔记--(4)对象的创建

    ECMAScript支持面向对象(OO)编程,但不使用类或者接口.对象可以在代码执行过程中创建或增强,因此具有动态性而非严格定义的实体.在没有类的情况下,可以采用下列模式创建对象. 对象的创建 工厂模 ...