Showing posts with label recordsand. Show all posts
Showing posts with label recordsand. Show all posts

Sunday, March 11, 2012

DELETING 100 million from a table weekly SQl SERVER 2000

DELETING 100 million from a table wly SQl SERVER 2000
Hi All
We have a table in SQL SERVER 2000 which has about 250 million records
and this will be growing by 100 million every w. At a time the table
should contain just 13 ws of data. when the 14th w data needs to
be loaded the first w's data has to be deleted.
And this deletes 100 million every w, since the delete is taking lot
of transaction log space the job is not successful.
Can you please help with what are the approaches we can take to fix
this problem?
Performance and transaction log are the issues we are facing. We tried
deletion in steps too but that also is taking time. What are the
different ways we can address this quickly.
Please reply at the earliest.
Thanks
HarishHi Harish,
You should look at partitioning, keep a cycle the partitions and simply
CREATE TABLE and DROP TABLE the new partitions, that way you won't have to
do any logging.
Tony
--
Tony Rogerson
SQL Server MVP
http://sqlserverfaq.com - free video tutorials
"harish" <harish.prabhala@.gmail.com> wrote in message
news:1131162393.816615.122850@.f14g2000cwb.googlegroups.com...
> DELETING 100 million from a table wly SQl SERVER 2000
> Hi All
> We have a table in SQL SERVER 2000 which has about 250 million records
> and this will be growing by 100 million every w. At a time the table
> should contain just 13 ws of data. when the 14th w data needs to
> be loaded the first w's data has to be deleted.
> And this deletes 100 million every w, since the delete is taking lot
> of transaction log space the job is not successful.
> Can you please help with what are the approaches we can take to fix
> this problem?
> Performance and transaction log are the issues we are facing. We tried
> deletion in steps too but that also is taking time. What are the
> different ways we can address this quickly.
> Please reply at the earliest.
> Thanks
> Harish
>|||I agree with Tony in that SQL2005 gives you Partitioning which is great for
these type activities. But if you do the deletes in smaller batches you can
backup the log during the operation periodically to keep the tran log from
growing.
SET ROWCOUNT 10000
WHILE 1 = 1
BEGIN
DELETE FROM TABLE WHERE Col = xxx
IF @.@.ROWCOUNT = 0
BREAK
END
SET ROWCOUNT 0
Andrew J. Kelly SQL MVP
"harish" <harish.prabhala@.gmail.com> wrote in message
news:1131162393.816615.122850@.f14g2000cwb.googlegroups.com...
> DELETING 100 million from a table wly SQl SERVER 2000
> Hi All
> We have a table in SQL SERVER 2000 which has about 250 million records
> and this will be growing by 100 million every w. At a time the table
> should contain just 13 ws of data. when the 14th w data needs to
> be loaded the first w's data has to be deleted.
> And this deletes 100 million every w, since the delete is taking lot
> of transaction log space the job is not successful.
> Can you please help with what are the approaches we can take to fix
> this problem?
> Performance and transaction log are the issues we are facing. We tried
> deletion in steps too but that also is taking time. What are the
> different ways we can address this quickly.
> Please reply at the earliest.
> Thanks
> Harish
>|||Hey
Thanks. We have an index on four columns in this table. For Ex A, B, C
and D
The delete statement's where clause has the conditions for A, B and C
The delete statement's where clause has the conditions for just A
Which of the two's performance will be faster?
We tried something like this:
SET ROWCOUNT 5000;
WHILE 1 = 1
BEGIN
DELETE FROM T1 WHERE dt < '20030101' -- original delete
IF @.@.rowcount < 5000 BREAK;
END
SET ROWCOUNT 0;
1) Does this setting ROWCOUNT first sort the table and then delete?
2) The above query is executed to delete all records satisfying the
condition in steps of 5000 until the delete is comple.
How can I stop it after one 5000?|||On 5 Nov 2005 11:04:29 -0800, harish wrote:
(snip)
Hi Harish,
I just replied to the same question in another thread.
Could you please ask your questions in JUST ONE place, and in JUST ONE
group? I've seen your messages scattered over several groups, and
several different messages in just this group. Many of them have
attracted replies. It's very hard to keep track of what is going on in
all thesse threads, and it's a waste of other people's time if someone
posts a reply to you that you already had received in another group.
Best, Hugo
--
(Remove _NO_ and _SPAM_ to get my e-mail address)|||harish (harish.prabhala@.gmail.com) writes:
> Thanks. We have an index on four columns in this table. For Ex A, B, C
> and D
> The delete statement's where clause has the conditions for A, B and C
> The delete statement's where clause has the conditions for just A
>
> Which of the two's performance will be faster?
The one that uses the clustered index on the table. In fact, given
the number of rows you are to delete, it is essential that you use
the clustered index in your WHERE condition. Else you will lose on
all the time it takes to locate the rows.

> We tried something like this:
>
> SET ROWCOUNT 5000;
> WHILE 1 = 1
> BEGIN
> DELETE FROM T1 WHERE dt < '20030101' -- original delete
> IF @.@.rowcount < 5000 BREAK;
> END
> SET ROWCOUNT 0;
>
> 1) Does this setting ROWCOUNT first sort the table and then delete?
It will locate the rows by some means. If there is no good index,
this may lead to a scan of the table. But there should not be any
sorting, as there is no reason to sort the table.
By the way, 5000 rows at a time, is a far too low batch-size; 50000
is a minimum. Since you need to delete 100 million, I would even
try a million at a time.
Keep in mind that if you are running with full recovery, you still
need to backup the transaction log. Simple recovery may be a good
thing here.

> 2) The above query is executed to delete all records satisfying the
> condition in steps of 5000 until the delete is comple.
> How can I stop it after one 5000?
The red button in Query Analyzer?
I still think you should consider partitioned views with one view per
month, or one per ten days in a month. In this case deleting the work
for ten days is as easy:
1) Create a new table for the next period.
2) Alter the view to include the new table, and not include the
table with the data to go.
3) Drop the table and lose 130 millions rows instantly.
Check out partitioned views in Books Online.
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp

DELETING 100 million from a table weekly SQl SERVER 2000

DELETING 100 million from a table weekly SQl SERVER 2000
Hi All
We have a table in SQL SERVER 2000 which has about 250 million records
and this will be growing by 100 million every week. At a time the table
should contain just 13 weeks of data. when the 14th week data needs to
be loaded the first week's data has to be deleted.
And this deletes 100 million every week, since the delete is taking lot
of transaction log space the job is not successful.
Can you please help with what are the approaches we can take to fix
this problem?
Performance and transaction log are the issues we are facing. We tried
deletion in steps too but that also is taking time. What are the
different ways we can address this quickly.
Please reply at the earliest.
Thanks
Harish
You could try inserting the clean records i.e. Current 13 weeks data into a
a temporary table.
Then truncate the table with all data in (bear in mind any identity
columns).
Reinsert the clean records into the table.
Again, bear in mind any idientity columns if you need to keep the ids in
sync as truncate will reset the seed count, also, you can't truncate a table
with a FK so you'd need to work around that too.
Not the best method but may be quicker than your current process.
Immy
"harish" <harish.prabhala@.gmail.com> wrote in message
news:1131163435.343059.179520@.f14g2000cwb.googlegr oups.com...
> DELETING 100 million from a table weekly SQl SERVER 2000
> Hi All
> We have a table in SQL SERVER 2000 which has about 250 million records
> and this will be growing by 100 million every week. At a time the table
> should contain just 13 weeks of data. when the 14th week data needs to
> be loaded the first week's data has to be deleted.
> And this deletes 100 million every week, since the delete is taking lot
> of transaction log space the job is not successful.
> Can you please help with what are the approaches we can take to fix
> this problem?
> Performance and transaction log are the issues we are facing. We tried
> deletion in steps too but that also is taking time. What are the
> different ways we can address this quickly.
> Please reply at the earliest.
> Thanks
> Harish
>
|||> Performance and transaction log are the issues we are facing. We tried
> deletion in steps too but that also is taking time. What are the
> different ways we can address this quickly.
Deleting in smaller separate transaction batches will reduce transaction log
space requirements and improve performance You'll need to experiment to
determine the best batch size for your environment. In my experience, the
optimum size is somewhere around the amount of SQL Server memory. For
example, with 2GB RAM and a 100 byte row size, delete no more than 20M rows
at a time. If you are using the FULL are BULK_LOGGED recovery model, you'll
also need to backup the log between batches to keep the log size manageable.
Alternatively, you can use change to the SIMPLE model for the duration of
the delete script and change back to your normal recovery model afterward.
Don't forget to perform a full database backup following the change from
SIMPLE recovery.
Use the TABLOCKX hint if possible. Ideally, the table's clustered index
should be the column(s) used for your delete criteria.
Another option is to partition data based on your delete criteria (separate
table for each week). This will allow you to simply drop the table
containing the oldest data. The partitioning implementation can be made
transparent to applications by using a UNION ALL view. After dropping the
oldest table, you create a new table for the latest data and change the view
accordingly. If you can adhere to the rules for local partitioned views as
described in the Books Online, there are performance advantages with the
partitioned view and the view is updatable as well. See the example below.
CREATE TABLE OrderDetails_20051030
(
OrderID int NOT NULL,
ProductID int NOT NULL,
Quantity int NOT NULL,
UnitPrice decimal(9, 2) NOT NULL,
OrderDate smalldatetime NOT NULL
CONSTRAINT CK_OrderDetails_20051030
CHECK (OrderDate > '20051023' AND OrderDate <= '20051030')
)
ALTER TABLE OrderDetails_20051030
ADD CONSTRAINT PK_OrderDetails_20051030
PRIMARY KEY(OrderDate, OrderID, ProductID)
GO
CREATE TABLE OrderDetails_20051106
(
OrderID int NOT NULL,
ProductID int NOT NULL,
Quantity int NOT NULL,
UnitPrice decimal(9, 2) NOT NULL,
OrderDate smalldatetime NOT NULL
CONSTRAINT CK_OrderDetails_20051106
CHECK (OrderDate > '20051030' AND OrderDate <= '20051106')
)
ALTER TABLE OrderDetails_20051106
ADD CONSTRAINT PK_OrderDetailsOrderDetails_20051106
PRIMARY KEY(OrderDate, OrderID, ProductID)
GO
CREATE TABLE OrderDetails_20051113
(
OrderID int NOT NULL,
ProductID int NOT NULL,
Quantity int NOT NULL,
UnitPrice decimal(9, 2) NOT NULL,
OrderDate smalldatetime NOT NULL
CONSTRAINT CK_OrderDetails_20051113_OrderDate
CHECK (OrderDate > '20051106' AND OrderDate <= '20051113')
)
ALTER TABLE OrderDetails_20051113
ADD CONSTRAINT PK_OrderDetails_20051113
PRIMARY KEY(OrderDate, OrderID, ProductID)
GO
CREATE VIEW OrderDetails AS
SELECT
OrderID,
ProductID,
Quantity,
UnitPrice,
OrderDate
FROM OrderDetails_20051030
UNION ALL
SELECT
OrderID,
ProductID,
Quantity,
UnitPrice,
OrderDate
FROM OrderDetails_20051106
UNION ALL
SELECT
OrderID,
ProductID,
Quantity,
UnitPrice,
OrderDate
FROM OrderDetails_20051113
GO
INSERT INTO OrderDetails VALUES(1, 1, 1, 1, '20051030')
INSERT INTO OrderDetails VALUES(2, 1, 1, 1, '20051106')
INSERT INTO OrderDetails VALUES(3, 1, 1, 1, '20051113')
GO
--execution plan shows only OrderDetails_20051106 is accessed because
--all PK columns are referenced in this query
SELECT
a.OrderID,
a.ProductID,
a.Quantity,
a.UnitPrice,
a.OrderDate
FROM OrderDetails a
WHERE a.OrderDate = '20051106' AND OrderID = 2 AND ProductID = 1
GO
--to remove oldest week:
DROP VIEW OrderDetails
GO
--drop oldest table
DROP TABLE OrderDetails_20051030
GO
--create table for new data
CREATE TABLE OrderDetails_20051120
(
OrderID int NOT NULL,
ProductID int NOT NULL,
Quantity int NOT NULL,
UnitPrice decimal(9, 2) NOT NULL,
OrderDate smalldatetime NOT NULL
CONSTRAINT CK_OrderDetails_20051120_OrderDate
CHECK (OrderDate > '20051113' AND OrderDate <= '20051120')
)
ALTER TABLE OrderDetails_20051120
ADD CONSTRAINT PK_OrderDetails_20051120
PRIMARY KEY(OrderDate, OrderID, ProductID)
GO
--create view with current tables
CREATE VIEW OrderDetails AS
SELECT
OrderID,
ProductID,
Quantity,
UnitPrice,
OrderDate
FROM OrderDetails_20051106
UNION ALL
SELECT
OrderID,
ProductID,
Quantity,
UnitPrice,
OrderDate
FROM OrderDetails_20051113
UNION ALL
SELECT
OrderID,
ProductID,
Quantity,
UnitPrice,
OrderDate
FROM OrderDetails_20051120
GO
Hope this helps.
Dan Guzman
SQL Server MVP
"harish" <harish.prabhala@.gmail.com> wrote in message
news:1131163435.343059.179520@.f14g2000cwb.googlegr oups.com...
> DELETING 100 million from a table weekly SQl SERVER 2000
> Hi All
> We have a table in SQL SERVER 2000 which has about 250 million records
> and this will be growing by 100 million every week. At a time the table
> should contain just 13 weeks of data. when the 14th week data needs to
> be loaded the first week's data has to be deleted.
> And this deletes 100 million every week, since the delete is taking lot
> of transaction log space the job is not successful.
> Can you please help with what are the approaches we can take to fix
> this problem?
> Performance and transaction log are the issues we are facing. We tried
> deletion in steps too but that also is taking time. What are the
> different ways we can address this quickly.
> Please reply at the earliest.
> Thanks
> Harish
>
|||I forgot to mention that SQL 2005 Enterprise introduces new table and index
partitioning features that make it easier to partition large tables.
Hope this helps.
Dan Guzman
SQL Server MVP
"Dan Guzman" <guzmanda@.nospam-online.sbcglobal.net> wrote in message
news:%23$vpN7h4FHA.3000@.TK2MSFTNGP12.phx.gbl...
> Deleting in smaller separate transaction batches will reduce transaction
> log space requirements and improve performance You'll need to experiment
> to determine the best batch size for your environment. In my experience,
> the optimum size is somewhere around the amount of SQL Server memory. For
> example, with 2GB RAM and a 100 byte row size, delete no more than 20M
> rows at a time. If you are using the FULL are BULK_LOGGED recovery model,
> you'll also need to backup the log between batches to keep the log size
> manageable. Alternatively, you can use change to the SIMPLE model for the
> duration of the delete script and change back to your normal recovery
> model afterward. Don't forget to perform a full database backup following
> the change from SIMPLE recovery.
> Use the TABLOCKX hint if possible. Ideally, the table's clustered index
> should be the column(s) used for your delete criteria.
> Another option is to partition data based on your delete criteria
> (separate table for each week). This will allow you to simply drop the
> table containing the oldest data. The partitioning implementation can be
> made transparent to applications by using a UNION ALL view. After
> dropping the oldest table, you create a new table for the latest data and
> change the view accordingly. If you can adhere to the rules for local
> partitioned views as described in the Books Online, there are performance
> advantages with the partitioned view and the view is updatable as well.
> See the example below.
> CREATE TABLE OrderDetails_20051030
> (
> OrderID int NOT NULL,
> ProductID int NOT NULL,
> Quantity int NOT NULL,
> UnitPrice decimal(9, 2) NOT NULL,
> OrderDate smalldatetime NOT NULL
> CONSTRAINT CK_OrderDetails_20051030
> CHECK (OrderDate > '20051023' AND OrderDate <= '20051030')
> )
> ALTER TABLE OrderDetails_20051030
> ADD CONSTRAINT PK_OrderDetails_20051030
> PRIMARY KEY(OrderDate, OrderID, ProductID)
> GO
> CREATE TABLE OrderDetails_20051106
> (
> OrderID int NOT NULL,
> ProductID int NOT NULL,
> Quantity int NOT NULL,
> UnitPrice decimal(9, 2) NOT NULL,
> OrderDate smalldatetime NOT NULL
> CONSTRAINT CK_OrderDetails_20051106
> CHECK (OrderDate > '20051030' AND OrderDate <= '20051106')
> )
> ALTER TABLE OrderDetails_20051106
> ADD CONSTRAINT PK_OrderDetailsOrderDetails_20051106
> PRIMARY KEY(OrderDate, OrderID, ProductID)
> GO
> CREATE TABLE OrderDetails_20051113
> (
> OrderID int NOT NULL,
> ProductID int NOT NULL,
> Quantity int NOT NULL,
> UnitPrice decimal(9, 2) NOT NULL,
> OrderDate smalldatetime NOT NULL
> CONSTRAINT CK_OrderDetails_20051113_OrderDate
> CHECK (OrderDate > '20051106' AND OrderDate <= '20051113')
> )
> ALTER TABLE OrderDetails_20051113
> ADD CONSTRAINT PK_OrderDetails_20051113
> PRIMARY KEY(OrderDate, OrderID, ProductID)
> GO
> CREATE VIEW OrderDetails AS
> SELECT
> OrderID,
> ProductID,
> Quantity,
> UnitPrice,
> OrderDate
> FROM OrderDetails_20051030
> UNION ALL
> SELECT
> OrderID,
> ProductID,
> Quantity,
> UnitPrice,
> OrderDate
> FROM OrderDetails_20051106
> UNION ALL
> SELECT
> OrderID,
> ProductID,
> Quantity,
> UnitPrice,
> OrderDate
> FROM OrderDetails_20051113
> GO
> INSERT INTO OrderDetails VALUES(1, 1, 1, 1, '20051030')
> INSERT INTO OrderDetails VALUES(2, 1, 1, 1, '20051106')
> INSERT INTO OrderDetails VALUES(3, 1, 1, 1, '20051113')
> GO
> --execution plan shows only OrderDetails_20051106 is accessed because
> --all PK columns are referenced in this query
> SELECT
> a.OrderID,
> a.ProductID,
> a.Quantity,
> a.UnitPrice,
> a.OrderDate
> FROM OrderDetails a
> WHERE a.OrderDate = '20051106' AND OrderID = 2 AND ProductID = 1
> GO
> --to remove oldest week:
> DROP VIEW OrderDetails
> GO
> --drop oldest table
> DROP TABLE OrderDetails_20051030
> GO
> --create table for new data
> CREATE TABLE OrderDetails_20051120
> (
> OrderID int NOT NULL,
> ProductID int NOT NULL,
> Quantity int NOT NULL,
> UnitPrice decimal(9, 2) NOT NULL,
> OrderDate smalldatetime NOT NULL
> CONSTRAINT CK_OrderDetails_20051120_OrderDate
> CHECK (OrderDate > '20051113' AND OrderDate <= '20051120')
> )
> ALTER TABLE OrderDetails_20051120
> ADD CONSTRAINT PK_OrderDetails_20051120
> PRIMARY KEY(OrderDate, OrderID, ProductID)
> GO
> --create view with current tables
> CREATE VIEW OrderDetails AS
> SELECT
> OrderID,
> ProductID,
> Quantity,
> UnitPrice,
> OrderDate
> FROM OrderDetails_20051106
> UNION ALL
> SELECT
> OrderID,
> ProductID,
> Quantity,
> UnitPrice,
> OrderDate
> FROM OrderDetails_20051113
> UNION ALL
> SELECT
> OrderID,
> ProductID,
> Quantity,
> UnitPrice,
> OrderDate
> FROM OrderDetails_20051120
> GO
>
> --
> Hope this helps.
> Dan Guzman
> SQL Server MVP
> "harish" <harish.prabhala@.gmail.com> wrote in message
> news:1131163435.343059.179520@.f14g2000cwb.googlegr oups.com...
>
|||Hey
Thanks. We have an index on four columns in this table. For Ex A, B, C
and D
The delete statement's where clause has the conditions for A, B and C
The delete statement's where clause has the conditions for just A
Which of the two's performance will be faster?
We tried something like this:
SET ROWCOUNT 5000;
WHILE 1 = 1
BEGIN
DELETE FROM T1 WHERE dt < '20030101' -- original delete
IF @.@.rowcount < 5000 BREAK;
END
SET ROWCOUNT 0;
1) Does this setting ROWCOUNT first sort the table and then delete?
2) The above query is executed to delete all records satisfying the
condition in steps of 5000 until the delete is comple.
How can I stop it after one 5000?
|||On 5 Nov 2005 11:01:30 -0800, harish wrote:
(snip)
Hi Harish,
I just replied to the same question in another thread.
Could you please ask your questions in JUST ONE place, and in JUST ONE
group? I've seen your messages scattered over several groups, and
several different messages in just this group. Many of them have
attracted replies. It's very hard to keep track of what is going on in
all thesse threads, and it's a waste of other people's time if someone
posts a reply to you that you already had received in another group.
Best, Hugo
(Remove _NO_ and _SPAM_ to get my e-mail address)
|||Hi
Sorry for that. I was new to the group and not sure which group is
active.
Thanks a lot for the information
Regards
Harish

