R_time_use
========================================
SELECT @@VERSION
SELECT SERVERPROPERTY('productversion'), SERVERPROPERTY ('productlevel'), SERVERPROPERTY ('edition')
========================================
who took the backup:
select user_name, backup_start_date from backupset where database_name='mysupplier' and type='D' order by backup_Start_date desc
========================================
To minimise the disk space the test copy uses I tried to shrink the log using:
BACKUP LOG MyDatabase WITH TRUNCATE_ONLY
This returns an error: "The log was not truncated because records at the beginning of the log are pending replication. Ensure the Log Reader Agent is running or use sp_repldone to mark transactions as distributed."
********************
Do the following
Run sp_replicationdboption 'database name','publish',true'
then run sp_repldone with the following parameters
EXEC sp_repldone @xactid = NULL, @xact_segno = NULL, @numtrans = 0, @time = 0, @reset = 1
then unpublish the database
sp_replicationdboption 'database name','publish','false'
Run
dbcc traceon(3604)
dbcc opentran --- against the database to ensure there are no replicated transactions.
At this point you should be able to truncate the log
========================================
/*
This script used for list all database space allocated and its free space
revision history :
28 Feb 2007 - Solihin - www.solihinho.wordpress.com
compatibility : SQL Server 2000 and SQL 2005
*/
SET NOCOUNT ON
IF OBJECT_ID('tempdb..#DBSTATS') IS NOT NULL
BEGIN
DROP TABLE #DBSTATS
END
CREATE TABLE #DBSTATS (
dbname sysname,
lname sysname,
usage varchar(20),
[size] decimal(9, 2) NULL ,
[used] decimal(9, 2) NULL
)
IF OBJECT_ID('tempdb..#temp_log') IS NOT NULL
BEGIN
DROP TABLE #temp_log
END
CREATE TABLE #temp_log
(
DBName sysname,
LogSize real,
LogSpaceUsed real,
Status int
)
IF OBJECT_ID('tempdb..#temp_sfs') IS NOT NULL
BEGIN
DROP TABLE #temp_sfs
END
CREATE TABLE #temp_sfs
(
fileid int,
filegroup int,
totalextents int,
usedextents int,
name varchar(1024),
filename varchar(1024)
)
DECLARE @dbname sysname
,@sql varchar(8000)
IF OBJECT_ID('tempdb..#temp_db') IS NOT NULL
BEGIN
DROP TABLE #temp_db
END
SELECT name INTO #temp_db
FROM master.dbo.sysdatabases
WHERE DATABASEPROPERTY(name,'IsOffline') = 0
AND has_dbaccess(name) = 1
ORDER BY name
WHILE (1 = 1)
BEGIN
SET @dbname = NULL
SELECT TOP 1 @dbname = name
FROM #temp_db
ORDER BY name
IF @dbname IS NULL
GOTO _NEXT
SET @sql = ' USE ' + @dbname + '
TRUNCATE TABLE #temp_sfs
INSERT INTO #temp_sfs
EXECUTE(''DBCC SHOWFILESTATS'')
INSERT INTO #DBSTATS (DBNAME, LNAME, USAGE, [SIZE], [USED])
SELECT db_name(), NAME, ''Data''
, totalextents * 64.0 / 1024.0
, usedextents * 64.0 / 1024.0
FROM #temp_sfs
INSERT INTO #DBSTATS (DBNAME, LNAME, USAGE, [SIZE], [USED])
SELECT db_name(), name, ''Log'', null, null
FROM sysfiles
WHERE status & 0x40 = 0x40'
EXEC(@sql)
DELETE FROM #temp_db WHERE name = @dbname
END
_NEXT:
INSERT INTO #Temp_Log
EXECUTE ('DBCC SQLPERF(LOGSPACE)')
UPDATE #DBSTATS
SET SIZE = B.LogSize
, USED = LogSize * LogSpaceUsed / 100
FROM #DBSTATS A
INNER JOIN #Temp_Log B
ON (A.DBNAME = B.DBNAME)AND(A.Usage = 'LOG')
SELECT dbname AS [database name],
lname AS [logical data name],
usage,
[size] AS [space allocated (MB)],
used AS[space used (MB)],
[size] - used AS [free space (MB)],
cast(used/[size]*100 AS numeric(9,2)) AS [space used %],
cast(100-(used/[size]*100) AS numeric(9,2)) AS [free space %]
FROM #DBSTATS
ORDER BY dbname, usage
DROP TABLE #DBSTATS
DROP TABLE #temp_db
DROP TABLE #temp_sfs
DROP TABLE #temp_log
SET NOCOUNT OFF
========================================
USE dbname;
GO
DBCC SHRINKFILE (filename or fileid,MB)
========================================
Separating the log files from data files:
1 Backup Transaction Log.
2 Shrink the Transaction Log Files
3 Allow the initial transaction log file to auto grow (currently auto grow is disabled)
4 Delete the newly added transaction log file.
5 Note down the Database Owner
6 Detach the Database
7 Move the Transaction Log file from R Drive to T Drive
8 Attach the database (point transaction log to new location) under same database owner.
9 Health check on Database.
10 Same steps should be done for each database
========================================
Finding user tables:
select distinct name from sysobjects where xtype='U'
========================================
Retrive latest backup from QA :
SELECT db.name AS [Database],
max(backup_finish_date)
AS [LastBackupDate]
FROM
[master].[dbo].[sysdatabases]
db LEFT OUTER JOIN
[msdb].[dbo]
.[backupset] bs
ON bs.database_name
= db.name
AND bs.type = 'D'
GROUP BY db.name
ORDER BY db.name
========================================
REPAIR_ALLOW_DATA_LOSS:
------------------------
ALTER DATABASE BIJOU
SET single_user WITH ROLLBACK IMMEDIATE;
go
DBCC checkdb ('BIJOU', repair_allow_data_loss);
go
----------------
SET multi_user WITH ROLLBACK IMMEDIATE;
------
dbcc checkdb(BIJOU)
----
========================================
Rename DB:
USE master
ALTER DATABASE career_res
SET single_user
GO
ALTER DATABASE career_res
MODIFY NAME = CAREER
GO
ALTER DATABASE CAREER
SET multi_user
GO
========================================
Remove transaction log files from disk :
Option 1
----------------
Begin Tran
declare @Date nvarchar(50)
declare @Time datetime
set @Time = DateAdd(hh, -72, GetDate())
set @Date = (Select Replace(Convert(nvarchar, @Time, 111), '/', '-') + 'T' + Convert(nvarchar, @Time, 108))
Select @Time,@Date
EXECUTE master.dbo.xp_delete_file 0,N'E:\Microsoft SQL Server\MSSQL.1\MSSQL\Backup\Transaction Log backups',N'Trn',@Date,1
RollBack Tran
-------------------------
Option 2
------------------
declare @dt datetime
select @dt=getdate()-3 --No days to delete
EXECUTE master.dbo.xp_delete_file 0,N'E:\Microsoft SQL Server\MSSQL.1\MSSQL\Backup\Transaction Log backups',N'*trn*',@dt
-----------------
========================================
Reindexing on all tables in a DB :
exec sp_msforeachtable @command1="Print '?' DBCC DBREINDEX ('?')"
Checkdb command for all the db's:
EXEC sp_MSforeachdb @command1="print '?' DBCC CHECKDB ('?')"
========================================
UPDATE ALL STATISTICS FOR ALL TABLES in the Database :
If you ever see a Query that is never Completing or very Slow on a Database. i.e If the performance Differs on TEST vs PROD Database, Check to see if the STATISTICS FOR the TABLE have been updated.
use this Query to UPDATE ALL STATISTICS FOR ALL TABLES in the Database,
USE <DATABASE NAME>
SELECT 'UPDATE STATISTICS '+TABLE_NAME FROM INFORMATION_SCHEMA.TABLES
wHERE TABLE_TYPE='BASE TABLE'
Copy all from the ResultSet and Paste it in a new Query wWindow and Execute.
========================================
SQL SERVER - TempDB is Full. Move TempDB from one drive to another drive:
Make sure that TempDB is set to autogrow and do not set a maximum size for TempDB. If the current drive is too full to allow autogrow events, then arrange a
bigger drive, or add files to TempDB
on another device (using ALTER DATABASE as described below and allow those files to autogrow.
Move TempDB from one drive to another drive. There are major two reasons why TempDB needs to move from one drive to other drive.
1) TempDB grows big and the existing drive does not have enough space.
2) Moving TempDB to another file group which is on different physical drive helps to improve database disk read, as they can be read simultaneously.
Follow direction below exactly to move database and log from one drive (c:) to another drive (d:) and (e:).
Open Query Analyzer and connect to your server. Run this script to get the names of the files used for TempDB.
USE TempDB
GO
EXEC sp_helpfile
GO
Results will be something like:
name fileid filename filegroup size
------- ------ -------------------------------------------------------------- ---------- -------
tempdev 1 C:Program FilesMicrosoft SQL ServerMSSQLdatatempdb.mdf PRIMARY 16000 KB
templog 2 C:Program FilesMicrosoft SQL ServerMSSQLdatatemplog.ldf NULL 1024 KB
along with other information related to the database. The names of the files are usually tempdev and demplog by default. These names will be used in next
statement. Run following code, to move mdf and ldf files.
USE master
GO
ALTER DATABASE TempDB MODIFY FILE (NAME = tempdev, FILENAME = 'd:datatempdb.mdf')
GO
ALTER DATABASE TempDB MODIFY FILE (NAME = templog, FILENAME = 'e:datatemplog.ldf')
GO
The definition of the TempDB is changed. However, no changes are made to TempDB till SQL Server restarts. Please stop and restart SQL Server and it will
create TempDB files in new locations.
========================================
========================================
DYNAMIC MANAGEMENT VIEWS:
Sometimes we wish to know what locks are being held by the transaction. Also it would be great to know what SPID owns the transaction, on which objects the locks are being held, what SQL statement caused the locks, etc...
With the introduction of DMV's in SQL Server 2005 getting this information is quite easy with this query:
SELECT L.request_session_id AS SPID, DB_NAME(L.resource_database_id) AS DatabaseName, O.Name AS LockedObjectName, P.object_id AS LockedObjectId, L.resource_type AS LockedResource, L.request_mode AS LockType, ST.text AS SqlStatementText, ES.login_name AS LoginName, ES.host_name AS HostName, TST.is_user_transaction as IsUserTransaction, AT.name as TransactionName, CN.auth_scheme as AuthenticationMethod FROM sys.dm_tran_locks L JOIN sys.partitions P ON P.hobt_id = L.resource_associated_entity_id JOIN sys.objects O ON O.object_id = P.object_id JOIN sys.dm_exec_sessions ES ON ES.session_id = L.request_session_id JOIN sys.dm_tran_session_transactions TST ON ES.session_id = TST.session_id JOIN sys.dm_tran_active_transactions AT ON TST.transaction_id = AT.transaction_id JOIN sys.dm_exec_connections CN ON CN.session_id = ES.session_id CROSS APPLY sys.dm_exec_sql_text(CN.most_recent_sql_handle) AS ST WHERE resource_database_id = db_id() ORDER BY L.request_session_id
Let's look at it one DMV at a time from top to bottom:
sys.dm_tran_locks:
Shows us all locks held on all resources for all transaction. We start from this view since we want to get information about locked resources.
sys.partitions:
Contains information about partitions in the database. We can join it to the sys.dm_tran_locks on the hobt_id only for resource_type values 'HOBT', 'Page', 'RID' and 'Key'. With this join we get the object_id of our locked table.
sys.objects:
Contains data for all schema scoped database objects. We join it to sys.partitions to get the Name of the locked object which is usually a table name.
sys.dm_exec_sessions:
Shows data about all active sessions on SQL server. We join it to sys.dm_tran_locks to get user login information for each held lock.
sys.dm_tran_session_transactions:
Shows data for all transactions for a session. By joining in to sys.dm_exec_sessions we get a link to all active transactions in the session.
sys.dm_tran_active_transactions:
Shows data for all active transactions in SQL Server. By joining it to sys.dm_tran_session_transactions we only get information for the session we want.
sys.dm_exec_connections:
Shows data for all connections in SQL Server. By joining it to sys.dm_exec_sessions we get connection info for our session.
sys.dm_exec_sql_text:
Returns SQL statement that is associated with the SQL handle input parameter. By cross applying it to the sys.dm_exec_connections we get the last executed statement for the connection, which in our case is the statement that is holding locks.
By applying the filter in the where clause you get the answers to questions like:
- What SQL Statement is causing the lock?
- Which user has executed the SQL statement that's holding the locks?
- What objects/tables are being locked?
- What kinds of locks are being held and on which pages, keys, RID's?
- etc...
The data about missing indexes is stored in the following DMV’s which all exclude info about spatial indexes:
sys.dm_db_missing_index_groups
This DMV returns only 2 columns with information about which indexes are in which group.
sys.dm_db_missing_index_group_stats
This DMV returns information about each missing indexes group. It returns info like the estimated average impact or how many seeks, scans and compilations/recompilations would benefit from adding the missing indexes.
sys.dm_db_missing_index_details
This DMV returns detailed information about each missing index like table name that is missing an index and CSV’s of columns that the index would be beneficial on.
sys.dm_db_missing_index_columns
This a Dynamic Management Function (DMF) that accepts an index_handle parameter. It returns columns that should be in the suggested index identified with the index_handle that can be obtained from sys.dm_db_missing_index_details and sys.dm_db_missing_index_groups. It does not include spatial indexes. The column named column_usage returns info on how this column would benefit for a particular index. EQUALITY and INEQUALITY mean that the column would be used in a where clause predicate. INCLUDE means that the column should be an included column on an existing non-clustered index.
Finding unused indexes
Since SQL Server keeps data of all used indexes, getting the unused indexes is a simple matter of comparing used indexes to all existing indexes. Those that exist but are not used are of course unused indexes.
SELECT OBJECT_SCHEMA_NAME(I.OBJECT_ID) AS SchemaName, OBJECT_NAME(I.OBJECT_ID) AS ObjectName, I.NAME AS IndexName FROM sys.indexes I WHERE -- only get indexes for user created tables OBJECTPROPERTY(I.OBJECT_ID, 'IsUserTable') = 1 -- find all indexes that exists but are NOT used AND NOT EXISTS ( SELECT index_id FROM sys.dm_db_index_usage_stats WHERE OBJECT_ID = I.OBJECT_ID AND I.index_id = index_id -- limit our query only for the current db AND database_id = DB_ID()) ORDER BY SchemaName, ObjectName, IndexName
Limitations
This cool feature of course has some limitations. Some of those are:
Database Engine Tuning Advisor kicks its behind. Think of Missing Indexes DMV’s as a really lightweight DTA. So when doing a really in-depth index analysis, don’t rely only on the Missing Indexes DMV’s. Just start with them.
Missing Indexes DMV’s don’t provide any information about new index overhead like space or IO/CPU overhead on updates, inserts and deletes.
There’s no information about the column order in the suggested index or whether it should be clustered or non-clustered.
Missing Indexes DMV’s consider only per query indexes and not per workload indexes.
Missing Indexes DMV’s can track a maximum of 500 indexes.
Trivial execution plans (plans for really simple SQL Statements) are not considered.
Rebuilding the SQL Server master database in MSSQL 2005
Permon_counters :
------------
Avg.Disk Sec/Read
1 )Monitoring SQL Server disk I/O with PerfMon counters::
<8 Msec or less as optionalpoor >20 Msec
Good <20 Msec
Better <12 Msec
Best <8 Msec
Avg.Disk Sec/Write
<8 Msec on non-cached disks
<1 Msec on cached disks as optional
non-cached writes cached writes
poor >20 Msec poor >4 Msec
Good <20 Msec Good <4 Msec
Better <12 Msec Better <2 Msec
Best <8 Msec Best <1 Msec
Avg.Disk Queue Length and Current Disk Queue Length
=========
2 ) SQL Server PerfMon counters for tracking Windows memory::
Memory:Available Mbytes (The amount of physical memeory available to run processes on the machine)
should be >100 MB
Memory:Pages Input/Sec ( The rate at which pages are rear from disk)
should<10
Memory:Pages/Sec ( The rate at which pages are rear from or written to disk)
-Investigate if >100 on slow disk subsystem or >600 on faster disk subsystem
Memory Manager
Memory Manager:Memory Grants Pending
-The current number of processes waiting for a workspace memory grant
- Should be at or close to zero. A higher or rising valuse may indicate insufficient memory
Memory Manager:Page Life Expectncy
-The time ,in seconds, that a page stay in the memory pool without being referenced before it is flush.
- Should be >=300 (5 min) .A lower or declining value may indicate memory pressure.
===========
Sql server Access Methods
Forwarded Records/sec
-Tables with records traversed by a point
- <10 per 100 Batch Requests/sec
Index searches/sec and Full scans/sec
- Index searches versus the number of ull table or clustered index scans
- (index search/sec)/(full scans/sec) >100
Pages splits/sec
-occurs when a 8kb page fills and must be split into two new 8kb pages
- < 20 per 100 Batch Requests/sec (or as low as possible)
fill factor should 70 - 80 % will be good
Sql server : Buffer Manager
Buffer cache Hit ratio
- Indicates how SQL server can get data from the buffer rather than disk
- >90% OLAP, >95% for OLTP
Free List STalls/sec
-The frequency that requestes for database buffer pages are suspended because there's no buffer available
- <2
Free Pages
- The total num of 8Kb data on all free lists
- >640
Lazy Writer/sec
- the num of times per second that lazywriter moves dirty pages from buffer to disk to free buffer space
- <20
Page Life Expectancy
- Num of seconds a data page stay in the buffer
- > 300 , otherwise memory pressure is at play
Page Lookups/sec
-Num of requests to find a page in the buffer
- (page lookups/sec)/(Batch Requests/sec) < 100
Page Reads/sec and Page writes/sec
- Num of physical database page reads and writes issued, respectively
- < 90 ( >90 not good)
Top 10 SQL Server 2008 Features for the Database Administrator (DBA)
------------
Following query to identify queries using a lot of CPU :
select top 50
st.objectid, st.dbid,
-- object_name(st.objectid, st.dbid), -- uncomment this line if you're using SQL 2005 SP2
total_worker_time/execution_count AS AverageCPUTime,
CASE statement_end_offset
WHEN -1 THEN st.text
ELSE SUBSTRING(st.text,statement_start_offset/2,statement_end_offset/2)
END AS StatementText
from
sys.dm_exec_query_stats qs CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) st
ORDER BY AverageCPUTime DESC