Showing posts with label delete. Show all posts
Showing posts with label delete. Show all posts

Wednesday, March 21, 2012

Primary Key Question

I have been developing a .NET Web app with an SQL Server 2005 Express database. Since I've been testing insert and delete code with a large data source, the Primary Key column which is also an autoincrement integer column is now at very large numbers (starting at over 40,000 now). I've tried doing a shrink on the database, but other then reducing the database size, it has done nothing to reduce the Primary Key numbers. Is there a command way to reduce the starting number back to 1 or do I need to completely reconstruct the database from scratch?

TIA

hi,

in order to "reset" the identity column value, you can "truncate" the table, that will implicitely reset the aut generated identity to your initial value via the
TRUNCATE TABLE schema.object;
statement... you will need high permissions on the object itself, and the table must not be referenced by a foreign key, ...
you can read further about requirements and permissions at http://msdn2.microsoft.com/en-us/library/ms177570.aspx...

or you can execute a
DBCC CHECKIDENT ('schema.object', RESEED);
command... start reading http://msdn2.microsoft.com/en-us/library/ms176057.aspx for further info and requirements..

regards

|||

Thanks - I'll give it a try.

Carl

|||

No dice. Neither of those options does it. That I want is, for example, a way of changing the lowest Primary/Identity Key value from 40,000 to 1. There are no records at lower values than 40,000. I'd also like to move all the Primary/Identity Key values down as well.

I suspect I need to effectively copy all the non-Primary/Identity Key data into a newly created database. If I do that, I think it is a characteristic of the INSERT statement that the Primary/Identity Key values will start at the seed values and autoincrement from there as rows are inserted and the incoming Primary/Identity Keys are ignored. I can then delete the original database and rename the new one to the original name.

Sound right?

Carl

|||

hi,

ok. you want to "re-assign" an autogenerated value starting from "1" to existing rows...

you actually do not have to "drop" the database...

just perform a "SELECT ... INTO" another table that will be created on the fly for you, as a temporary storage... then drop all rows from the "real" table and reset the identity value and repopulate the original table from the temp storage...

I mean something like

SET NOCOUNT ON;

USE tempdb;

GO

CREATE TABLE dbo.myData(

Id int NOT NULL IDENTITY PRIMARY KEY,

Data varchar(10) DEFAULT 'test data'

);

GO

PRINT 'this will add some data';

DECLARE @.i int;

SET @.i = 1;

INSERT INTO dbo.myData VALUES ( DEFAULT );

WHILE @.i < 20 BEGIN

INSERT INTO dbo.myData SELECT Data FROM dbo.myData;

SET @.i = @.i + 1;

END;

GO

SELECT COUNT(*) AS [Count], MAX(Id) AS [Max], MIN(Id) AS [Min] FROM dbo.myData;

GO

PRINT 'Deleting rows 1 to 200000';

DELETE FROM dbo.myData WHERE Id < 200000;

GO

SELECT COUNT(*) AS [Count], MAX(Id) AS [Max], MIN(Id) AS [Min] FROM dbo.myData;

PRINT 'now we do have an initial unused ''identity range''';

GO

PRINT 'Move rows to a temp table';

SELECT * INTO dbo.tempTable FROM dbo.myData;

PRINT 'truncating the original myData table';

PRINT 'this will actually reseed the identity value as well';

TRUNCATE TABLE dbo.myData;

SELECT COUNT(*) AS [Count], MAX(Id) AS [Max], MIN(Id) AS [Min] FROM dbo.myData;

GO

PRINT 'move data again to the original table, but this time omitting the Id col';

INSERT INTO dbo.myData (Data) SELECT Data FROM dbo.tempTable ORDER BY Id;

SELECT COUNT(*) AS [Count], MAX(Id) AS [Max], MIN(Id) AS [Min] FROM dbo.myData;

GO

PRINT 'deleting temp table';

DROP TABLE dbo.tempTable;

GO

PRINT 'final clean up';

DROP TABLE dbo.myData;

--<--

this will add some data

Count Max Min

-- -- --

524288 524288 1

Deleting rows 1 to 200000

Count Max Min

-- -- --

324289 524288 200000

now we do have an initial unused 'identity range'

Move rows to a temp table

truncating the original myData table

this will actually reseed the identity value as well

Count Max Min

-- -- --

0 NULL NULL

move data again to the original table, but this time omitting the Id col

Count Max Min

-- -- --

324289 324289 1

deleting temp table

final clean up

but this is usually not an efficient task you should do in production systems as, after all, key values are not interesting to human beings when autogenerated... you usually choose such a primary key for performance reasons or when you can not find an actual natural key in your data modelling desing phase (very bad ), so the actual value is not that big deal at all,, you just need it non repetetive and you get it as expected...

regards

|||

Thank you, Sir.