DELETING 100 million from a table weekly SQl SERVER 2000

DELETING 100 million from a table weekly SQl SERVER 2000

Hi All

We have a table in SQL SERVER 2000 which has about 250 million records
and this will be growing by 100 million every week. At a time the table
should contain just 13 weeks of data. when the 14th week data needs to
be loaded the first week's data has to be deleted.

And this deletes 100 million every week, since the delete is taking lot
of transaction log space the job is not successful.

Can you please help with what are the approaches we can take to fix
this problem?

Performance and transaction log are the issues we are facing. We tried
deletion in steps too but that also is taking time. What are the
different ways we can address this quickly.

Please reply at the earliest.

Thanks
HarishHi Harish,

You should look at partitioning, keep a cycle the partitions and simply
CREATE TABLE and DROP TABLE the new partitions, that way you won't have to
do any logging.

Tony

--
Tony Rogerson
SQL Server MVP
http://sqlserverfaq.com - free video tutorials

"harish" <harish.prabhala@.gmail.com> wrote in message
news:1131162982.103343.268120@.o13g2000cwo.googlegr oups.com...
> DELETING 100 million from a table weekly SQl SERVER 2000
> Hi All
> We have a table in SQL SERVER 2000 which has about 250 million records
> and this will be growing by 100 million every week. At a time the table
> should contain just 13 weeks of data. when the 14th week data needs to
> be loaded the first week's data has to be deleted.
> And this deletes 100 million every week, since the delete is taking lot
> of transaction log space the job is not successful.
> Can you please help with what are the approaches we can take to fix
> this problem?
> Performance and transaction log are the issues we are facing. We tried
> deletion in steps too but that also is taking time. What are the
> different ways we can address this quickly.
> Please reply at the earliest.
> Thanks
> Harish|||Am 4 Nov 2005 19:56:22 -0800 schrieb harish:

> DELETING 100 million from a table weekly SQl SERVER 2000
> Hi All
> We have a table in SQL SERVER 2000 which has about 250 million records
> and this will be growing by 100 million every week. At a time the table
> should contain just 13 weeks of data. when the 14th week data needs to
> be loaded the first week's data has to be deleted.
> And this deletes 100 million every week, since the delete is taking lot
> of transaction log space the job is not successful.
> Can you please help with what are the approaches we can take to fix
> this problem?
> Performance and transaction log are the issues we are facing. We tried
> deletion in steps too but that also is taking time. What are the
> different ways we can address this quickly.
> Please reply at the earliest.
> Thanks
> Harish

In this special case i would think about using a table per week. There is
no faster way then DROP/CREATE or maybe TRUNCATE. You have to change a lot
in the way you work with this data, but you have UNION and maybe you can
use VIEWS.
Or you use a big Solid State Disk for your database :-))

bye,
Helmut|||helmut woess (hw@.iis.at) writes:
> In this special case i would think about using a table per week. There is
> no faster way then DROP/CREATE or maybe TRUNCATE. You have to change a lot
> in the way you work with this data, but you have UNION and maybe you can
> use VIEWS.
> Or you use a big Solid State Disk for your database :-))

Since one table per week becomes quite a job to manage, I would go for
one table per month, and then truncate once per month.

