sqlserver之timestamp
1、多线程操作
public int UpdateProc() { string sql = "p_updatetest"; int o = SqlExecute.Instance.ExecuteProce(sql); Console.WriteLine(o.ToString()+";time:"+DateTime.Now.ToString("yyyy-MM-dd hh:MM:ss fff")); return o; } public void Update1() { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); Console.WriteLine("开始:"); bool r = true; while (r) { var r1 = System.Threading.Tasks.Task.Factory.StartNew(() => { //int rr=Update(); int rr = UpdateProc(); return rr; }); var r2 = System.Threading.Tasks.Task.Factory.StartNew(() => { //int rr=Update(); int rr = UpdateProc(); return rr; }); var r3 = System.Threading.Tasks.Task.Factory.StartNew(() => { //int rr=Update(); int rr = UpdateProc(); return rr; }); var r4 = System.Threading.Tasks.Task.Run(() => { int rr = UpdateProc(); return rr; }); var r5 = System.Threading.Tasks.Task.Run(() => { int rr = UpdateProc(); return rr; }); var r6 = System.Threading.Tasks.Task.Run(() => { int rr = UpdateProc(); return rr; }); var r7 = System.Threading.Tasks.Task.Run(() => { int rr = UpdateProc(); return rr; }); var r8 = System.Threading.Tasks.Task.Run(() => { int rr = UpdateProc(); return rr; }); var r9 = System.Threading.Tasks.Task.Run(() => { int rr = UpdateProc(); return rr; }); var r10 = System.Threading.Tasks.Task.Run(() => { int rr = UpdateProc(); return rr; }); var r11 = System.Threading.Tasks.Task.Run(() => { int rr = UpdateProc(); return rr; }); var r12 = System.Threading.Tasks.Task.Run(() => { int rr = UpdateProc(); return rr; }); var r13 = System.Threading.Tasks.Task.Run(() => { int rr = UpdateProc(); return rr; }); var r14 = System.Threading.Tasks.Task.Run(() => { int rr = UpdateProc(); return rr; }); var r15 = System.Threading.Tasks.Task.Run(() => { int rr = UpdateProc(); return rr; }); System.Threading.Tasks.Task.WaitAll(r1, r2, r3, r4,r5, r6, r7, r8, r9, r10,r11,r12,r13,r14,r15); if (r1.Result == 0 || r2.Result == 0 || r3.Result == 0 || r4.Result == 0 || r5.Result == 0|| r6.Result == 0 || r7.Result == 0 || r8.Result == 0 || r9.Result == 0 || r10.Result == 0) r = false; } stopwatch.Stop(); Console.WriteLine("结束:"+stopwatch.Elapsed.TotalMilliseconds.ToString("#0.00000000 ")); }
2、存储过程
USE [DBTest] GO /****** Object: StoredProcedure [dbo].[p_updatetest] Script Date: 2018/1/8 星期一 20:47:04 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER proc [dbo].[p_updatetest] as begin tran begin try select top 10 id,timestamp into #tempIds from tTest where istrue = 0 and num>0 order by newid()--id --WAITFOR DELAY '00:00:00.40' --update tTest set num=num-1,istrue=(case when num-1=0 then 1 else 0 end) where exists (select 1 from #tempIds where #tempIds.id=tTest.id) --update tTest set num=num-1,istrue=(case when num-1=0 then 1 else 0 end) where id in (select id from #tempIds) --and num>0 and istrue=0 update tTest set num=num-1,istrue=(case when num-1=0 then 1 else 0 end) where EXISTS (select 1 from #tempIds where #tempIds.id=tTest.id and #tempIds.timestamp=tTest.timestamp) --WAITFOR DELAY '00:00:00.100' if(@@ERROR<>0) rollback tran else commit tran end try begin catch rollback tran end catch
3、表结构

结论:速度变慢
1.公开数据库中自动生成的唯一二进制数字的数据类型。
2.timestamp 通常用作给表行加版本戳的机制。
3.存储大小为 8 个字节。 不可为空的 timestamp 列在语义上等价于 binary(8) 列。可为空的 timestamp 列在语义上等价于 varbinary(8) 列。这将导致在C#程序中获取到的timestamp类型则变成了byte[]类型。所以如果我们需要从数据库中获取并使用这个时间戳的话就必需经过转换。
4.timestamp 数据类型只是递增的数字,不保留日期或时间。 若要记录日期或时间,请使用 datetime 数据类型。
5.一个表只能有一个 timestamp 列。每次插入或更新包含 timestamp 列的行时,timestamp 列中的值均会更新。对行的任何更新都会更改 timestamp 值。
6.总结:SQL Server timestamp 数据类型与时间和日期无关。SQL Server timestamp 是二进制数字,它表明数据库中数据修改发生的相对顺序。实现 timestamp 数据类型最初是为了支持 SQL Server 恢复算法。每次修改页时,都会使用当前的 @@DBTS 值对其做一次标记,然后 @@DBTS 加1。这样做足以帮助恢复过程确定页修改的相对次序,但是 timestamp 值与时间没有任何关系。@@DBTS 返回当前数据库最后使用的时间戳值。插入或更新包含 timestamp 列的行时,将产生一个新的时间戳值。
时间戳字段在数据库中起什么作用:
1.给一个表加一个时间戳字段(timestamp),假设某条记录同时被两个人A和B读取并且正在修改。A先修改完成然后保存了,然后B再保存的时候,会由于时间戳不一致(因为A之前先保存修改了时间戳)导致B保存失败。timestamp是数据库记录版本控制的好东西,Linq to sql, entity framework都有很好的支持。
2.数据库优化:当在处理几十万条并发数据时,我们就可以在使用最频繁的表中添加一列字段,类型为timestamp,添加完毕后系统会自动生成相应的唯一值,如果数据记录有任何改动,timestamp值也会做相应的调整。
1.http://www.cnblogs.com/iampkm/p/4082916.html
2.http://www.cnblogs.com/windows/articles/2149701.html

浙公网安备 33010602011771号