Yes, it did work and in the process I've learned more about SQL Express and T-SQ, important for a relative newbieL. Not only the original question I asked, but a seperate isuue I wasn't aware I was messing up until I kept getting an error message from the SELECT phrase in the second INSERT statement. I was insisting on putting parens around the column name list and SELECT didn't like that. When I ceased and desited, everything went smoothly. I initially tried this on the smaller of the two existing tables and each time it bombed out, it actually added to the PRIMARY IDENTITY Key. Since I expect more blanks from delete and insert statements as I develop the C# access code, that's not important now and I can fix it later. Reseeding the second table (the big one) went through without a hitch once I knew the right syntax. I expect that I'll need to write a script or something to do this and similar cleanup as I develop this app since I have more big tables to add with similar situations. Hopefully, I won't need to mess with it anymore when the app coding job's done, but at least I know how to do it now.

I'll take your advice on the autogenerated key into account and redesign accordingly. One of the two current tables I can change; the other not based on expected content.

Thanks for the help. I think I'm off and running again

Carl

|||

hi Carl,

Speedo wrote:

....

I'll take your advice on the autogenerated key into account and redesign accordingly. One of the two current tables I can change; the other not based on expected content.

wait before redesigning... I did not say that autogenearated keys are bad ..

they are just another (available) candidate key in our entity definition.. it should not be the only one, but such a candidate key can be good primary key, as it's very compact (and this is very usefull in relation where the referencing table must map to the referenced table) and this is a "nice to have" feature in indexes implementation...

very often database architechts implement a surrogate key in the design phase.. sometime these surrogate keys become primary keys and sometime not...

a nice article about nautural vs surrogate keys is available at http://www.informit.com/articles/article.asp?p=25862&redir=1&rl=1 , even if related to GUID columns..

regards

Primary Key Question

I have been developing a .NET Web app with an SQL Server 2005 Express database. Since I've been testing insert and delete code with a large data source, the Primary Key column which is also an autoincrement integer column is now at very large numbers (starting at over 40,000 now). I've tried doing a shrink on the database, but other then reducing the database size, it has done nothing to reduce the Primary Key numbers. Is there a command way to reduce the starting number back to 1 or do I need to completely reconstruct the database from scratch?

TIA

hi,

in order to "reset" the identity column value, you can "truncate" the table, that will implicitely reset the aut generated identity to your initial value via the
TRUNCATE TABLE schema.object;
statement... you will need high permissions on the object itself, and the table must not be referenced by a foreign key, ...
you can read further about requirements and permissions at http://msdn2.microsoft.com/en-us/library/ms177570.aspx...

or you can execute a
DBCC CHECKIDENT ('schema.object', RESEED);
command... start reading http://msdn2.microsoft.com/en-us/library/ms176057.aspx for further info and requirements..

regards

|||

Thanks - I'll give it a try.

Carl

|||

No dice. Neither of those options does it. That I want is, for example, a way of changing the lowest Primary/Identity Key value from 40,000 to 1. There are no records at lower values than 40,000. I'd also like to move all the Primary/Identity Key values down as well.

I suspect I need to effectively copy all the non-Primary/Identity Key data into a newly created database. If I do that, I think it is a characteristic of the INSERT statement that the Primary/Identity Key values will start at the seed values and autoincrement from there as rows are inserted and the incoming Primary/Identity Keys are ignored. I can then delete the original database and rename the new one to the original name.

Sound right?

Carl

|||

hi,

ok. you want to "re-assign" an autogenerated value starting from "1" to existing rows...

you actually do not have to "drop" the database...

just perform a "SELECT ... INTO" another table that will be created on the fly for you, as a temporary storage... then drop all rows from the "real" table and reset the identity value and repopulate the original table from the temp storage...

I mean something like

SET NOCOUNT ON; USE tempdb; GO CREATE TABLE dbo.myData( Id int NOT NULL IDENTITY PRIMARY KEY, Data varchar(10) DEFAULT 'test data' ); GO PRINT 'this will add some data'; DECLARE @.i int; SET @.i = 1; INSERT INTO dbo.myData VALUES ( DEFAULT ); WHILE @.i < 20 BEGIN INSERT INTO dbo.myData SELECT Data FROM dbo.myData; SET @.i = @.i + 1; END; GO SELECT COUNT(*) AS [Count], MAX(Id) AS [Max], MIN(Id) AS [Min] FROM dbo.myData; GO PRINT 'Deleting rows 1 to 200000'; DELETE FROM dbo.myData WHERE Id < 200000; GO SELECT COUNT(*) AS [Count], MAX(Id) AS [Max], MIN(Id) AS [Min] FROM dbo.myData; PRINT 'now we do have an initial unused ''identity range'''; GO PRINT 'Move rows to a temp table'; SELECT * INTO dbo.tempTable FROM dbo.myData; PRINT 'truncating the original myData table'; PRINT 'this will actually reseed the identity value as well'; TRUNCATE TABLE dbo.myData; SELECT COUNT(*) AS [Count], MAX(Id) AS [Max], MIN(Id) AS [Min] FROM dbo.myData; GO PRINT 'move data again to the original table, but this time omitting the Id col'; INSERT INTO dbo.myData (Data) SELECT Data FROM dbo.tempTable ORDER BY Id; SELECT COUNT(*) AS [Count], MAX(Id) AS [Max], MIN(Id) AS [Min] FROM dbo.myData; GO PRINT 'deleting temp table'; DROP TABLE dbo.tempTable; GO PRINT 'final clean up'; DROP TABLE dbo.myData; --<-- this will add some data Count Max Min -- -- -- 524288 524288 1 Deleting rows 1 to 200000 Count Max Min -- -- -- 324289 524288 200000 now we do have an initial unused 'identity range' Move rows to a temp table truncating the original myData table this will actually reseed the identity value as well Count Max Min -- -- -- 0 NULL NULL move data again to the original table, but this time omitting the Id col Count Max Min -- -- -- 324289 324289 1 deleting temp table final clean up