If this would be too much data, I would then try every tenth day. This
makes it a lot easier to set up the check constraints for the partitions.

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp|||"Erland Sommarskog" <esquel@.sommarskog.se> wrote in message
news:Xns97059EAF78709Yazorman@.127.0.0.1...
> helmut woess (hw@.iis.at) writes:
> > In this special case i would think about using a table per week. There
is
> > no faster way then DROP/CREATE or maybe TRUNCATE. You have to change a
lot
> > in the way you work with this data, but you have UNION and maybe you can
> > use VIEWS.
> > Or you use a big Solid State Disk for your database :-))
> Since one table per week becomes quite a job to manage, I would go for
> one table per month, and then truncate once per month.
> If this would be too much data, I would then try every tenth day. This
> makes it a lot easier to set up the check constraints for the partitions.

Another way to handle this which is SQL Server specific is to set a rowcount
of say 10,000 and loop through deleting 10,000 rows at a time.

And either back up the log frequently enough or use a simple recovery
method.

>
> --
> Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
> Books Online for SQL Server SP3 at
> http://www.microsoft.com/sql/techin.../2000/books.asp

DELETING 100 million from a table weekly SQl SERVER 2000

DELETING 100 million from a table weekly SQl SERVER 2000
Hi All
We have a table in SQL SERVER 2000 which has about 250 million records
and this will be growing by 100 million every week. At a time the table
should contain just 13 weeks of data. when the 14th week data needs to
be loaded the first week's data has to be deleted.
And this deletes 100 million every week, since the delete is taking lot
of transaction log space the job is not successful.
Can you please help with what are the approaches we can take to fix
this problem?
Performance and transaction log are the issues we are facing. We tried
deletion in steps too but that also is taking time. What are the
different ways we can address this quickly.
Please reply at the earliest.
Thanks
HarishYou could try inserting the clean records i.e. Current 13 weeks data into a
a temporary table.
Then truncate the table with all data in (bear in mind any identity
columns).
Reinsert the clean records into the table.
Again, bear in mind any idientity columns if you need to keep the ids in
sync as truncate will reset the seed count, also, you can't truncate a table
with a FK so you'd need to work around that too.
Not the best method but may be quicker than your current process.
Immy
"harish" <harish.prabhala@.gmail.com> wrote in message
news:1131163435.343059.179520@.f14g2000cwb.googlegroups.com...
> DELETING 100 million from a table weekly SQl SERVER 2000
> Hi All
> We have a table in SQL SERVER 2000 which has about 250 million records
> and this will be growing by 100 million every week. At a time the table
> should contain just 13 weeks of data. when the 14th week data needs to
> be loaded the first week's data has to be deleted.
> And this deletes 100 million every week, since the delete is taking lot
> of transaction log space the job is not successful.
> Can you please help with what are the approaches we can take to fix
> this problem?
> Performance and transaction log are the issues we are facing. We tried
> deletion in steps too but that also is taking time. What are the
> different ways we can address this quickly.
> Please reply at the earliest.
> Thanks
> Harish
>|||> Performance and transaction log are the issues we are facing. We tried
> deletion in steps too but that also is taking time. What are the
> different ways we can address this quickly.
Deleting in smaller separate transaction batches will reduce transaction log
space requirements and improve performance You'll need to experiment to
determine the best batch size for your environment. In my experience, the
optimum size is somewhere around the amount of SQL Server memory. For
example, with 2GB RAM and a 100 byte row size, delete no more than 20M rows
at a time. If you are using the FULL are BULK_LOGGED recovery model, you'll
also need to backup the log between batches to keep the log size manageable.
Alternatively, you can use change to the SIMPLE model for the duration of
the delete script and change back to your normal recovery model afterward.
Don't forget to perform a full database backup following the change from
SIMPLE recovery.
Use the TABLOCKX hint if possible. Ideally, the table's clustered index
should be the column(s) used for your delete criteria.
Another option is to partition data based on your delete criteria (separate
table for each week). This will allow you to simply drop the table
containing the oldest data. The partitioning implementation can be made
transparent to applications by using a UNION ALL view. After dropping the
oldest table, you create a new table for the latest data and change the view
accordingly. If you can adhere to the rules for local partitioned views as
described in the Books Online, there are performance advantages with the
partitioned view and the view is updatable as well. See the example below.
CREATE TABLE OrderDetails_20051030
(
OrderID int NOT NULL,
ProductID int NOT NULL,
Quantity int NOT NULL,
UnitPrice decimal(9, 2) NOT NULL,
OrderDate smalldatetime NOT NULL
CONSTRAINT CK_OrderDetails_20051030
CHECK (OrderDate > '20051023' AND OrderDate <= '20051030')
)
ALTER TABLE OrderDetails_20051030
ADD CONSTRAINT PK_OrderDetails_20051030
PRIMARY KEY(OrderDate, OrderID, ProductID)
GO
CREATE TABLE OrderDetails_20051106
(
OrderID int NOT NULL,
ProductID int NOT NULL,
Quantity int NOT NULL,
UnitPrice decimal(9, 2) NOT NULL,
OrderDate smalldatetime NOT NULL
CONSTRAINT CK_OrderDetails_20051106
CHECK (OrderDate > '20051030' AND OrderDate <= '20051106')
)
ALTER TABLE OrderDetails_20051106
ADD CONSTRAINT PK_OrderDetailsOrderDetails_20051106
PRIMARY KEY(OrderDate, OrderID, ProductID)
GO
CREATE TABLE OrderDetails_20051113
(
OrderID int NOT NULL,
ProductID int NOT NULL,
Quantity int NOT NULL,
UnitPrice decimal(9, 2) NOT NULL,
OrderDate smalldatetime NOT NULL
CONSTRAINT CK_OrderDetails_20051113_OrderDate
CHECK (OrderDate > '20051106' AND OrderDate <= '20051113')
)
ALTER TABLE OrderDetails_20051113
ADD CONSTRAINT PK_OrderDetails_20051113
PRIMARY KEY(OrderDate, OrderID, ProductID)
GO
CREATE VIEW OrderDetails AS
SELECT
OrderID,
ProductID,
Quantity,
UnitPrice,
OrderDate
FROM OrderDetails_20051030
UNION ALL
SELECT
OrderID,
ProductID,
Quantity,
UnitPrice,
OrderDate
FROM OrderDetails_20051106
UNION ALL
SELECT
OrderID,
ProductID,
Quantity,
UnitPrice,
OrderDate
FROM OrderDetails_20051113
GO
INSERT INTO OrderDetails VALUES(1, 1, 1, 1, '20051030')
INSERT INTO OrderDetails VALUES(2, 1, 1, 1, '20051106')
INSERT INTO OrderDetails VALUES(3, 1, 1, 1, '20051113')
GO
--execution plan shows only OrderDetails_20051106 is accessed because
--all PK columns are referenced in this query
SELECT
a.OrderID,
a.ProductID,
a.Quantity,
a.UnitPrice,
a.OrderDate
FROM OrderDetails a
WHERE a.OrderDate = '20051106' AND OrderID = 2 AND ProductID = 1
GO
--to remove oldest week:
DROP VIEW OrderDetails
GO
--drop oldest table
DROP TABLE OrderDetails_20051030
GO
--create table for new data
CREATE TABLE OrderDetails_20051120
(
OrderID int NOT NULL,
ProductID int NOT NULL,
Quantity int NOT NULL,
UnitPrice decimal(9, 2) NOT NULL,
OrderDate smalldatetime NOT NULL
CONSTRAINT CK_OrderDetails_20051120_OrderDate
CHECK (OrderDate > '20051113' AND OrderDate <= '20051120')
)
ALTER TABLE OrderDetails_20051120
ADD CONSTRAINT PK_OrderDetails_20051120
PRIMARY KEY(OrderDate, OrderID, ProductID)
GO
--create view with current tables
CREATE VIEW OrderDetails AS
SELECT
OrderID,
ProductID,
Quantity,
UnitPrice,
OrderDate
FROM OrderDetails_20051106
UNION ALL
SELECT
OrderID,
ProductID,
Quantity,
UnitPrice,
OrderDate
FROM OrderDetails_20051113
UNION ALL
SELECT
OrderID,
ProductID,
Quantity,
UnitPrice,
OrderDate
FROM OrderDetails_20051120
GO
Hope this helps.
Dan Guzman
SQL Server MVP
"harish" <harish.prabhala@.gmail.com> wrote in message
news:1131163435.343059.179520@.f14g2000cwb.googlegroups.com...
> DELETING 100 million from a table weekly SQl SERVER 2000
> Hi All
> We have a table in SQL SERVER 2000 which has about 250 million records
> and this will be growing by 100 million every week. At a time the table
> should contain just 13 weeks of data. when the 14th week data needs to
> be loaded the first week's data has to be deleted.
> And this deletes 100 million every week, since the delete is taking lot
> of transaction log space the job is not successful.
> Can you please help with what are the approaches we can take to fix
> this problem?
> Performance and transaction log are the issues we are facing. We tried
> deletion in steps too but that also is taking time. What are the
> different ways we can address this quickly.
> Please reply at the earliest.
> Thanks
> Harish
>|||I forgot to mention that SQL 2005 Enterprise introduces new table and index
partitioning features that make it easier to partition large tables.
Hope this helps.
Dan Guzman
SQL Server MVP
"Dan Guzman" <guzmanda@.nospam-online.sbcglobal.net> wrote in message
news:%23$vpN7h4FHA.3000@.TK2MSFTNGP12.phx.gbl...
> Deleting in smaller separate transaction batches will reduce transaction
> log space requirements and improve performance You'll need to experiment
> to determine the best batch size for your environment. In my experience,
> the optimum size is somewhere around the amount of SQL Server memory. For
> example, with 2GB RAM and a 100 byte row size, delete no more than 20M
> rows at a time. If you are using the FULL are BULK_LOGGED recovery model,
> you'll also need to backup the log between batches to keep the log size
> manageable. Alternatively, you can use change to the SIMPLE model for the
> duration of the delete script and change back to your normal recovery
> model afterward. Don't forget to perform a full database backup following
> the change from SIMPLE recovery.
> Use the TABLOCKX hint if possible. Ideally, the table's clustered index
> should be the column(s) used for your delete criteria.
> Another option is to partition data based on your delete criteria
> (separate table for each week). This will allow you to simply drop the
> table containing the oldest data. The partitioning implementation can be
> made transparent to applications by using a UNION ALL view. After
> dropping the oldest table, you create a new table for the latest data and
> change the view accordingly. If you can adhere to the rules for local
> partitioned views as described in the Books Online, there are performance
> advantages with the partitioned view and the view is updatable as well.
> See the example below.
> CREATE TABLE OrderDetails_20051030
> (
> OrderID int NOT NULL,
> ProductID int NOT NULL,
> Quantity int NOT NULL,
> UnitPrice decimal(9, 2) NOT NULL,
> OrderDate smalldatetime NOT NULL
> CONSTRAINT CK_OrderDetails_20051030
> CHECK (OrderDate > '20051023' AND OrderDate <= '20051030')
> )
> ALTER TABLE OrderDetails_20051030
> ADD CONSTRAINT PK_OrderDetails_20051030
> PRIMARY KEY(OrderDate, OrderID, ProductID)
> GO
> CREATE TABLE OrderDetails_20051106
> (
> OrderID int NOT NULL,
> ProductID int NOT NULL,
> Quantity int NOT NULL,
> UnitPrice decimal(9, 2) NOT NULL,
> OrderDate smalldatetime NOT NULL
> CONSTRAINT CK_OrderDetails_20051106
> CHECK (OrderDate > '20051030' AND OrderDate <= '20051106')
> )
> ALTER TABLE OrderDetails_20051106
> ADD CONSTRAINT PK_OrderDetailsOrderDetails_20051106
> PRIMARY KEY(OrderDate, OrderID, ProductID)
> GO
> CREATE TABLE OrderDetails_20051113
> (
> OrderID int NOT NULL,
> ProductID int NOT NULL,
> Quantity int NOT NULL,
> UnitPrice decimal(9, 2) NOT NULL,
> OrderDate smalldatetime NOT NULL
> CONSTRAINT CK_OrderDetails_20051113_OrderDate
> CHECK (OrderDate > '20051106' AND OrderDate <= '20051113')
> )
> ALTER TABLE OrderDetails_20051113
> ADD CONSTRAINT PK_OrderDetails_20051113
> PRIMARY KEY(OrderDate, OrderID, ProductID)
> GO
> CREATE VIEW OrderDetails AS
> SELECT
> OrderID,
> ProductID,
> Quantity,
> UnitPrice,
> OrderDate
> FROM OrderDetails_20051030
> UNION ALL
> SELECT
> OrderID,
> ProductID,
> Quantity,
> UnitPrice,
> OrderDate
> FROM OrderDetails_20051106
> UNION ALL
> SELECT
> OrderID,
> ProductID,
> Quantity,
> UnitPrice,
> OrderDate
> FROM OrderDetails_20051113
> GO
> INSERT INTO OrderDetails VALUES(1, 1, 1, 1, '20051030')
> INSERT INTO OrderDetails VALUES(2, 1, 1, 1, '20051106')
> INSERT INTO OrderDetails VALUES(3, 1, 1, 1, '20051113')
> GO
> --execution plan shows only OrderDetails_20051106 is accessed because
> --all PK columns are referenced in this query
> SELECT
> a.OrderID,
> a.ProductID,
> a.Quantity,
> a.UnitPrice,
> a.OrderDate
> FROM OrderDetails a
> WHERE a.OrderDate = '20051106' AND OrderID = 2 AND ProductID = 1
> GO
> --to remove oldest week:
> DROP VIEW OrderDetails
> GO
> --drop oldest table
> DROP TABLE OrderDetails_20051030
> GO
> --create table for new data
> CREATE TABLE OrderDetails_20051120
> (
> OrderID int NOT NULL,
> ProductID int NOT NULL,
> Quantity int NOT NULL,
> UnitPrice decimal(9, 2) NOT NULL,
> OrderDate smalldatetime NOT NULL
> CONSTRAINT CK_OrderDetails_20051120_OrderDate
> CHECK (OrderDate > '20051113' AND OrderDate <= '20051120')
> )
> ALTER TABLE OrderDetails_20051120
> ADD CONSTRAINT PK_OrderDetails_20051120
> PRIMARY KEY(OrderDate, OrderID, ProductID)
> GO
> --create view with current tables
> CREATE VIEW OrderDetails AS
> SELECT
> OrderID,
> ProductID,
> Quantity,
> UnitPrice,
> OrderDate
> FROM OrderDetails_20051106
> UNION ALL
> SELECT
> OrderID,
> ProductID,
> Quantity,
> UnitPrice,
> OrderDate
> FROM OrderDetails_20051113
> UNION ALL
> SELECT
> OrderID,
> ProductID,
> Quantity,
> UnitPrice,
> OrderDate
> FROM OrderDetails_20051120
> GO
>
> --
> Hope this helps.
> Dan Guzman
> SQL Server MVP
> "harish" <harish.prabhala@.gmail.com> wrote in message
> news:1131163435.343059.179520@.f14g2000cwb.googlegroups.com...
>|||Hey
Thanks. We have an index on four columns in this table. For Ex A, B, C
and D
The delete statement's where clause has the conditions for A, B and C
The delete statement's where clause has the conditions for just A
Which of the two's performance will be faster?
We tried something like this:
SET ROWCOUNT 5000;
WHILE 1 = 1
BEGIN
DELETE FROM T1 WHERE dt < '20030101' -- original delete
IF @.@.rowcount < 5000 BREAK;
END
SET ROWCOUNT 0;
1) Does this setting ROWCOUNT first sort the table and then delete?
2) The above query is executed to delete all records satisfying the
condition in steps of 5000 until the delete is comple.
How can I stop it after one 5000?|||On 5 Nov 2005 11:01:30 -0800, harish wrote:
(snip)
Hi Harish,
I just replied to the same question in another thread.
Could you please ask your questions in JUST ONE place, and in JUST ONE
group? I've seen your messages scattered over several groups, and
several different messages in just this group. Many of them have
attracted replies. It's very hard to keep track of what is going on in
all thesse threads, and it's a waste of other people's time if someone
posts a reply to you that you already had received in another group.
Best, Hugo
--
(Remove _NO_ and _SPAM_ to get my e-mail address)|||Hi
Sorry for that. I was new to the group and not sure which group is
active.
Thanks a lot for the information
Regards
Harish

