473,465 Members | 1,925 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Time to shrink a database

Hi all,
I posted messages before about trying to purge many records (about 35%)
of a 200Gig database. The responses gave me a lot to think about,
especially regarding the indexes. But due to the short windows that I
have to run in, manipulating the indexes is not an option.

But this leads to another question. When all of this is done, we will
need to shrink the db to reclaim the space. We will also need to
rebuild the indexes, but this can be done one table at a time, so that
might be ok. What I am looking for is advice on how to get through a
shink of a 200G db on a fairly slow machine. Are there any 'tricks of
the trade' that will help me get through it? I believe one of the DBAs
said that they have not been able to shrink the db in years because it
takes longer than the longest available window.

Thanks In Advance

Apr 11 '06 #1
12 11744
(continuation of first post)

One idea that I had, which may not work at all, is as follows.

Copy a table to a temp db.
Do the shrink on the temp db.
Drop the original table.
Move the table from the temp db to the original db.

Repeat for each table in the db.

Shrink the original db.

The thought behind this is that if you shrink a db with only one table,
you can get through it much quicker than shrinking a db with all of the
tables in it. This could be done in the available window.
Then when you shrink the original db, there is less work to do since
each table was already shrunk.
I assume that because of the files behind the DBs, this would not work.
Any thoughts on this would be appreciated. I am planning on setting up
a test of this, but if it is a waste of time, please let me know.

Thanks

Apr 11 '06 #2
ha*****@yahoo.com (ha*****@yahoo.com) writes:
I posted messages before about trying to purge many records (about 35%)
of a 200Gig database. The responses gave me a lot to think about,
especially regarding the indexes. But due to the short windows that I
have to run in, manipulating the indexes is not an option.

But this leads to another question. When all of this is done, we will
need to shrink the db to reclaim the space. We will also need to
rebuild the indexes, but this can be done one table at a time, so that
might be ok. What I am looking for is advice on how to get through a
shink of a 200G db on a fairly slow machine. Are there any 'tricks of
the trade' that will help me get through it? I believe one of the DBAs
said that they have not been able to shrink the db in years because it
takes longer than the longest available window.


I'm not sure you are going shrink at all. Even if you are removing
a lot of rows from the database, I assume that new rows keep coming in
all the time? There is no point in shrinking, if it will grow again.

Reindexing on the other hand is a good idea, but this it not something
you should run when your DELETE job is done, but which should be performed
regularly. Defragmenting can be performed in two ways DBCC DBREINDEX and
DBCC INDEXDEFRAG. The first is an offline operation, that is the table
is not accessible while it's running. INDEXDEFRAG is an online operation.
Again, I'm assuming that data is inserted and updated in the
database on a regular basis.

According to Books Online, shrinking is an online operation in the sense
that uses can keep on working. I would expect it to take some load,
and I would certainly not run a shrink on office hours. A tip is to
specify a target size; that's the variation I've been most successful
with.

If you are going to shrink, which again I don't recommend, the best is
to do this when all tables have been reduced by your deletion job.
You cannot shrink one table at a time as you outlined in you other
post.

Once you have completely any shrinking, you should definitely run
defragmentation, as shrinking causes a lot of fragmentation.

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

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Apr 11 '06 #3
Ok, I think I understand your point about not shrinking the db. Does
this mean that new records will be written to the space that was freed
up by the deletes? I thought that the space would not be reused until
you did a shrink to release it.
However, my purge will be removing about 200 million records and the db
only grows at about 10 million per month. So it would take a long time
to fill up the space freed by the purge.

As for the DBREINDEX and the INDEXDEFRAG, do they produce similar
results? The DBAs that I am doing this for seem to believe that a
reindex will give a bigger performance boost than the defrag.

thanks

Apr 12 '06 #4
Does anyone have a resource on index defragmentation that could be done
on a schedule for an entire database based on some automated
statistics? I see plenty of info in BOL, but whether I should use fill
factors of 80, 30 or what I have no idea. We have never done any kind
of index defragmentation - is there some way that the database itself
can just handle it?