but this is usually not an efficient task you should do in production systems as, after all, key values are not interesting to human beings when autogenerated... you usually choose such a primary key for performance reasons or when you can not find an actual natural key in your data modelling desing phase (very bad ), so the actual value is not that big deal at all,, you just need it non repetetive and you get it as expected...

regards

|||

Thank you, Sir.

Yes, it did work and in the process I've learned more about SQL Express and T-SQ, important for a relative newbieL. Not only the original question I asked, but a seperate isuue I wasn't aware I was messing up until I kept getting an error message from the SELECT phrase in the second INSERT statement. I was insisting on putting parens around the column name list and SELECT didn't like that. When I ceased and desited, everything went smoothly. I initially tried this on the smaller of the two existing tables and each time it bombed out, it actually added to the PRIMARY IDENTITY Key. Since I expect more blanks from delete and insert statements as I develop the C# access code, that's not important now and I can fix it later. Reseeding the second table (the big one) went through without a hitch once I knew the right syntax. I expect that I'll need to write a script or something to do this and similar cleanup as I develop this app since I have more big tables to add with similar situations. Hopefully, I won't need to mess with it anymore when the app coding job's done, but at least I know how to do it now.

I'll take your advice on the autogenerated key into account and redesign accordingly. One of the two current tables I can change; the other not based on expected content.

Thanks for the help. I think I'm off and running again

Carl

|||

hi Carl,

Speedo wrote:

....

I'll take your advice on the autogenerated key into account and redesign accordingly. One of the two current tables I can change; the other not based on expected content.

wait before redesigning... I did not say that autogenearated keys are bad ..

they are just another (available) candidate key in our entity definition.. it should not be the only one, but such a candidate key can be good primary key, as it's very compact (and this is very usefull in relation where the referencing table must map to the referenced table) and this is a "nice to have" feature in indexes implementation...

very often database architechts implement a surrogate key in the design phase.. sometime these surrogate keys become primary keys and sometime not...

a nice article about nautural vs surrogate keys is available at http://www.informit.com/articles/article.asp?p=25862&redir=1&rl=1 , even if related to GUID columns..

regards

sql

Friday, March 9, 2012

Primary filegroup

Hi
I have a database with 3 datafiles :
file1 => filegroup primary
file2 => filegroup primary
file3 => filegroup index_data
Now I want to delete the file2. Is that possible ? I get the message that
the file is not empty. How can I determine what data is in the file ?
Regards
Gene
You need to empty it first using DBCC SHRINKFILE with the EMPTYFILE option.
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"Gene Vangampelaere" <gene.vangampelaere.remove.this@.infohos.remove.thi s.be> wrote in message
news:e5GdaAfVFHA.3716@.TK2MSFTNGP12.phx.gbl...
> Hi
> I have a database with 3 datafiles :
> file1 => filegroup primary
> file2 => filegroup primary
> file3 => filegroup index_data
> Now I want to delete the file2. Is that possible ? I get the message that the file is not empty.
> How can I determine what data is in the file ?
>
> Regards
> Gene
>
|||thx !!!
"Tibor Karaszi" <tibor_please.no.email_karaszi@.hotmail.nomail.com> schreef
in bericht news:OyG65IfVFHA.1200@.TK2MSFTNGP14.phx.gbl...
> You need to empty it first using DBCC SHRINKFILE with the EMPTYFILE
> option.
> --
> Tibor Karaszi, SQL Server MVP
> http://www.karaszi.com/sqlserver/default.asp
> http://www.solidqualitylearning.com/
>
> "Gene Vangampelaere"
> <gene.vangampelaere.remove.this@.infohos.remove.thi s.be> wrote in message
> news:e5GdaAfVFHA.3716@.TK2MSFTNGP12.phx.gbl...
>
|||Hi,
To do this, use the DBCC SHRINKFILE with EMPTY FILE option for the NDF file
This will empty the file and mark it as unavailable. After this use ALTER
DATABASE.. REMOVE FILE option to remove the NDF file.
Steps:
1. Take a database backup
2. Use database_name
go
DBCCSHRINKFILE('NDFFILE_name',X'EMPTYFILE')
3. Use ALTER database to Remove the file
ALTER DATABASE <DBNAME> REMOVE FILE 'logical ndf file name'
Thanks
Hari
SQL Server MVP
"Gene Vangampelaere" <gene.vangampelaere.remove.this@.infohos.remove.thi s.be>
wrote in message news:e5GdaAfVFHA.3716@.TK2MSFTNGP12.phx.gbl...
> Hi
> I have a database with 3 datafiles :
> file1 => filegroup primary
> file2 => filegroup primary
> file3 => filegroup index_data
> Now I want to delete the file2. Is that possible ? I get the message that
> the file is not empty. How can I determine what data is in the file ?
>
> Regards
> Gene
>

Primary filegroup

