![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
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? |
#3
| |||
| |||
|
|
-- 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? |
#4
| |||
| |||
|
|
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 |
#5
| |||
| |||
|
|
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? |
#6
| |||
| |||
|
|
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? |
#7
| |||
| |||
|
|
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 |
![]() |
| Thread Tools | |
| Display Modes | |
| |