473,395 Members | 1,456 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Update clustered column in place?

Is there any way to update a column in a clustered index without incurring
the cost of reordering.

Example:
Create table TableX (
Col1 int,
Col2 smalldatetime,
Col3 varchar(10))
go
create clustered indext ix_test on tableX (Col2, Col1)
go
update TableX set Col2 = '2004-12-07'

-- Yes I specifically left off the criteria to update every row.

It would seem to me in this situation that the data types require the same
storage, all rows are being updated in one transaction, and there's no new
to reorder since all will be the same.

Thanks,
Danny
Jul 23 '05 #1
2 1506
Danny,

No, when a key column in an index is updated, the row has to be deleted and
re-inserted into the index, even if all rows in the index are updated to the
same value. Server has to keep the index(btree) consistent without any out
of order row at any point of time for many reasons. For example, this is
needed to for concurrent user of the index to read the index while the
update is being performed. Even if the user ask to do the update under a
table lock, it is needed to make concurrent user conducting nolock scan
work, and most importantly if the update transaction rollback in the middle
or the server crashes in the middle of the update, undo of the update
transaction requires the btree to be consistent at any point of time.

--
Gang He
Software Design Engineer
Microsoft SQL Server Storage Engine

This posting is provided "AS IS" with no warranties, and confers no rights.
"Danny" <is****@flash.net> wrote in message
news:V6*******************@newssvr33.news.prodigy. com...
Is there any way to update a column in a clustered index without incurring
the cost of reordering.

Example:
Create table TableX (
Col1 int,
Col2 smalldatetime,
Col3 varchar(10))
go
create clustered indext ix_test on tableX (Col2, Col1)
go
update TableX set Col2 = '2004-12-07'

-- Yes I specifically left off the criteria to update every row.

It would seem to me in this situation that the data types require the same
storage, all rows are being updated in one transaction, and there's no new
to reorder since all will be the same.

Thanks,
Danny

Jul 23 '05 #2
Danny (is****@flash.net) writes:
Is there any way to update a column in a clustered index without incurring
the cost of reordering.

Example:
Create table TableX (
Col1 int,
Col2 smalldatetime,
Col3 varchar(10))
go
create clustered indext ix_test on tableX (Col2, Col1)
go
update TableX set Col2 = '2004-12-07'

-- Yes I specifically left off the criteria to update every row.

It would seem to me in this situation that the data types require the same
storage, all rows are being updated in one transaction, and there's no new
to reorder since all will be the same.


Maybe if you had updated Col1. But now you are updating the lead column:

INSERT TableX (Col1, Col2, Col3)
VALUES (1, '20040101', 'Slabank')
INSERT TableX (Col1, Col2, Col3)
VALUES (2, '20030101', 'Slabank')

When you perform the update above, the two rows will have to change places.

In practice, even if you updated Col1, I would not be surprised if there
were data moved around. Recall that in a non-unique clustered index,
SQL Server adds a four-byte uniquifier, and your update could force a
change in these. And this could lead to data being moved, although I
can't really say that I know that this will happen.

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: M Wells | last post by:
Hi All, I have a database that is serving a web site with reasonably high traffiic. We're getting errors at certain points where processes are being locked. In particular, one of our people...
10
by: R Camarda | last post by:
I have the following statement that takes quite a long time. Longest of any of my SQL statment updates. UPDATE F_REGISTRATION_STD_SESSION SET PREVIOUS_YEAR_SESSION_ID = ( SELECT...
4
by: Tryfon Gavriel | last post by:
Hi all I recently noticed when trying to optimise a major query of a chess website I am the webmaster of, that adding an order by for "gamenumber" which is a clustered index field as in for...
2
by: Miss Livvy | last post by:
Would it be OK to use varchar(5) instead of char(5) as the first field of a composite clustered index? My gut tells me that varchar would be a bad idea, but I am not finding much information on...
4
by: Thomas R. Hummel | last post by:
Hello, I am writing a stored procedure that will take data from several different tables and will combine the data into a single table for our data warehouse. It is mostly pretty straightforward...
16
by: robert | last post by:
been ruminating on the question (mostly in a 390/v7 context) of whether, and if so when, a row update becomes an insert/delete. i assume that there is a threshold on the number of columns of the...
5
by: pb648174 | last post by:
I've been doing a bit of reading and have read in quite a few places that an identity column is a good clustered index and that all or at least most tables should have a clustered index. The tool I...
22
by: pbd22 | last post by:
hi. I am having probelms with an update statement. every time i run it, "every" row updates, not just the one(s) intended. so, here is what i have. i have tried this with both AND and OR and...
4
by: =?Utf-8?B?QmFidU1hbg==?= | last post by:
Hi, I have a GridView and a SqlDataSource controls on a page. The SqlDataSource object uses stored procedures to do the CRUD operations. The DataSource has three columns one of which -...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.