DELETING 100 million from a table weekly SQl SERVER 2000

DELETING 100 million from a table weekly SQl SERVER 2000
Hi All
We have a table in SQL SERVER 2000 which has about 250 million records
and this will be growing by 100 million every week. At a time the table
should contain just 13 weeks of data. when the 14th week data needs to
be loaded the first week's data has to be deleted.
And this deletes 100 million every week, since the delete is taking lot
of transaction log space the job is not successful.
Can you please help with what are the approaches we can take to fix
this problem?
Performance and transaction log are the issues we are facing. We tried
deletion in steps too but that also is taking time. What are the
different ways we can address this quickly.
Please reply at the earliest.
Thanks
Harish
If you can move up to SQL Server 2005, then you should look at partitioning the table.
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
Blog: http://solidqualitylearning.com/blogs/tibor/
"harish" <harish.prabhala@.gmail.com> wrote in message
news:1131162784.737373.142270@.g43g2000cwa.googlegr oups.com...
> DELETING 100 million from a table weekly SQl SERVER 2000
> Hi All
> We have a table in SQL SERVER 2000 which has about 250 million records
> and this will be growing by 100 million every week. At a time the table
> should contain just 13 weeks of data. when the 14th week data needs to
> be loaded the first week's data has to be deleted.
> And this deletes 100 million every week, since the delete is taking lot
> of transaction log space the job is not successful.
> Can you please help with what are the approaches we can take to fix
> this problem?
> Performance and transaction log are the issues we are facing. We tried
> deletion in steps too but that also is taking time. What are the
> different ways we can address this quickly.
> Please reply at the earliest.
> Thanks
> Harish
>
|||Hey
Thanks. We have an index on four columns in this table. For Ex A, B, C
and D
The delete statement's where clause has the conditions for A, B and C
The delete statement's where clause has the conditions for just A
Which of the two's performance will be faster?
We tried something like this:
SET ROWCOUNT 5000;
WHILE 1 = 1
BEGIN
DELETE FROM T1 WHERE dt < '20030101' -- original delete
IF @.@.rowcount < 5000 BREAK;
END
SET ROWCOUNT 0;
1) Does this setting ROWCOUNT first sort the table and then delete?
2) The above query is executed to delete all records satisfying the
condition in steps of 5000 until the delete is comple.
How can I stop it after one 5000?
|||"harish" <harish.prabhala@.gmail.com> wrote in message
news:1131217180.922372.211460@.g44g2000cwa.googlegr oups.com...
> Thanks. We have an index on four columns in this table. For Ex A, B, C
> and D
> The delete statement's where clause has the conditions for A, B and C
> The delete statement's where clause has the conditions for just A
>
> Which of the two's performance will be faster?
There should be virtually no difference. What might be faster would be
using a predicate that can be satisfied by the clustered index, to avoid
doing two lookups.

