SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


lock table during index creation


lock table during index creation

Author
Message
sqlblue
sqlblue
SSC Veteran
SSC Veteran (228 reputation)SSC Veteran (228 reputation)SSC Veteran (228 reputation)SSC Veteran (228 reputation)SSC Veteran (228 reputation)SSC Veteran (228 reputation)SSC Veteran (228 reputation)SSC Veteran (228 reputation)

Group: General Forum Members
Points: 228 Visits: 295
Hi, I need to create 2 indexes on a large table
(~200 million records). I wanted to lock the entire table
during these 2 transactions to prevent any update or insert
to the table by the users, but the user should be able to do select. I'm on SQL Server 2005. My example of code below. Could you please let
me know if I have it correctly.

use testdb
go
begin tran
select top 1 * table1 with(tablockx)
create nonclustered index (IX_ID_Type_Date)
on table1(id,type, date)
if @@error <> 0
rollback tran
else
commit tran
go

begin tran
select top 1 * table1 with(tablockx)
create nonclustered index (IX_ID_Company)
on table1(id, Company)
if @@error <> 0
rollback tran
else
commit tran
go
GilaMonster
GilaMonster
SSC Guru
SSC Guru (114K reputation)SSC Guru (114K reputation)SSC Guru (114K reputation)SSC Guru (114K reputation)SSC Guru (114K reputation)SSC Guru (114K reputation)SSC Guru (114K reputation)SSC Guru (114K reputation)

Group: General Forum Members
Points: 114477 Visits: 45501
Why do you want to lock the tables?

iirc, in SQL 2000, creating a nonclustered index makes the table readonly for the duration of the index creation

Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


GilaMonster
GilaMonster
SSC Guru
SSC Guru (114K reputation)SSC Guru (114K reputation)SSC Guru (114K reputation)SSC Guru (114K reputation)SSC Guru (114K reputation)SSC Guru (114K reputation)SSC Guru (114K reputation)SSC Guru (114K reputation)

Group: General Forum Members
Points: 114477 Visits: 45501
Not at all. SQL's quite capable of handling index creation while the DB's in use.

Since you're on SQL 2005, you can create the index either online or offline. Offline is the behaviour from 2000, the table is readonly while the index is been created.
Online, the table can be read and written while the index is been created and after creation, SQL merges in any changes that happened during the build.

Either way, you don't have to force locks or transactions or anything like that. The engine handles all that internally

Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


jshahan
jshahan
SSChasing Mays
SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)

Group: General Forum Members
Points: 639 Visits: 2112
Hi Gail (I hope you see this)

I have databases using SQL 2005, 2008 and 2008 R2.

I want to drop a non clustered primary key and immediately add a clustered version of the same index.

Will SQL Server still “protect me” in this circumstance if this is done while the database is in use? I won't get dupes or anything in the PK column?

Script would be something like this:

ALTER TAble Table1
drop PK_Table1

ALTER TABLE Table1
ADD CONSTRAINT [PK_Table1]
PRIMARY KEY CLUSTERED
(
[ID] ASC
)
GilaMonster
GilaMonster
SSC Guru
SSC Guru (114K reputation)SSC Guru (114K reputation)SSC Guru (114K reputation)SSC Guru (114K reputation)SSC Guru (114K reputation)SSC Guru (114K reputation)SSC Guru (114K reputation)SSC Guru (114K reputation)

Group: General Forum Members
Points: 114477 Visits: 45501
BEGIN TRANSACTION

ALTER TAble Table1
drop PK_Table1

ALTER TABLE Table1
ADD CONSTRAINT [PK_Table1]
PRIMARY KEY CLUSTERED
(
[ID] ASC
)

COMMIT TRANSACTION



Please in future post new questions in a new thread. Thanks

Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


jshahan
jshahan
SSChasing Mays
SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)

Group: General Forum Members
Points: 639 Visits: 2112
Sorry for the improper posting. I thought my question was virtually identical and using the same thread seemed to make sense if someone could find all the answers in one spot. Again my apologies. Won't do it agani.

I take it from your response that wrapping this in a transaction will prevent a problem with duplicate keys, but would it make sense to do this when a database is in use and the table has a couple of million rows?
GilaMonster
GilaMonster
SSC Guru
SSC Guru (114K reputation)SSC Guru (114K reputation)SSC Guru (114K reputation)SSC Guru (114K reputation)SSC Guru (114K reputation)SSC Guru (114K reputation)SSC Guru (114K reputation)SSC Guru (114K reputation)

Group: General Forum Members
Points: 114477 Visits: 45501
Couple million's small, but no, modifying indexes is not a good idea when the server is in use.

Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search