I have a SQL Server 2014 database dump which is approx. 60GB large. In SQL Server Management Studio, it is shown for the original DB, that the "ROWS Data" has an initial size of ~ 99000MB and the "LOG" has an initial size of ~ 25600MB.
Now there are a view tables in the Database which are about 10GB large and which I can flush/clean.
After deleting the data inside those tables, what is the best way to decrease the physical size of the database? A lot of posts I discovered are dealing with SHRINKDATABASE but some articles won't recommend it because of bad fragmentation and performance.
Thanks in advance.
Here is a code I have used to reduce the space used and free up Database space and actual Drive space. Hope this helps.
USE YourDataBase;
GO
-- Truncate the log by changing the database recovery model to SIMPLE.
ALTER DATABASE YourDataBase
SET RECOVERY SIMPLE;
GO
-- Shrink the truncated log file to 1 MB.
DBCC SHRINKFILE (YourDataBase_log, 1);
GO
-- Reset the database recovery model.
ALTER DATABASE YourDataBase
SET RECOVERY FULL;
GO
Instead of use DBCC SHRINKDATABASE I suggest you to use
DBCC SHRINKFILE, obviously your tables should be stored on different FILEGROUP.
Then you can reduce the physical size of your database optimizing fragmentation.
Hope this could help
You need to shrink your DataBase by using below Query.
ALTER DATABASE [DBNAME] SET RECOVERY SIMPLE WITH NO_WAIT
DBCC SHRINKFILE(PTechJew_LOG, 1)
ALTER DATABASE [DBNAME] SET RECOVERY FULL WITH NO_WAIT
After run this query check your log file.
Its Worked.
Related
I have a limited space in the server and i have to remove transactions periodically. Moreover, I use below query that answered in this StackOverFlow question:How do you clear the SQL Server transaction log?
USE db;
GO
-- Truncate the log by changing the database recovery model to SIMPLE.
ALTER DATABASE db
SET RECOVERY SIMPLE;
GO
-- Shrink the truncated log file to 1 MB.
DBCC SHRINKFILE (db_Log, 1);
GO
-- Reset the database recovery model.
ALTER DATABASE db
SET RECOVERY FULL;
GO
However, i want this simple query to be much more advanced and put below conditions.
Remove Transaction Logs When 10% free space remained from the db_Log(100 mb free 900 mb used)
Transaction Logs file Must have a fixed size(1GB)
This is just an XYProblem. The problem isn't the transaction log size, it that's you aren't taking transaction log back ups and wondering why the transaction log is growing. It's growing because you aren't backing it up.
Either you need to add an agent task to regularly create transaction log back ups, or change the recovery model. Considering your statement "actually the transaction logs are not very important" I suggest the latter, and then set the max size of the file:
ALTER DATABASE db SET RECOVERY SIMPLE;
GO
ALTER DATABASE db
MODIFY FILE (NAME=db_Log, MAXSIZE = 1024MB);
I restored a Production database to test environment. In Prod it's configured to Transactional Replication and database around 400GB, log file alone 120GB.
I tried Database set to Simple recovery and shrink DBCC Shrinkfile still log file size same (I know shrinking is not an ideal solution, but I want to make it small). There are no long running transactions and blocking
Here is what I followed:
* Backup database
ALTER DATABASE DatabaseName SET RECOVERY SIMPLE
GO
DBCC SHRINKFILE (databasenaem_log,5)
GO
ALTER DATABASE DatabaseName SET RECOVERY FULL
GO
I checked the sys.databases, log_reuse_wait_desc column and it shows "replication", this may be the reason log file won't allow shrinking. The problem is that there is no replication (publisher or subscriber) on the on the Database or server.
select name, log_reuse_wait_desc from sys.databases
Do I need to set up replication and turn off?
This is an issue that I have encountered before. Sometimes it is because Replication was configured and then removed while the replication processes were still running, sometimes the cause isn't known (DBCC CheckDB with REPAIR ALLOW DATA LOSS seems to be a frequent cause), but the best way I ever discovered to fix the issue is with the information in this MSDN article.
Basically, you set up replication with SNAPSHOT replication, then remove the replication.
This will clear the Replication hold on the log, and allow you to shrink it.
I am using SQL Server express edition with 10 GB Size limit, There some table having millions + rows and I need to perform data purging operation at the end of every month, so as to free up the space and make it available for new data storage
If I use the shrink through SSMS(Right click on database then task then shrink ) it works smoothly and free up the space as expected.
But the problem arises when I try to achieve the same with
ALTER DATABASE MyDatabase SET RECOVERY SIMPLE
GO
DBCC SHRINKDATABASE(MyDatabase)
GO
ALTER DATABASE MyDatabase SET RECOVERY FULL
GO
It literally show no effect, infact it increase the log file size but never freeing up the space.Is there any solution,it will be greatly appriciable
Thanks in advance
I would not switch the recovery model to SIMPLE to force shrinking.
When the recovery model is set to FULL you must execute a full backup and at least one transaction log backup to fulfil the recovery condition before you can shrink the database.
I have a spring project, when I start the server a named file 'tempdb' is created in the sql server directory, the size of this file is too big (reaches 8G)
I like to know why this file is created? is there a way to reduce its size?
Thanks in advance
run this
-- use the proper DB
USE tempdb;
GO
-- sync
CHECKPOINT;
GO
-- drop buffers
DBCC DROPCLEANBUFFERS;
DBCC FREEPROCCACHE;
DBCC FREESYSTEMCACHE('ALL');
DBCC FREESESSIONCACHE;
GO
--shrink db (file_name, size_in_MB)
DBCC SHRINKFILE (TEMPDEV, 1024);
GO
Either, you can always restart whole Database (like service restart), it will also drop the tempdb.
The tempdb system database is a global resource that is available to all users that are connected to an instance of SQL Server. The tempdb database is used to store the following objects: user objects, internal objects, and version stores. It is also used to store Worktables that hold intermediate results that are created during query processing and sorting.
You can use "ALTER DATABASE tempdb" command to limit/shrink the size of this.
The size 8Gb is suspicious though as tempdb size is seen mostly in MBs. You might want to check for any ugly queries when you're starting up your server.
I have a SQL Server 2005 database from which I'm removing several large tables to an archive database. The original database should shrink considerably.
To make the archive database, I was going to restore a copy of the original and just remove the current tables from that.
Is this the best way to go about it? What should I do with logs/shrinking to make sure the final sizes are as small as possible? The archive database may grow a little, but the original continues its normal growth.
That seems like an ok way to do it. Set the recovery model to simple, then truncate and shrink the log files. This will make it as small as possible.
See here for a good way to do it.
Note: This assumes you don't want or need to recover the archive database back to specific points in time. The reason being that Simple recovery model does not save the transactions in a transaction log. So as your archive database changes "a little" (as you said), it won't save the transactions to a log.
I use this script and this is very useful in developing.
BACKUP log [CustomerServiceSystem] with truncate_only
go
DBCC SHRINKDATABASE ([CustomerServiceSystem], 10, TRUNCATEONLY)
go
Redesign the db
Try one of these sql commands:
DBCC SHRINKDATABASE
DBCC SHRINKFILE
Or right click into Sql Server Management Studio Object Explorer's database and select Tasks-Shrink.