Hi
I have a database with 3 datafiles :
file1 => filegroup primary
file2 => filegroup primary
file3 => filegroup index_data
Now I want to delete the file2. Is that possible ' I get the message that
the file is not empty. How can I determine what data is in the file ?
Regards
GeneYou need to empty it first using DBCC SHRINKFILE with the EMPTYFILE option.
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"Gene Vangampelaere" <gene.vangampelaere.remove.this@.infohos.remove.this.be>
wrote in message
news:e5GdaAfVFHA.3716@.TK2MSFTNGP12.phx.gbl...
> Hi
> I have a database with 3 datafiles :
> file1 => filegroup primary
> file2 => filegroup primary
> file3 => filegroup index_data
> Now I want to delete the file2. Is that possible ' I get the message that
the file is not empty.
> How can I determine what data is in the file ?
>
> Regards
> Gene
>|||thx !!!
"Tibor Karaszi" <tibor_please.no.email_karaszi@.hotmail.nomail.com> schreef
in bericht news:OyG65IfVFHA.1200@.TK2MSFTNGP14.phx.gbl...
> You need to empty it first using DBCC SHRINKFILE with the EMPTYFILE
> option.
> --
> Tibor Karaszi, SQL Server MVP
> http://www.karaszi.com/sqlserver/default.asp
> http://www.solidqualitylearning.com/
>
> "Gene Vangampelaere"
> <gene.vangampelaere.remove.this@.infohos.remove.this.be> wrote in message
> news:e5GdaAfVFHA.3716@.TK2MSFTNGP12.phx.gbl...
>|||Hi,
To do this, use the DBCC SHRINKFILE with EMPTY FILE option for the NDF file
This will empty the file and mark it as unavailable. After this use ALTER
DATABASE.. REMOVE FILE option to remove the NDF file.
Steps:
1. Take a database backup
2. Use database_name
go
DBCCSHRINKFILE('NDFFILE_name',_'EMPTYFIL
E')
3. Use ALTER database to Remove the file
ALTER DATABASE <DBNAME> REMOVE FILE 'logical ndf file name'
Thanks
Hari
SQL Server MVP
"Gene Vangampelaere" <gene.vangampelaere.remove.this@.infohos.remove.this.be>
wrote in message news:e5GdaAfVFHA.3716@.TK2MSFTNGP12.phx.gbl...
> Hi
> I have a database with 3 datafiles :
> file1 => filegroup primary
> file2 => filegroup primary
> file3 => filegroup index_data
> Now I want to delete the file2. Is that possible ' I get the message that
> the file is not empty. How can I determine what data is in the file ?
>
> Regards
> Gene
>

primary file group run out of free space??

Hi all
I got the following error massage when tried to delete some 3 GB records
from a 26 GB records table - 'tblName':
'Could not allocate space for object 'tblName' in database 'dbName' because
the primary
filegroup is full'.
At the time of this error there were some 4 GB free disk space on the disk
where the datababse data and log file were.
Also these files were not limitted in grow size.
My questions are:
1) why does a 'delete' operation involves allocation of space for the table
that is being deleted'
Does this operation involves creating image data for the table that is being
deleted, in the transaction log, where the deletion actually occures and
than commited back to the original table'
So if i am deleting data from a 26GB table, there should be this amount of
free space on the disk where the log's file group is located?
2) Where can i see the state of the primary filegroup and how can i increase
its size?
Thanks for your attention
ReaRea
delete is a logged operation , so have you seen the log file during the
deletion?
SET ROWCOUNT 1000
WHILE 1 = 1
BEGIN
--Perfrom your DELETION (TRUNCATION would be more efficient)
IF @.@.ROWCOUNT = 0
BEGIN
BREAK
END
ELSE
BEGIN
CHECKPOINT
END
END
SET ROWCOUNT 0
"Rea Peleg" <rea_p@.afek.co.il> wrote in message
news:%23ZeKwl$YEHA.2016@.TK2MSFTNGP09.phx.gbl...
> Hi all
> I got the following error massage when tried to delete some 3 GB records
> from a 26 GB records table - 'tblName':
> 'Could not allocate space for object 'tblName' in database 'dbName'
because
> the primary
> filegroup is full'.
> At the time of this error there were some 4 GB free disk space on the disk
> where the datababse data and log file were.
> Also these files were not limitted in grow size.
> My questions are:
> 1) why does a 'delete' operation involves allocation of space for the
table
> that is being deleted'
> Does this operation involves creating image data for the table that is
being
> deleted, in the transaction log, where the deletion actually occures and
> than commited back to the original table'
> So if i am deleting data from a 26GB table, there should be this amount of
> free space on the disk where the log's file group is located?
> 2) Where can i see the state of the primary filegroup and how can i
increase
> its size?
>
> Thanks for your attention
> Rea
>|||Thanks alot!
So how much disk space should a deletion of 3 GB from a 26GB table consume'
Is there a way to estimate the amount of disk space deletion operations
consume from
the transaction log's disk'
2) in your code below: what is the edvantage of doing deletions this way'
Thanks again
Rea
"Uri Dimant" <urid@.iscar.co.il> wrote in message
news:e8FMLCAZEHA.3564@.TK2MSFTNGP11.phx.gbl...
> Rea
> delete is a logged operation , so have you seen the log file during the
> deletion?
> SET ROWCOUNT 1000
> WHILE 1 = 1
> BEGIN
> --Perfrom your DELETION (TRUNCATION would be more efficient)
> IF @.@.ROWCOUNT = 0
> BEGIN
> BREAK
> END
> ELSE
> BEGIN
> CHECKPOINT
> END
> END
> SET ROWCOUNT 0
> "Rea Peleg" <rea_p@.afek.co.il> wrote in message
> news:%23ZeKwl$YEHA.2016@.TK2MSFTNGP09.phx.gbl...
> because
disk[vbcol=seagreen]
> table
> being
of[vbcol=seagreen]
> increase
>|||Rea
I divide a long/big transaction into a small one.
CHECKPOINT flows a data from transaction log into the disk to remove an
inactive portions (btw you can also perform BACKUP LOG operation)
With that way you don't lock others by running your big deletion and also
keep a log file with an appropriate size.
"Rea Peleg" <rea_p@.afek.co.il> wrote in message
news:OPOU01AZEHA.3716@.TK2MSFTNGP11.phx.gbl...
> Thanks alot!
> So how much disk space should a deletion of 3 GB from a 26GB table
consume'
> Is there a way to estimate the amount of disk space deletion operations
> consume from
> the transaction log's disk'
> 2) in your code below: what is the edvantage of doing deletions this way'
> Thanks again
> Rea
> "Uri Dimant" <urid@.iscar.co.il> wrote in message
> news:e8FMLCAZEHA.3564@.TK2MSFTNGP11.phx.gbl...
records[vbcol=seagreen]
> disk
and[vbcol=seagreen]
amount[vbcol=seagreen]
> of
>

