Restored SQL Server database taking too much space [duplicate] - sql-server

This question already has answers here:
My log file is too large
(9 answers)
SQL Server Log File Is Huge
(4 answers)
SQL Server 2008 log file size is large and growing quickly
(4 answers)
Closed 7 months ago.
I generated .bak file from the production server. The size was around 2.2 GB. When I restored this file on my laptop the database _log.ldf file takes about 44 GB space. I then used Database right click -> Tasks -> Generate scripts menu. I chose both schema and data in advanced options and generated the script. These are the lines in beginning of script.
USE [master]
GO
/****** Object: Database [GRF_MDS] Script Date: 05-08-2022 14:18:42 ******/
CREATE DATABASE [GRF_MDS]
CONTAINMENT = NONE
ON PRIMARY
( NAME = N'GRF_MDS', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\MSSQL\DATA\GRF_MDS.mdf' , SIZE = 859584KB , MAXSIZE = UNLIMITED, FILEGROWTH = 65536KB )
LOG ON
( NAME = N'GRF_MDS_log', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\MSSQL\DATA\GRF_MDS_log.ldf' , SIZE = 45686784KB , MAXSIZE = 2048GB , FILEGROWTH = 65536KB )
WITH CATALOG_COLLATION = DATABASE_DEFAULT
I tried changing GRF_MDS_log.ldf size to 1 GB and ran the script but Datagrip hanged due to memory issues as the script was around 3 GB in size. I guess I can solve this problem by creating an application that reads the script line by line and executes. But is there any available option to reduce size? I have already tried Tasks -> Shrink -> database/ files menu.

Related

When create database in SQL Server 2016 log file size is huge

Below query used to create database in SQL Server 2016
CREATE DATABASE TestDB_v821 ON ( Name = 'TestDB_v821', FILENAME = 'E:\Data\TestDB_v821.mdf', FILEGROWTH = 10%)
LOG ON ( NAME = 'TestDB_v821_Log', FILENAME = 'E:\Log\TestDB_v821_Log.ldf', FILEGROWTH = 10%)
The query taking more time than normal. When i check the log file size using select * from sys.dm_db_log_space_usage
database_id total_log_size_in_bytes used_log_space_in_bytes used_log_space_in_percent log_space_in_bytes_since_last_backup
----------- ----------------------- ----------------------- ------------------------- ------------------------------------
17 2508193792 8749568 0.3488394 225280
Need to know why the simple create database statement creates the hug log file. It is same behavior when try to create other database also. Is it due to some setting. Please help me on this.
It will take the initial size and auto-growth settings from the model database, so you need to reduce it there if you want smaller defaults.

multiple DB backups using a variable name

I have the current script to backup the 3 databases in use (All 3 DB are inter linked). What I would like to do is set the name to be used as a variable which is used for each db backup (rather than changing it 3 times before each execution. Any help would be greatly appreciated.
BACKUP DATABASE DB1
TO DISK = 'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\Backup\DB1\TC1\STEP_10.bak' -- Amend the filename
WITH FORMAT,
MEDIANAME = 'C_Program Files_Microsoft SQL Server_MSSQL11.MSSQLSERVER_MSSQL_Backup',
NAME = 'DB1';
GO
USE GATEKEEPER_ELEC;
BACKUP DATABASE GATEKEEPER_ELEC
TO DISK = 'C:\Program Files\Microsoft SQL
Server\MSSQL11.MSSQLSERVER\MSSQL\Backup\DB2\TC1\STEP_10.bak' -- Amend the filename
WITH FORMAT,
MEDIANAME = 'C_Program Files_Microsoft SQL Server_MSSQL11.MSSQLSERVER_MSSQL_Backup',
NAME = 'DB2';
GO
USE PFV;
BACKUP DATABASE PFV
TO DISK = 'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\Backup\DB3\TC1\STEP_10.bak' -- Amend the filename
WITH FORMAT,
MEDIANAME = 'C_Program Files_Microsoft SQL Server_MSSQL11.MSSQLSERVER_MSSQL_Backup',
NAME = 'DB3';

T-SQL Script to get tempDB location that will return True or False

I'm making an automation script to move tempDB files to another disk. I'm using DSC (with xSQLServerScript modules) to ensure consistent environment across multiple servers. In order not to touch servers that have tempDB in the right location already I need to reference a script that will return only True or False value. As I'm an SQL noob I would really appreciate if somebody would provide an example for such script.
If that makes any difference here is the T-SQL script I use to move tempDB files:
Alter database tempdb modify file (name = tempdev, filename = 'D:\Sqldata\tempdb.mdf', SIZE = 1000MB, MAXSIZE = UNLIMITED, FILEGROWTH = 20%)
Alter database tempdb modify file (name = templog, filename = 'D:\Sqldata\templog.ldf', SIZE = 100MB, MAXSIZE = UNLIMITED, FILEGROWTH = 20%)
Somewhat unclear on what you want here but think you want to know if all files for tempdb are located on the D drive. Since I don't much care for strings representing true/false I am using the bit datatype here. The bit datatype will convert to 1 for any value that is not null and not 0.
select convert(bit, COUNT(*))
from tempdb.sys.sysfiles
where left(filename, 1) = 'D'

Create database in stored procedure [duplicate]

This question already has answers here:
SQL Server: use parameter in CREATE DATABASE
(2 answers)
Closed 6 years ago.
How can I create a database via a stored procedure?
CREATE PROCEDURE CreateDataBase
#strDBPath nvarchar(MAX),
#strFileName nvarchar(50),
#strLOGPath nvarchar(MAX),
#strLOGFileName nvarchar(50)
AS
BEGIN
CREATE DATABASE [APPLICATION]
CONTAINMENT = NONE
ON PRIMARY
( NAME = strFileName, FILENAME = strDBPath, SIZE = 5120KB , FILEGROWTH = 1024KB )
LOG ON
( NAME = strFileName, FILENAME = strLOGPath , SIZE = 2048KB , FILEGROWTH = 10%)
END
GO
In SQL Server Management Studio everything seems alright. But in my Visual Studio database project the compiler complains about strDBPath and strLOGPath.
Error:
Incorrect Syntax near strDBPath
Because you try to use them as variables NOT USING THE PREFIX # in your statement. I am pretty sure, if you would READ the error message (quite telling you do not include it in your question here, you know) it would hint you in this direction.

How to create an SQL Server 2008 database from script

I'm trying to do an Entity Framework walkthrough so I:
downloaded SQL script here: http://www.learnentityframework.com
in SQL Server Management Studio, I right-clicked Database, Create Database, named it
right-clicked on the new database, New Query
clicked on "Open File" and opened the script file: Create_ProgrammingEFDB1_SQLServer2008.sql
clicked "! Execute"
But the script (756K) has been running for 10 minutes now and still says "executing..."
My questions are:
Is this the standard way to read in an SQL script into SQL Server?
Is it supposed to take this long? This is how I would do it in MySQL/PHPMyAdmin it it might take a couple seconds, so I assume I'm not doing something right.
Here is the beginning of the script, I changed the file paths so they point to the right .mdf and .ldf files:
****/
--PART ONE CREATE THE DATABASE. Note the file paths in the first few commands.
--Change them for your own computer.--
USE [master]
GO
/****** Object: Database [ProgrammingEFDB1] Script Date: 01/28/2009 10:17:44 ******/
CREATE DATABASE [ProgrammingEFDB1] ON PRIMARY
( NAME = N'ProgrammingEFDB1', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\ProgrammingEFDB1.mdf' , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
LOG ON
( NAME = N'ProgrammingEFDB1_log', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\ProgrammingEFDB1_log.LDF' , MAXSIZE = UNLIMITED, FILEGROWTH = 10%)
GO
ALTER DATABASE [ProgrammingEFDB1] SET COMPATIBILITY_LEVEL = 90
GO
IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))
begin
EXEC [ProgrammingEFDB1].[dbo].[sp_fulltext_database] #action = 'disable'
end
...
ANSWER:
I had already created a database with the same name so it was trying to create a database that was already there which made it hang for some reason. I deleted that database, reran the script and it completed successfully in 3 seconds.
I don't know what does your script do exactly in the next 754K, but the lines you posted seem quite harmless.
Try adding the following to your script:
SET STATISTICS TIME ON
This will show queries execution times as they run, and it will help you to locate the problem more exactly.
But the script (756K)
Must be a lot more than just a CREATE DATABASE in the script, so very hard to say when the script is doing.
You can write progress reports from the script back to the client, or use SQL Profiler to see what commands are being executed.

Resources