dbTalk Databases Forums  

update field with the same value

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss update field with the same value in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
migurus
 
Posts: n/a

Default update field with the same value - 02-10-2011 , 06:38 PM






This is SQL Server 2005, we have an application written in such a way
that all updates are done on all columns of a table subject to change,
even though only one field needs to be modified.

For example:

create table #T (
CID INT,
DT DATETIME,
QTY INT);

INSERT INTO #T
select 1, '20110210 12:10', 10 union all
select 2, '20110210 12:20', 10 union all
select 3, '20110210 12:30', 30 union all
select 4, '20110210 12:40', 40

-- we need to change quantity to 0 on row with CID=2
-- app produces this sql:

update #T
set CID=3,DT='20110210 12:30',QTY=0
where CID=3

My question - how bad is it? in real tables some colums are indexed,
does it make server to try to modify indexes? Is SQL Server smart not
to update columns whose value was not changed?

Your input is much appreciated.
migurus

Reply With Quote
  #2  
Old   
Gene Wirchenko
 
Posts: n/a

Default Re: update field with the same value - 02-10-2011 , 07:18 PM






On Thu, 10 Feb 2011 16:38:22 -0800 (PST), migurus <migurus (AT) yahoo (DOT) com>
wrote:

Quote:
This is SQL Server 2005, we have an application written in such a way
that all updates are done on all columns of a table subject to change,
even though only one field needs to be modified.

For example:

create table #T (
CID INT,
DT DATETIME,
QTY INT);

INSERT INTO #T
select 1, '20110210 12:10', 10 union all
select 2, '20110210 12:20', 10 union all
select 3, '20110210 12:30', 30 union all
select 4, '20110210 12:40', 40

-- we need to change quantity to 0 on row with CID=2
-- app produces this sql:

update #T
set CID=3,DT='20110210 12:30',QTY=0
^
Based on the comment above, this should be "2".

Quote:
where CID=3

My question - how bad is it? in real tables some colums are indexed,
does it make server to try to modify indexes? Is SQL Server smart not
to update columns whose value was not changed?
Why are you worrying about this? Is it running too slow for you?

The internal details are likely beyond your control. Just let
the DBMS do its job.

Sincerely,

Gene Wirchenko

Reply With Quote
  #3  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: update field with the same value - 02-11-2011 , 02:54 AM



migurus (migurus (AT) yahoo (DOT) com) writes:
Quote:
-- we need to change quantity to 0 on row with CID=2
-- app produces this sql:

update #T
set CID=3,DT='20110210 12:30',QTY=0
where CID=3

My question - how bad is it? in real tables some colums are indexed,
does it make server to try to modify indexes? Is SQL Server smart not
to update columns whose value was not changed?
If this reflects the actual SQL - save the temp table - it is bad, but for
another reason than you ask about.

The app should produce parameterised SQL and not inline values, but maybe it
does in real life?

As for updating each column, even if unchanged... Since the application is
generating the SQL, it could make the effort to generate an UPDATE statement
with only the columns that changes. Then again, that's an extra complexity
and it could introduce bugs. And if you have stored procedures, you
typically have a procedure that accepts parameters for all columns.

So, all in all, not too much to be worried about - as long as it is
parameterised.

--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx

Reply With Quote
  #4  
Old   
migurus
 
Posts: n/a

Default Re: update field with the same value - 02-11-2011 , 01:14 PM



On Feb 11, 12:54*am, Erland Sommarskog <esq... (AT) sommarskog (DOT) se> wrote:
Quote:
migurus (migu... (AT) yahoo (DOT) com) writes:
-- we need to change quantity to 0 on row with CID=2
-- app produces this sql:

update #T
set CID=3,DT='20110210 12:30',QTY=0
where CID=3

My question - how bad is it? in real tables some colums are indexed,
does it make server to try to modify indexes? Is SQL Server smart not
to update columns whose value was not changed?

If this reflects the actual SQL - save the temp table - it is bad, but for
another reason than you ask about.

The app should produce parameterised SQL and not inline values, but maybeit
does in real life?

