SQL SERVER性能分析 死锁检测数据库阻塞语句
作中数据库经常出错死锁,并且还要要求解决当前的死锁,问题多多;
参照CSDN,中国风(Roy)一篇死锁文章并改进了下;
/***********************************************************************************************************************
整理人:黑木崖上的蜗牛(lenolotus) 日期:2009.04.28
************************************************************************************************************************/
/***********************************************************************************************************************
阻塞:其中一个事务阻塞,其它事务等待对方释放它们的锁,同时会导致死锁问题。 整理人:中国风(Roy) 参照Roy_88的博客
http://blog.csdn.net/roy_88/archive/2008/07/21/2682044.aspx
日期:2008.07.20
************************************************************************************************************************/
--生成测试表Ta
if not object_id('Ta') is null
drop table Ta
go
create table Ta(ID int Primary key,Col1 int,Col2 nvarchar(10))
insert Ta
select 1,101,'A' union all
select 2,102,'B' union all
select 3,103,'C'
go
生成数据:
/*
表Ta
ID Col1 Col2
----------- ----------- ----------
1 101 A
2 102 B
3 103 C (3 行受影响) */
1、将处理阻塞减到最少:
2、不要在事务中请求用户输入
3、在读数据考虑便用行版本管理
4、在事务中尽量访问最少量的数据
5、尽可能地使用低的事务隔离级别
阻塞1(事务):
--测试单表
-----------------------连接窗口1(update\insert\delete)------------------------------
begin tran
--update
update ta set col2='BB' where ID=2
--或insert
--begin tran
-- insert Ta values(4,104,'D')
--或delete
--begin tran
-- delete ta where ID=1
--rollback tran
-------------------------连接窗口2(查询表)---------------------------------------------
begin tran
select * from ta
--rollback tran
--- --分析--------------------------------------------------
-->SQL SERVER 2005查询死锁进程
select
request_session_id as spid,
resource_type,
db_name(resource_database_id) as dbName
相关文档:
方法(1)
SELECT stuff((select ','+ltrim(ColumnName) from #A for xml path('')
),1,1,'')
/*
102,103,104,105
*/
方法(2)
DECLARE @s NVARCHAR(1000)='';
SELECT @s+=ColumnName+',' from #A;
SELECT @s; ......
sql 2005表的复制有两种:一种就是把整个表复制过去,就好像复制文件并且重命名。别外一种就是把表的内容复制过出.
select * into newtable form oldtable;把oldtabel复制到newtable且newtable不存在,否则出错.;
insert into newtable select * from oldtable把oldtable的内容插入到newtable, newtable一定要存在, ......
本文演示了 SQL Server 2008 分区表实例;
1. 创建测试数据库 ;
2. 创建分区函数;
3. 创建分区架构;
4. 创建分区表;
5. 创建分区索引 ;
6. 分区切换 ;
7. 查询哪些表使用了分区表;
-- 作成者 leno
-- 日期: 2009-06-06 23:50:01.700
-- ......