Apr 12 '06 #5
ha*****@yahoo.com (ha*****@yahoo.com) writes:
Ok, I think I understand your point about not shrinking the db. Does
this mean that new records will be written to the space that was freed
up by the deletes?
Not really. If you don't defragment, the likelyhood that any space will
be reused is small. But if you defragment, the tables are compressed,
and the free space moved to free extents where it indeed can be reused.
I thought that the space would not be reused until
you did a shrink to release it.
When I think of it, you will probably have to defragment before you
can shrink. Shrinking works with free extents I guess. So if there
are pages in a table that are 10% full, that space will not be
reclaimed by a shrink.
However, my purge will be removing about 200 million records and the db
only grows at about 10 million per month. So it would take a long time
to fill up the space freed by the purge.
Less than two years.
As for the DBREINDEX and the INDEXDEFRAG, do they produce similar
results? The DBAs that I am doing this for seem to believe that a
reindex will give a bigger performance boost than the defrag.


I will have to admit that I am not too well acqauinted with INDEXDEFRAG
to answer. I personally prefer DBREINDEX, but it has the drawback of
closing out users, which appears to be a concern for you.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Apr 12 '06 #6
pb648174 (go****@webpaul.net) writes:
Does anyone have a resource on index defragmentation that could be done
on a schedule for an entire database based on some automated
statistics? I see plenty of info in BOL, but whether I should use fill
factors of 80, 30 or what I have no idea. We have never done any kind
of index defragmentation - is there some way that the database itself
can just handle it?


I guess that you can set up a maintenance job, but I think it's better
to run a separate job.

The easy way is just to run a cursor over sysobjects and run DBREINDEX
on all tables. As long as you have enough off-hours to permit this, this
may be good enough. And it's certainly far better than nothing at all.

A colleage of mine has composed a reindexing job for our system. It uses
DBCC SHOWCONTIG to check for fragmentation, and runs DBREINDEX only if
fragmentation is over 30%. For the tables with lower fragmentation, he
instead runs UPDATE STATISTICS WITH FULLSCAN INDEX on my insistence. This
is because some tables have monotonically growing keys, so they don't
fragment, but statistics easily gets out of date, as new rows are always
added outside the current intervals in the histogramme.

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

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Apr 12 '06 #7
So there isn't some source out there that has the script posted on the
net? If this is something that should be done for every single
database, why should every person write their own and go through a
development bug fix cycle to figure out what they are doing wrong? I'm
worried about throwing something together and it causing more problems
than it solves (right now we have no problems as far as I know related
to indexes or fragmentation for databases in use for years).

Apr 13 '06 #8
pb648174 (go****@webpaul.net) writes:
So there isn't some source out there that has the script posted on the
net? If this is something that should be done for every single
database, why should every person write their own and go through a
development bug fix cycle to figure out what they are doing wrong? I'm
worried about throwing something together and it causing more problems
than it solves (right now we have no problems as far as I know related
to indexes or fragmentation for databases in use for years).


I didn't say that there is not anything publicly available for this, but
I was too lazy to start searching for something. As a matter of fact the
code that we use to get the tables with 30% more fragmentation was some-
thing that SQL Server MVP Andrew Kelly posted to
microsoft.public.sqlserver.tools once. (I believe that it was that news-
group, but I could be wrong.)

SQL Server comes with maintenance plans where you can set this up,
and probably get some default. But the problem is, that this is not a
case of one size fits all. There are several factors involved: how
much data is inserted? updated? deleted? What availability requirements
do you have? Do you need to use GUIDs etc.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Apr 13 '06 #9
The total database size is less than half a gig. Most of the time the
data is being read, with maybe 20% of the time being inserts and
updates. I would like an expert to give me the link so I know I'm not
getting the wrong thing.. I see plenty of stuff out there but am unsure
what the right thing to do is. I was hoping there would be
functionality in SQL 2005 that would just handle it.