primary file group run out of free space??

Hi all
I got the following error massage when tried to delete some 3 GB records
from a 26 GB records table - 'tblName':
'Could not allocate space for object 'tblName' in database 'dbName' because
the primary
filegroup is full'.
At the time of this error there were some 4 GB free disk space on the disk
where the datababse data and log file were.
Also these files were not limitted in grow size.
My questions are:
1) why does a 'delete' operation involves allocation of space for the table
that is being deleted'
Does this operation involves creating image data for the table that is being
deleted, in the transaction log, where the deletion actually occures and
than commited back to the original table'
So if i am deleting data from a 26GB table, there should be this amount of
free space on the disk where the log's file group is located?
2) Where can i see the state of the primary filegroup and how can i increase
its size?
Thanks for your attention
ReaRea
delete is a logged operation , so have you seen the log file during the
deletion?
SET ROWCOUNT 1000
WHILE 1 = 1
BEGIN
--Perfrom your DELETION (TRUNCATION would be more efficient)
IF @.@.ROWCOUNT = 0
BEGIN
BREAK
END
ELSE
BEGIN
CHECKPOINT
END
END
SET ROWCOUNT 0
"Rea Peleg" <rea_p@.afek.co.il> wrote in message
news:%23ZeKwl$YEHA.2016@.TK2MSFTNGP09.phx.gbl...
> Hi all
> I got the following error massage when tried to delete some 3 GB records
> from a 26 GB records table - 'tblName':
> 'Could not allocate space for object 'tblName' in database 'dbName'
because
> the primary
> filegroup is full'.
> At the time of this error there were some 4 GB free disk space on the disk
> where the datababse data and log file were.
> Also these files were not limitted in grow size.
> My questions are:
> 1) why does a 'delete' operation involves allocation of space for the
table
> that is being deleted'
> Does this operation involves creating image data for the table that is
being
> deleted, in the transaction log, where the deletion actually occures and
> than commited back to the original table'
> So if i am deleting data from a 26GB table, there should be this amount of
> free space on the disk where the log's file group is located?
> 2) Where can i see the state of the primary filegroup and how can i
increase
> its size?
>
> Thanks for your attention
> Rea
>|||Thanks alot!
So how much disk space should a deletion of 3 GB from a 26GB table consume'
Is there a way to estimate the amount of disk space deletion operations
consume from
the transaction log's disk'
2) in your code below: what is the edvantage of doing deletions this way'
Thanks again
Rea
"Uri Dimant" <urid@.iscar.co.il> wrote in message
news:e8FMLCAZEHA.3564@.TK2MSFTNGP11.phx.gbl...
> Rea
> delete is a logged operation , so have you seen the log file during the
> deletion?
> SET ROWCOUNT 1000
> WHILE 1 = 1
> BEGIN
> --Perfrom your DELETION (TRUNCATION would be more efficient)
> IF @.@.ROWCOUNT = 0
> BEGIN
> BREAK
> END
> ELSE
> BEGIN
> CHECKPOINT
> END
> END
> SET ROWCOUNT 0
> "Rea Peleg" <rea_p@.afek.co.il> wrote in message
> news:%23ZeKwl$YEHA.2016@.TK2MSFTNGP09.phx.gbl...
> > Hi all
> > I got the following error massage when tried to delete some 3 GB records
> > from a 26 GB records table - 'tblName':
> >
> > 'Could not allocate space for object 'tblName' in database 'dbName'
> because
> > the primary
> > filegroup is full'.
> >
> > At the time of this error there were some 4 GB free disk space on the
disk
> > where the datababse data and log file were.
> > Also these files were not limitted in grow size.
> >
> > My questions are:
> > 1) why does a 'delete' operation involves allocation of space for the
> table
> > that is being deleted'
> > Does this operation involves creating image data for the table that is
> being
> > deleted, in the transaction log, where the deletion actually occures and
> > than commited back to the original table'
> > So if i am deleting data from a 26GB table, there should be this amount
of
> > free space on the disk where the log's file group is located?
> >
> > 2) Where can i see the state of the primary filegroup and how can i
> increase
> > its size?
> >
> >
> > Thanks for your attention
> > Rea
> >
> >
>|||Rea
I divide a long/big transaction into a small one.
CHECKPOINT flows a data from transaction log into the disk to remove an
inactive portions (btw you can also perform BACKUP LOG operation)
With that way you don't lock others by running your big deletion and also
keep a log file with an appropriate size.
"Rea Peleg" <rea_p@.afek.co.il> wrote in message
news:OPOU01AZEHA.3716@.TK2MSFTNGP11.phx.gbl...
> Thanks alot!
> So how much disk space should a deletion of 3 GB from a 26GB table
consume'
> Is there a way to estimate the amount of disk space deletion operations
> consume from
> the transaction log's disk'
> 2) in your code below: what is the edvantage of doing deletions this way'
> Thanks again
> Rea
> "Uri Dimant" <urid@.iscar.co.il> wrote in message
> news:e8FMLCAZEHA.3564@.TK2MSFTNGP11.phx.gbl...
> > Rea
> > delete is a logged operation , so have you seen the log file during the
> > deletion?
> >
> > SET ROWCOUNT 1000
> > WHILE 1 = 1
> > BEGIN
> > --Perfrom your DELETION (TRUNCATION would be more efficient)
> > IF @.@.ROWCOUNT = 0
> > BEGIN
> > BREAK
> > END
> > ELSE
> > BEGIN
> >
> > CHECKPOINT
> > END
> > END
> >
> > SET ROWCOUNT 0
> > "Rea Peleg" <rea_p@.afek.co.il> wrote in message
> > news:%23ZeKwl$YEHA.2016@.TK2MSFTNGP09.phx.gbl...
> > > Hi all
> > > I got the following error massage when tried to delete some 3 GB
records
> > > from a 26 GB records table - 'tblName':
> > >
> > > 'Could not allocate space for object 'tblName' in database 'dbName'
> > because
> > > the primary
> > > filegroup is full'.
> > >
> > > At the time of this error there were some 4 GB free disk space on the
> disk
> > > where the datababse data and log file were.
> > > Also these files were not limitted in grow size.
> > >
> > > My questions are:
> > > 1) why does a 'delete' operation involves allocation of space for the
> > table
> > > that is being deleted'
> > > Does this operation involves creating image data for the table that is
> > being
> > > deleted, in the transaction log, where the deletion actually occures
and
> > > than commited back to the original table'
> > > So if i am deleting data from a 26GB table, there should be this
amount
> of
> > > free space on the disk where the log's file group is located?
> > >
> > > 2) Where can i see the state of the primary filegroup and how can i
> > increase
> > > its size?
> > >
> > >
> > > Thanks for your attention
> > > Rea
> > >
> > >
> >
> >
>

