同一个表,SELECT同时再进行INSERT的问题!

lanruoshui 2011-08-18 03:03:11
针对同一个表的操作,一个SELECT操作需要10秒,在执行到5秒的时候同时往这个表中INSERT,这个INSERT操作是可以执行的,但是会阻塞,我的问题是如何不阻塞?这个问题主要的考点是什么?
...全文
627 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
ohfox 2011-08-19
  • 打赏
  • 举报
回复
用 为提交读隔离级别,或者select的 with(nolock)提示
lanruoshui 2011-08-18
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 chtzhking 的回复:]
SQL code


select * from tbA(nolock)
执行上面语句的时候insert值就不会有锁的情况了


执行查询语句的时候在表后面加上(nolock)或者with(nolock),其目的就是查询是不锁定表,从而达到提高查询速度的目的。
[/Quote]
其目的是查询不锁定表,就是说查询同时可以insert???
chtzhking 2011-08-18
  • 打赏
  • 举报
回复

select * from tbA(nolock)
执行上面语句的时候insert值就不会有锁的情况了

执行查询语句的时候在表后面加上(nolock)或者with(nolock),其目的就是查询是不锁定表,从而达到提高查询速度的目的。
海木漄 2011-08-18
  • 打赏
  • 举报
回复
顶~~~~~~!
怪众生太美丽 2011-08-18
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 chtzhking 的回复:]
引用 10 楼 geniuswjt 的回复:

哥们的过程试了下
sysprocesses要改为
sys.sysprocesses
才跑的起来引用 9 楼 chtzhking 的回复:
SQL code

create procedure [dbo].[sp_who_lock]
as
begin
declare @spid int,@bl int,
@intTransact……
[/Quote]
运行的数据库不同吧,嘿 谢谢兄了,这存过好用啊...
chtzhking 2011-08-18
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 geniuswjt 的回复:]

哥们的过程试了下
sysprocesses要改为
sys.sysprocesses
才跑的起来引用 9 楼 chtzhking 的回复:
SQL code

create procedure [dbo].[sp_who_lock]
as
begin
declare @spid int,@bl int,
@intTransactionCountOnEntry int,
……
[/Quote]一直在2008上用这个过程,没发现什么错误。谢geniuswjt啦
lanruoshui 2011-08-18
  • 打赏
  • 举报
回复
谁能说说具体的,不太懂,发点相关资料也行~
geniuswjt 2011-08-18
  • 打赏
  • 举报
回复
哥们的过程试了下
sysprocesses要改为
sys.sysprocesses
才跑的起来[Quote=引用 9 楼 chtzhking 的回复:]
SQL code

create procedure [dbo].[sp_who_lock]
as
begin
declare @spid int,@bl int,
@intTransactionCountOnEntry int,
@intRowcount int,
@intCountProperties int,
@intCoun……
[/Quote]
chtzhking 2011-08-18
  • 打赏
  • 举报
回复

create procedure [dbo].[sp_who_lock]
as
begin
declare @spid int,@bl int,
@intTransactionCountOnEntry int,
@intRowcount int,
@intCountProperties int,
@intCounter int
create table #tmp_lock_who (
id int identity(1,1),
spid smallint,
bl smallint)
IF @@ERROR<>0 RETURN @@ERROR
insert into #tmp_lock_who(spid,bl) select 0 ,blocked
from (select * from sysprocesses where blocked>0 ) a
where not exists(select * from (select * from sysprocesses
where blocked>0 ) b
where a.blocked=spid)
union select spid,blocked from sysprocesses where blocked>0
IF @@ERROR<>0 RETURN @@ERROR
-- 找到临时表的记录数
select @intCountProperties = Count(*),@intCounter = 1
from #tmp_lock_who
IF @@ERROR<>0 RETURN @@ERROR
if @intCountProperties=0
select '现在没有阻塞和死锁信息' as message
-- 循环开始
while @intCounter <= @intCountProperties
begin
-- 取第一条记录
select @spid = spid,@bl = bl
from #tmp_lock_who where Id = @intCounter
begin
if @spid =0
select '引起数据库死锁的是: '+ CAST(@bl AS VARCHAR(10))
+ '进程号,其执行的SQL语法如下'
else
select '进程号SPID:'+ CAST(@spid AS VARCHAR(10))+ '被'
+ '进程号SPID:'+ CAST(@bl AS VARCHAR(10)) +'阻塞,其当前进程执行的SQL语法如下'
DBCC INPUTBUFFER (@bl )
end
-- 循环指针下移
set @intCounter = @intCounter + 1
end
drop table #tmp_lock_who
return 0
end

sp_who_lock
--执行就可以看到是什么进程在锁住数据库
kill (spid)
/*spid为阻塞进程编号!~*/即可
wnyxy001 2011-08-18
  • 打赏
  • 举报
回复
nolock

--小F-- 2011-08-18
  • 打赏
  • 举报
回复
事务和琐
Jimlyli 2011-08-18
  • 打赏
  • 举报
回复
望观~~
chtzhking 2011-08-18
  • 打赏
  • 举报
回复
sp_who_lock
执行这个
唵嘛呢叭咪吽 2011-08-18
  • 打赏
  • 举报
回复
with(nolock)?
fjicefire 2011-08-18
  • 打赏
  • 举报
回复
锁 ?
chuanzhang5687 2011-08-18
  • 打赏
  • 举报
回复
围观楼下!
快溜 2011-08-18
  • 打赏
  • 举报
回复

34,594

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