Apr 18 '06 #10
pb648174 (go****@webpaul.net) writes:
The total database size is less than half a gig. Most of the time the
data is being read, with maybe 20% of the time being inserts and
updates. I would like an expert to give me the link so I know I'm not
getting the wrong thing.. I see plenty of stuff out there but am unsure
what the right thing to do is. I was hoping there would be
functionality in SQL 2005 that would just handle it.


If the database is that small, I would set up job that just loops
sysobjects and reindex every table in sight. It's not worth the effort
to anything more sophisticated.

The reason that we exempt some tables is from reindexing is mainly to
reduce execution time for the job.

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

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Apr 18 '06 #11
Ok, does running the below once per week seem reasonable for a SQL 2005
DB?

DECLARE @TableName varchar(255)

DECLARE TableCursor CURSOR FOR
SELECT top 1 table_name FROM information_schema.tables
WHERE table_type = 'base table'
OPEN TableCursor

FETCH NEXT FROM TableCursor INTO @TableName
WHILE @@FETCH_STATUS = 0
BEGIN
Exec('ALTER INDEX ALL ON ' + @TableName + ' REBUILD WITH (FILLFACTOR =
80)')
FETCH NEXT FROM TableCursor INTO @TableName
END

CLOSE TableCursor

DEALLOCATE TableCursor

DBCC CHECKDB
go

Apr 20 '06 #12
pb648174 (go****@webpaul.net) writes:
Ok, does running the below once per week seem reasonable for a SQL 2005
DB?
Some small modifications:
DECLARE TableCursor CURSOR FOR
SELECT top 1 table_name FROM information_schema.tables


1) The TOP 1 should probably not be there.

2) Change "table_name" to quotename(table_name), so you can defrag
Northwind and its Order Details too. :-)

3) And change to correct case, in case you one day want to run it on
case-sensitive database. It's INFORMATION_SCHEMA etc.

And I will have to admit that I have not really digested the new
ALTER INDEX syntax in SQL 2005, but I guess that part looks find.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Apr 20 '06 #13

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

Similar topics

1
by: Adrian Parker | last post by:
Hello all. I have a Access database which I read records from. All the records read fine, except the Date/Time field of the database is not being properly assigned to my Date-Time-Picker control...
1
by: Kelvin | last post by:
Hi all, Any some tools or sample codes drop down asp script execute time and database connection time ? Thanks.
4
by: Tommy.Vincent | last post by:
hi all, This will be a easy question for all out here. I have a database of 28GB. having 3 Data Files 22 GB, 3.58 Gb and 2.70 GB respectively. and a Transaction Log file of 156 mb. When i...
1
by: jdph40 | last post by:
I am using Access 2002. I downloaded the forms sample database FrmSmp97.mdb from Microsoft and used the following code in the timer event of a hidden form in order to close a database if no...
7
by: Shivalee Gupta via AccessMonster.com | last post by:
i am working on access 2000. can i write a code which will tell me, at the time of closing the database, that at what time i had opened the database and at what time i am closing it? i am in a...
1
by: simon | last post by:
I have time in my program: for example: dim timeTest as datetime timeTest="10:15:10" I save this time to database as decimal value: timeTest.ToOADate - I get the decimal value and I save...
0
by: janbazamin | last post by:
Hi Dear. I am making time table application in ASP.NET i am facing some problem i am adding Start time, End Time In Database. on some date. if i again select same date and stat time and end time. i...
0
by: janbazamin | last post by:
I am making time table application in ASP.NET i am facing some problem i am adding Start time, End Time In Database. on some date. if i again select same date and stat time and end time. i am...
8
jschrader
by: jschrader | last post by:
I am trying to write a conditional Statement in ASP based on dates/times in a database entry. I'm basically a ASP/web designer and try my best to get into the fun coding, so not sure as to the...
1
by: MANU1k | last post by:
My database properties show Space Available 0.00 MB. And users complain the application is very slow. Do i need to do a database shrink? Also can i schedule db shrink on a weekly basis is that...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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

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