> 1) Does this setting ROWCOUNT first sort the table and then delete?
No; it just deletes the first N rows it finds that satisfy the
predicate.

> 2) The above query is executed to delete all records satisfying the
> condition in steps of 5000 until the delete is comple.
> How can I stop it after one 5000?
Remove the loop.
Adam Machanic
Pro SQL Server 2005, available now
www.apress.com/book/bookDisplay.html?bID=457
|||On 5 Nov 2005 10:59:40 -0800, harish wrote:
(snip)
Hi Harish,
I just replied to the same question in another thread.
Could you please ask your questions in JUST ONE place, and in JUST ONE
group? I've seen your messages scattered over several groups, and
several different messages in just this group. Many of them have
attracted replies. It's very hard to keep track of what is going on in
all thesse threads, and it's a waste of other people's time if someone
posts a reply to you that you already had received in another group.
Best, Hugo
(Remove _NO_ and _SPAM_ to get my e-mail address)
|||Where's the other thread?
Adam Machanic
Pro SQL Server 2005, available now
www.apress.com/book/bookDisplay.html?bID=457
"Hugo Kornelis" <hugo@.pe_NO_rFact.in_SPAM_fo> wrote in message
news:uq5qm1drmjlp5kt0hp0jl3krefsm9qgff8@.4ax.com...
> On 5 Nov 2005 10:59:40 -0800, harish wrote:
> (snip)
> Hi Harish,
> I just replied to the same question in another thread.
> Could you please ask your questions in JUST ONE place, and in JUST ONE
> group? I've seen your messages scattered over several groups, and
> several different messages in just this group. Many of them have
> attracted replies. It's very hard to keep track of what is going on in
> all thesse threads, and it's a waste of other people's time if someone
> posts a reply to you that you already had received in another group.
> Best, Hugo
> --
> (Remove _NO_ and _SPAM_ to get my e-mail address)
|||On Sat, 5 Nov 2005 18:08:27 -0500, Adam Machanic wrote:

>Where's the other thread?
Hi Adam,
One in microsoft.public.sqlserver.newusers (that's where I replied to
his questions - but don't bother to look it up, my answers are virtually
identical; to yours).
One in microsoft.public.sqlserver.server.
And no less than THREE in microsoft.public.sqlserver.programming.
If there were more, I didn't see them.
Best, Hugo
(Remove _NO_ and _SPAM_ to get my e-mail address)
|||Hi Hugo
This is Harish again
set rowcount 100000
declare @.a int
while 1=1
begin
begin tran del1
delete from dbo.extt_vpm_ldr_stg_2
select @.a = @.@.rowcount
print @.a
commit tran del1
if @.a < 100000 break;
end
TOTAL TABLE SIZE - 650 000 records
I am using the above statement to delete in chunks on 100000.
After every 100000 I have put a COMMIT TRAN. Hence as per the logic it
should delete 100000 commit and delete the next 100000.
And the transaction log is suppose 100MB before the statement is
executed. It is increasing in steps of some 30MB for each delete to
upto some 250MB and then goes back to some 50MB.
What we need is the transaction log must increase for every delete and
drop and again increase and so on.
How can the above statement be modified to take care of this behaviour?
Please reply asap.
Thanks
Harish
|||On 9 Nov 2005 08:10:52 -0800, harish wrote:

>Hi Hugo
>This is Harish again
Hi Harish,
This is my previous reply again
Could you please ask your questions in JUST ONE place, and in JUST ONE
group? I've seen your messages scattered over several groups, and
several different messages in just this group. Many of them have
attracted replies. It's very hard to keep track of what is going on in
all thesse threads, and it's a waste of other people's time if someone
posts a reply to you that you already had received in another group.
BTW, I already saw your question in one of the other groups, and I have
already seen at least one answer (can't recall off the top of my head
who wrote it, though).
Best, Hugo
(Remove _NO_ and _SPAM_ to get my e-mail address)