Saturday, February 25, 2012

Previous machine name in Profiler

I changed the machine name yesterday (from Machine1 to Server1).
I'd delete the SQL server registration in Enterprise Manager and
register again with the new server name - Server1.
Everything run correctly.
But when I open the Profiler, create a new trace, I use (local) when
"Connect to SQL Server" prompted. After I click "OK", I saw my old
machine name "Machine1" in "Trace SQL Server" field under the "General"
tab. Everytime I have to change that server name to my new machine name
in order to use the profiler.
Does anyone knows how to change that default to my new machine name?
(It didn't run when I use the old machine name)
Thanks.
Peter CCH
Check out Client/Server Network Utility the alias to be changed
"Peter CCH" <petercch.wodoy@.gmail.com> wrote in message
news:1140415112.093022.101800@.o13g2000cwo.googlegr oups.com...
>I changed the machine name yesterday (from Machine1 to Server1).
> I'd delete the SQL server registration in Enterprise Manager and
> register again with the new server name - Server1.
> Everything run correctly.
> But when I open the Profiler, create a new trace, I use (local) when
> "Connect to SQL Server" prompted. After I click "OK", I saw my old
> machine name "Machine1" in "Trace SQL Server" field under the "General"
> tab. Everytime I have to change that server name to my new machine name
> in order to use the profiler.
> Does anyone knows how to change that default to my new machine name?
> (It didn't run when I use the old machine name)
> Thanks.
>
> Peter CCH
>
|||Hi Peter
Did you follow: http://support.microsoft.com/default...b;en-us;303774
John
"Peter CCH" wrote:

> I changed the machine name yesterday (from Machine1 to Server1).
> I'd delete the SQL server registration in Enterprise Manager and
> register again with the new server name - Server1.
> Everything run correctly.
> But when I open the Profiler, create a new trace, I use (local) when
> "Connect to SQL Server" prompted. After I click "OK", I saw my old
> machine name "Machine1" in "Trace SQL Server" field under the "General"
> tab. Everytime I have to change that server name to my new machine name
> in order to use the profiler.
> Does anyone knows how to change that default to my new machine name?
> (It didn't run when I use the old machine name)
> Thanks.
>
> Peter CCH
>
|||I tried:
sp_dropserver 'Machine1'
go
sp_addserver 'Server1', 'local'
go
But the problem still remain.
I tried to add in alias under Client/Network Utility, but still the
same, problem remain.
Peter CCH
|||OK, got it.
Have to restart the SQL Server in order to take effect.
Thanks.
Peter CCH

Previous machine name in Profiler

I changed the machine name yesterday (from Machine1 to Server1).
I'd delete the SQL server registration in Enterprise Manager and
register again with the new server name - Server1.
Everything run correctly.
But when I open the Profiler, create a new trace, I use (local) when
"Connect to SQL Server" prompted. After I click "OK", I saw my old
machine name "Machine1" in "Trace SQL Server" field under the "General"
tab. Everytime I have to change that server name to my new machine name
in order to use the profiler.
Does anyone knows how to change that default to my new machine name?
(It didn't run when I use the old machine name)
Thanks.
Peter CCHCheck out Client/Server Network Utility the alias to be changed
"Peter CCH" <petercch.wodoy@.gmail.com> wrote in message
news:1140415112.093022.101800@.o13g2000cwo.googlegroups.com...
>I changed the machine name yesterday (from Machine1 to Server1).
> I'd delete the SQL server registration in Enterprise Manager and
> register again with the new server name - Server1.
> Everything run correctly.
> But when I open the Profiler, create a new trace, I use (local) when
> "Connect to SQL Server" prompted. After I click "OK", I saw my old
> machine name "Machine1" in "Trace SQL Server" field under the "General"
> tab. Everytime I have to change that server name to my new machine name
> in order to use the profiler.
> Does anyone knows how to change that default to my new machine name?
> (It didn't run when I use the old machine name)
> Thanks.
>
> Peter CCH
>|||Hi Peter
Did you follow: [url]http://support.microsoft.com/default.aspx?scid=kb;en-us;303774[/ur
l]
John
"Peter CCH" wrote:

> I changed the machine name yesterday (from Machine1 to Server1).
> I'd delete the SQL server registration in Enterprise Manager and
> register again with the new server name - Server1.
> Everything run correctly.
> But when I open the Profiler, create a new trace, I use (local) when
> "Connect to SQL Server" prompted. After I click "OK", I saw my old
> machine name "Machine1" in "Trace SQL Server" field under the "General"
> tab. Everytime I have to change that server name to my new machine name
> in order to use the profiler.
> Does anyone knows how to change that default to my new machine name?
> (It didn't run when I use the old machine name)
> Thanks.
>
> Peter CCH
>|||I tried:
---
sp_dropserver 'Machine1'
go
sp_addserver 'Server1', 'local'
go
---
But the problem still remain.
I tried to add in alias under Client/Network Utility, but still the
same, problem remain.
Peter CCH|||OK, got it.
Have to restart the SQL Server in order to take effect.
Thanks.
Peter CCH

Previous machine name in Profiler

I changed the machine name yesterday (from Machine1 to Server1).
I'd delete the SQL server registration in Enterprise Manager and
register again with the new server name - Server1.
Everything run correctly.
But when I open the Profiler, create a new trace, I use (local) when
"Connect to SQL Server" prompted. After I click "OK", I saw my old
machine name "Machine1" in "Trace SQL Server" field under the "General"
tab. Everytime I have to change that server name to my new machine name
in order to use the profiler.
Does anyone knows how to change that default to my new machine name?
(It didn't run when I use the old machine name)
Thanks.
Peter CCHCheck out Client/Server Network Utility the alias to be changed
"Peter CCH" <petercch.wodoy@.gmail.com> wrote in message
news:1140415112.093022.101800@.o13g2000cwo.googlegroups.com...
>I changed the machine name yesterday (from Machine1 to Server1).
> I'd delete the SQL server registration in Enterprise Manager and
> register again with the new server name - Server1.
> Everything run correctly.
> But when I open the Profiler, create a new trace, I use (local) when
> "Connect to SQL Server" prompted. After I click "OK", I saw my old
> machine name "Machine1" in "Trace SQL Server" field under the "General"
> tab. Everytime I have to change that server name to my new machine name
> in order to use the profiler.
> Does anyone knows how to change that default to my new machine name?
> (It didn't run when I use the old machine name)
> Thanks.
>
> Peter CCH
>|||Hi Peter
Did you follow: http://support.microsoft.com/default.aspx?scid=kb;en-us;303774
John
"Peter CCH" wrote:
> I changed the machine name yesterday (from Machine1 to Server1).
> I'd delete the SQL server registration in Enterprise Manager and
> register again with the new server name - Server1.
> Everything run correctly.
> But when I open the Profiler, create a new trace, I use (local) when
> "Connect to SQL Server" prompted. After I click "OK", I saw my old
> machine name "Machine1" in "Trace SQL Server" field under the "General"
> tab. Everytime I have to change that server name to my new machine name
> in order to use the profiler.
> Does anyone knows how to change that default to my new machine name?
> (It didn't run when I use the old machine name)
> Thanks.
>
> Peter CCH
>|||I tried:
---
sp_dropserver 'Machine1'
go
sp_addserver 'Server1', 'local'
go
---
But the problem still remain.
I tried to add in alias under Client/Network Utility, but still the
same, problem remain.
Peter CCH|||OK, got it.
Have to restart the SQL Server in order to take effect.
Thanks.
Peter CCH

Monday, February 20, 2012

Preventing Deadlock

Trace 1204 has narrowed the cause of deadlock to these delete statements.
There are a total of 10 delete statements in this SP.
create procedure del_emp @.empid int
Delete from tbl_employees
where empid in (Select empid from tbl_location where empid = @.empid)
Delete from tbl_management
where mge_ID in (Select mge_ID from tbl_sunmanagement where empid = @.empid)
......
......
Questions:
1. should i add (nolock) within the select statement? ex: Select empid from
tbl_location WITH (NOLOCK)
2. should i add (updlock) within the delete statement? ex: Delete from
tbl_employees WITH (UPDLOCK)
Any suggestions highly appreciated.Hi
You don't say what is being blocked and what it causing the blocking! You
may be missing an index, which would help, check out the query plan to see if
this is so. Will the empid's in tbl_location be anything other than @.empid?
Do you explicitly start a transaction outside this procedure?
John
"morphius" wrote:
> Trace 1204 has narrowed the cause of deadlock to these delete statements.
> There are a total of 10 delete statements in this SP.
> create procedure del_emp @.empid int
> Delete from tbl_employees
> where empid in (Select empid from tbl_location where empid = @.empid)
> Delete from tbl_management
> where mge_ID in (Select mge_ID from tbl_sunmanagement where empid = @.empid)
> ......
> ......
> Questions:
> 1. should i add (nolock) within the select statement? ex: Select empid from
> tbl_location WITH (NOLOCK)
> 2. should i add (updlock) within the delete statement? ex: Delete from
> tbl_employees WITH (UPDLOCK)
> Any suggestions highly appreciated.
>
>|||Hi John,
Thanks for the reply. The empid in tbl_location is not anything other than
the empid. Yes a transaction is explicitly started outside this procedure and
between 100-300 users could be using the transaction simultaneously.
"John Bell" wrote:
> Hi
> You don't say what is being blocked and what it causing the blocking! You
> may be missing an index, which would help, check out the query plan to see if
> this is so. Will the empid's in tbl_location be anything other than @.empid?
> Do you explicitly start a transaction outside this procedure?
> John
> "morphius" wrote:
> > Trace 1204 has narrowed the cause of deadlock to these delete statements.
> > There are a total of 10 delete statements in this SP.
> >
> > create procedure del_emp @.empid int
> >
> > Delete from tbl_employees
> > where empid in (Select empid from tbl_location where empid = @.empid)
> >
> > Delete from tbl_management
> > where mge_ID in (Select mge_ID from tbl_sunmanagement where empid = @.empid)
> >
> > ......
> > ......
> >
> > Questions:
> > 1. should i add (nolock) within the select statement? ex: Select empid from
> > tbl_location WITH (NOLOCK)
> > 2. should i add (updlock) within the delete statement? ex: Delete from
> > tbl_employees WITH (UPDLOCK)
> >
> > Any suggestions highly appreciated.
> >
> >
> >
> >|||Hi
If I understand you correctly, then there is no need to query table location
and you could just use
Delete from tbl_employees
where empid = @.empid
or is was there a typo or misunderstanding?
If no other code is in the transaction other than the stored procedure call
(i.e. two delete statements) then you would need to look at the query plans.
John
"morphius" wrote:
> Hi John,
> Thanks for the reply. The empid in tbl_location is not anything other than
> the empid. Yes a transaction is explicitly started outside this procedure and
> between 100-300 users could be using the transaction simultaneously.
> "John Bell" wrote:
> > Hi
> >
> > You don't say what is being blocked and what it causing the blocking! You
> > may be missing an index, which would help, check out the query plan to see if
> > this is so. Will the empid's in tbl_location be anything other than @.empid?
> > Do you explicitly start a transaction outside this procedure?
> >
> > John
> >
> > "morphius" wrote:
> >
> > > Trace 1204 has narrowed the cause of deadlock to these delete statements.
> > > There are a total of 10 delete statements in this SP.
> > >
> > > create procedure del_emp @.empid int
> > >
> > > Delete from tbl_employees
> > > where empid in (Select empid from tbl_location where empid = @.empid)
> > >
> > > Delete from tbl_management
> > > where mge_ID in (Select mge_ID from tbl_sunmanagement where empid = @.empid)
> > >
> > > ......
> > > ......
> > >
> > > Questions:
> > > 1. should i add (nolock) within the select statement? ex: Select empid from
> > > tbl_location WITH (NOLOCK)
> > > 2. should i add (updlock) within the delete statement? ex: Delete from
> > > tbl_employees WITH (UPDLOCK)
> > >
> > > Any suggestions highly appreciated.
> > >
> > >
> > >
> > >