由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Database版 - Question about SQL server lock
相关主题
To get the 2nd, 3rd, 4th largest value有MS SQL的经验的人在那些城市容易找工作?
SQL Conditional SelectAn interview question: data store schema design
新手学数据库一个简单题求助T-SQL string split效率
SQL server Express 怎么加 link table问一个SQL Server执行外部文件的问题
改写(migrate) stored proc 问题Questions on SQL
如何让SQL 2005 CLR Trigger返回结果给Stored Procedure用?怎么写这个Query,谢谢
如何在 SQL script 里面调用dll?SQL Server 2K5,How to move a table to another filegroup?
救命,SQL server每天半夜自己shut down,束手无策问一个Oralce index的问题
相关话题的讨论汇总
话题: empid话题: dob话题: payrate话题: employee话题: update
进入Database版参与讨论
1 (共1页)
f*****e
发帖数: 5177
1
Say I have two tables, Employee and PayRate. Employee has a primary key
EmpID which is a foreign key to PayRate. I need to update any matched
employees whose pay rate is greater than 1. I have to consider both
performance and mutual lock. Here are two solutions:
1. SELECT then UPDATE
select @cnt=count(*) from employee where name=@name and dob=@dob and empid
in (select empid from payrate where rate >1)
if @cnt > 1
begin
update employee set status = 1 where name=@name and dob=@dob and empid in
(
gy
发帖数: 620
2
下面两句我没看出啥区别来, 谁说说??
(一个先判断一下, 然后update, 另一个直接update.)

if @cnt > 1
begin
update employee set status = 1 where name=@name and dob=@dob and empid in
(select empid from payrate where rate > 1)
2. UPDATE directly
update employee set status = 1 where name=@name and dob=@dob and empid in
(select empid from payrate where rate > 1)

【在 f*****e 的大作中提到】
: Say I have two tables, Employee and PayRate. Employee has a primary key
: EmpID which is a foreign key to PayRate. I need to update any matched
: employees whose pay rate is greater than 1. I have to consider both
: performance and mutual lock. Here are two solutions:
: 1. SELECT then UPDATE
: select @cnt=count(*) from employee where name=@name and dob=@dob and empid
: in (select empid from payrate where rate >1)
: if @cnt > 1
: begin
: update employee set status = 1 where name=@name and dob=@dob and empid in

B*****g
发帖数: 34098
3
乱,看不懂。不看了,凑活用merge

in

【在 f*****e 的大作中提到】
: Say I have two tables, Employee and PayRate. Employee has a primary key
: EmpID which is a foreign key to PayRate. I need to update any matched
: employees whose pay rate is greater than 1. I have to consider both
: performance and mutual lock. Here are two solutions:
: 1. SELECT then UPDATE
: select @cnt=count(*) from employee where name=@name and dob=@dob and empid
: in (select empid from payrate where rate >1)
: if @cnt > 1
: begin
: update employee set status = 1 where name=@name and dob=@dob and empid in

j*****n
发帖数: 1781
4
use IF EXISTS instead of count(*)

in

【在 f*****e 的大作中提到】
: Say I have two tables, Employee and PayRate. Employee has a primary key
: EmpID which is a foreign key to PayRate. I need to update any matched
: employees whose pay rate is greater than 1. I have to consider both
: performance and mutual lock. Here are two solutions:
: 1. SELECT then UPDATE
: select @cnt=count(*) from employee where name=@name and dob=@dob and empid
: in (select empid from payrate where rate >1)
: if @cnt > 1
: begin
: update employee set status = 1 where name=@name and dob=@dob and empid in

z***y
发帖数: 7151
5
The first solution actually duplicated in select statement.
To minimize the lock time or avoiding lock escalation, make sure you have
non clustered index on rate column in payrate table.
What would happen if you don't have non clustered index on rate column is
that query optimizer would upgrade to table level share lock if it think it
was more efficient than row level share lock. When table level share lock
was applied to table, it would exclude any update or insert operations onto
this table.

【在 f*****e 的大作中提到】
: Say I have two tables, Employee and PayRate. Employee has a primary key
: EmpID which is a foreign key to PayRate. I need to update any matched
: employees whose pay rate is greater than 1. I have to consider both
: performance and mutual lock. Here are two solutions:
: 1. SELECT then UPDATE
: select @cnt=count(*) from employee where name=@name and dob=@dob and empid
: in (select empid from payrate where rate >1)
: if @cnt > 1
: begin
: update employee set status = 1 where name=@name and dob=@dob and empid in

z***y
发帖数: 7151
6
Actually in this senario, there are no difference. A friend Andy Lenard
wrote a CLR function to tell how it works internally, he found that sql
server will return true as soon as it found first matched record. In this
senario,
it would not fetch data before sending true or false.
Another way to verify this is from the execution plan.

【在 j*****n 的大作中提到】
: use IF EXISTS instead of count(*)
:
: in

1 (共1页)
进入Database版参与讨论
相关主题
问一个Oralce index的问题改写(migrate) stored proc 问题
[合集] 问一个Oralce index的问题如何让SQL 2005 CLR Trigger返回结果给Stored Procedure用?
MS SQL database engineer(sr)ONSITE 面试该如何准备?如何在 SQL script 里面调用dll?
一个大家看起来可能很简单的问题救命,SQL server每天半夜自己shut down,束手无策
To get the 2nd, 3rd, 4th largest value有MS SQL的经验的人在那些城市容易找工作?
SQL Conditional SelectAn interview question: data store schema design
新手学数据库一个简单题求助T-SQL string split效率
SQL server Express 怎么加 link table问一个SQL Server执行外部文件的问题
相关话题的讨论汇总
话题: empid话题: dob话题: payrate话题: employee话题: update