Friday, 8 January 2016

Finding out how much memory SQL Server is utilizing?


Many time for a new DBA  hard to find how much My Server utilising memory? Here is explained a number of ways to find the memory utilized by SQL server.
By Using Performance monitor:
 Go to Start–>Run and type perfmon . You will find the following screen where you can see the total memory used(the pages/sec) by your box.

By Using DMV:
You can Query to sys.dm_os_memory_clerks DMV to find out Memory utilization by SQL server. To see how see the following query
select name,type, 
sum(single_pages_kb+multi_pages_kb) as MemoryKB
 from sys.dm_os_memory_clerks
group by name,type
order by memorykb desc
By using SQL Reports:
Right Click on SQL Server–>reports–>Standard Reports–> Memory Consumption
See the Following image.



Microsoft SQL Server, Error: 5120 (Access Denied)

Problem:
I am trying to attach a database into MS SQL server 2012/ Altering from Read only true to Read only fals;
The following error is occurred:
Unable to open the physical file “, filename”, Operating system error 5: “(Access Denied)”. (Microsoft SQL Server, Error: 5120)

Cause:
The associated file/s (.mdf or .ldf) read only or SQL (user) do not have permission to access file in the location it saved.
Solution:
Browse the location of file/s (.mdf or .ldf) right click on file –>Properties


Click On security Tab –> Select the user (for me it is Everyone) –>click on Edit
And check the full control/ Modify option in Allow column–> Apply –>OK
Do the same for all other associated files, if any.
Now it’s completed. Try what are you doing.

Thursday, 3 September 2015

How to shrink tempdb in Different Ways

There may come a time when you might want to shrink tempdb because it has become too large.
There are a few ways you can do this and I have listed them below but please read to the end of the post before making a decision on which way you want to approach this. There is an important note at the end of the post.
So first, we’ll look at the configuration on my server
1SELECT name, size
2FROM sys.master_files
3WHERE database_id = DB_ID(N'tempdb');
name                 size
-------------------- -----------
tempdev              1280
templog              640

(2 row(s) affected)
Note that the size column is listing the size of the file in 8Kb pages. In this instance my “tempdev” file is 10Mb (( 1280 * 8 ) = 10240 kb)

How to shrink tempdb using DBCC SHRINKFILE

The syntax is as follows and the operation does not require a restart of the sql server service.
DBCC SHRINKFILE(logical_filename, size_in_MB)
So I will shrink the file to 5Mb
1DBCC SHRINKFILE(tempdev, 5);
Which produces the following output and I can see that CurrentSize is now 50% smaller than previously
DbId   FileId      CurrentSize MinimumSize UsedPages   EstimatedPages
------ ----------- ----------- ----------- ----------- --------------
2      1           640         288         176         176

(1 row(s) affected)
DBCC execution completed. If DBCC printed error messages, contact your system administrator.
name                 size
-------------------- -----------
tempdev              640
templog              640

(2 row(s) affected)
Don’t try and increase filesizes in tempdb using this command because you will see an error. In this example, the attempt was to increase to 50Mb.
Cannot shrink file ‘1’ in database ‘tempdb’ to 6400 pages as it only contains 640 pages.

How to shrink tempdb using DBCC SHRINKDATABASE

The syntax is as follows and does not require a restart of the SQL Server service:
DBCC SHRINKDATABASE(tempdb, ‘target_percentage_of_free_space’);
So if the data files in tempdb had enough free space, you could shrink tempdb by running this command to leave 10% of free space at the end of the files:
1DBCC SHRINKDATABASE(tempdb, 10);

How to shrink tempdb using ALTER DATABASE

As in my post about moving tempdb, you can use the ALTER DATABASE command to perform a tempdb resize. A restart of the SQL Server service will shrink tempdb to its original predefined size but you can also resize tempdb using ALTER DATABASE.
The following script will resize both the log and data file to be 100Mb.
1USE master;
2GO
3ALTER DATABASE tempdb
4MODIFY FILE (NAME = tempdev, SIZE=100Mb);
5GO
6ALTER DATABASE tempdb
7MODIFY FILE (NAME = templog, SIZE=100Mb);
8GO

How to shrink tempdb using Management Studio

You can also use Management Studio to resize Tempdb and perform shrink operations by right mouse clicking the tempdb database and choosing Tasks->Shrink.
To resize tempdb, you can set the file sizes by right mouse clicking the tempdb and choosing Properties->Files and setting the sizes there.
Note that with both the ALTER DATABASE and management studio methods, you cannot resize a tempdb file to a particular size if the data contained in the file exceed the size that you are trying to size to.

Should you shrink TempDB?

It is documented in the Microsoft article that it can cause consistency errors in the TempDB database if you perform a shrink operation while the database is in use so please read this carefully and consider whether you can shrink the database by other means, i.e restarting the SQL Server instance which will create a brand new copy of TempDB releasing the disk space.

Tempdb won’t shrink?

I sometimes get asked about why tempdb won’t shrink when using one of the methods which does not involve a SQL Server service restart. Microsoft recommends that if possible, shrinking of tempdb should be done either in single user mode or when there is no current tempdb activity. I have extracted the quote below from the article that is linked to in the previous section.
It is safe to run shrink in tempdb while tempdb activity is ongoing. However, you may encounter other errors such as blocking, deadlocks, and so on that can prevent shrink from completing. Therefore, in order to make sure that a shrink of tempdb will succeed, we recommend that you do this while the server is in single-user mode or when you have stopped all tempdb activity.