As for updating each column, even if unchanged... Since the application is
generating the SQL, it could make the effort to generate an UPDATE statement
with only the columns that changes. Then again, that's an extra complexity
and it could introduce bugs. And if you have stored procedures, you
typically have a procedure that accepts parameters for all columns.

So, all in all, not too much to be worried about - as long as it is
parameterised.

--
Erland Sommarskog, SQL Server MVP, esq... (AT) sommarskog (DOT) se

Books Online for SQL Server 2005 athttp://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books...
Books Online for SQL Server 2000 athttp://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
The app does use parameterised statement, not inline values, I put
that sql in my original post just to illustrate the situation.

To see all pros and cons (complexity, extra effort, etc) I'd like to
undesrtand is SQL Server smart not
to update columns whose value was not changed?

Thanks in advance!

Reply With Quote
  #5  
Old   
Gene Wirchenko
 
Posts: n/a

Default Re: update field with the same value - 02-11-2011 , 02:05 PM



On Fri, 11 Feb 2011 11:14:55 -0800 (PST), migurus <migurus (AT) yahoo (DOT) com>
wrote:

[snip]

Quote:
To see all pros and cons (complexity, extra effort, etc) I'd like to
undesrtand is SQL Server smart not
to update columns whose value was not changed?
It does not matter. You can not do anything about it either way.
And if you did find out, it could easily change in another release.

(You are tilting at windwheels.)

Sincerely,

Gene Wirchenko

Reply With Quote
  #6  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: update field with the same value - 02-11-2011 , 05:03 PM



migurus (migurus (AT) yahoo (DOT) com) writes:
Quote:
To see all pros and cons (complexity, extra effort, etc) I'd like to
undesrtand is SQL Server smart not
to update columns whose value was not changed?
SQL Server updates the row, and logs the row, so on this level it does not
matter. What may be relevant is indexes. If an unchanged colunm appears in
an index, will SQL Server still update those indexes?

To study this, try this batch in the Northwind database:

BEGIN TRANSACTION

UPDATE Orders
SET CustomerID = 'ALFKI',
EmployeeID = 9
WHERE OrderID = 11000

Then look at the locks (for instance with beta_lockinfo,
http://www.sommarskog.se/sqlutil/beta_lockinfo.html.) You will see that
there are locks on the indexes CustomerOrders and EmployeeOrders.

Now rollback, and change 9 to 2 (which is the current value for order 1100)
and run again and study the locks. You will see that there is no lock on
EmployeeOrders. Thus, SQL Server did realize that it not have to update
the index.

--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se

Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx

Reply With Quote
  #7  
Old   
migurus
 
Posts: n/a

Default Re: update field with the same value - 02-14-2011 , 03:59 PM



On Feb 11, 3:03*pm, Erland Sommarskog <esq... (AT) sommarskog (DOT) se> wrote:
Quote:
migurus (migu... (AT) yahoo (DOT) com) writes:
To see all pros and cons (complexity, extra effort, etc) I'd like to
undesrtand is SQL Server smart not
to update columns whose value was not changed?

SQL Server updates the row, and logs the row, so on this level it does not
matter. What may be relevant is indexes. If an unchanged colunm appears in
an index, will SQL Server still update those indexes?

To study this, try this batch in the Northwind database:

BEGIN TRANSACTION

UPDATE Orders
SET * *CustomerID = 'ALFKI',
* * * *EmployeeID = 9
WHERE *OrderID = 11000

Then look at the locks (for instance with beta_lockinfo,http://www.sommarskog.se/sqlutil/beta_lockinfo.html.) You will see that
there are locks on the indexes CustomerOrders and EmployeeOrders.

Now rollback, and change 9 to 2 (which is the current value for order 1100)
and run again and study the locks. You will see that there is no lock on
EmployeeOrders. Thus, SQL Server did realize that it not have to update
the index.

--
Erland Sommarskog, SQL Server MVP, esq... (AT) sommarskog (DOT) se

Links for SQL Server Books Online:
SQL 2008:http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005:http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
Erland, I greatly appreciate your help here - exact, to the point,
with great illustration.
Thanks a lot.

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.