Category: SQL Server

  • PowerShell script to find files that are consuming the most disk space

    Problem

    As you know, SQL Server databases and backup files can take up a lot of disk space.  When disk is running low and you need to troubleshoot disk space issues, the first thing to do is to find large files that are consuming disk space.  In this article I will show you a PowerShell script that you can use to find large files on your disks.

    Solution

    http://www.mssqltips.com/sqlservertip/2774/powershell-script-to-find-files-that-are-consuming-the-most-disk-space/

  • Local System, Network Service and Local Service

    You have often seen the above default service accounts while configuring the SQL Services. Let’s see what the difference between these accounts is.

     


     

     

    Local System: Completely trusted account, more than the Administrator account. There is nothing on a single box that this account cannot do and it has the right to access the network as the machine. However local system account might restrict the SQL Server interaction with the other server. The actual name of the account is “NT AUTHORITY\SYSTEM”

     

    Take example of “Lock Pages in Memory“, it will default granted to this account. No need to explicitly specify it.

     

    Network Service: has more access to resources and objects than members of the Users group. Services that run as the Network Service account access network resources by using the credentials of the computer account. The actual name of the account is “NT AUTHORITY\NETWORK SERVICE“. This account is far more limited than the Limited than Local System account.

     

    Local Service: A limited service account that is very similar to Network Service and meant to run standard least-privileged services. However unlike Network Service it has no ability to access the network as the machine.
    This limited access helps safeguard the system if individual services or processes are compromised.
    Services that run as the Local Service account access network resources as a null session without credentials.
    The actual name of the account is “NT AUTHORITY\LOCAL SERVICE“. Local Service account is not supported for the SQL Server or SQL Server Agent services.


     

  • DBCC DROPCLEANBUFFERS and CHECKPOINT

    DBCC DROPCLEANBUFFERS: is very useful command while doing the performance tuning of the queries. We can use DBCC DROPCLEANBUFFERS to test queries with a cold buffer cache without shutting down and restarting the server.

    Before we get into more detail, Let’s take a look sys.dm_os_buffer_descriptors DMV.

    sys.dm_os_buffer_descriptors
    Returns information about all the data pages which are currently in the SQL Server buffer pool, Output of the DMV will help us to determine the distribution of database pages in the buffer pool.

    As you might know, when a data page is read from disk, it will first copy to buffer pool and cached for reuse. Each cached data page has one buffer descriptor.

    Sys.dm_os_buffer_descriptors returns cached pages for all user and system databases.

    You can use below query to check the buffer descriptor for the current database.

    use SQLMonitor
    select sysObj.name,* 
    from sys.dm_os_buffer_descriptors bufferDescriptors
    INNER JOIN sys.allocation_units AllocUnits ON bufferDescriptors.allocation_unit_id = AllocUnits.allocation_unit_id
    INNER JOIN sys.partitions Partitions ON AllocUnits.container_id = Partitions.hobt_id
    INNER JOIN sys.objects sysObj ON Partitions.object_id = sysObj.object_id
    WHERE bufferDescriptors.database_id = DB_ID()
    AND sysObj.is_ms_shipped = 0
    

    From the above image, you can see that query has return 58293 rows, which means that number of pages. Now let’s execute the DBCC DROPCLEANBUFFERS:, it should clear all the pages.

    Execute below command, and again execute the sys.dm_os_buffer_descriptors bufferDescriptors query.

    DBCC DROPCLEANBUFFERS
    
    use SQLMonitor
    select sysObj.name,* 
    from sys.dm_os_buffer_descriptors bufferDescriptors
    INNER JOIN sys.allocation_units AllocUnits ON bufferDescriptors.allocation_unit_id = AllocUnits.allocation_unit_id
    INNER JOIN sys.partitions Partitions ON AllocUnits.container_id = Partitions.hobt_id
    INNER JOIN sys.objects sysObj ON Partitions.object_id = sysObj.object_id
    WHERE bufferDescriptors.database_id = DB_ID()
    AND sysObj.is_ms_shipped = 0
    

    See the results of sys.dm_os_buffer_descriptors, still it has return 154 rows which means all the pages from buffer pool is not cleared.

    The ideal method to clean the buffer pool is, you have to execute the CHECKPOINT command before executing the DBCC DROPCLEANBUFFERS command.

    CHECKPOINT Writes all dirty pages for the current database to disk. Dirty pages are data pages that have been entered into the buffer cache and modified, but not yet written to disk. Checkpoints save time during a later recovery by creating a point at which all dirty pages are guaranteed to have been written to disk.

    CheckPoint will help us to produce the cold buffer cache.

    --Execute the checkpoint command
    CHECKPOINT
    --Execute DBCC
    DBCC DROPCLEANBUFFERS
    --Execute below query to check the buffer descriptors
    use SQLMonitor
    select sysObj.name,* 
    from sys.dm_os_buffer_descriptors bufferDescriptors
    INNER JOIN sys.allocation_units AllocUnits ON bufferDescriptors.allocation_unit_id = AllocUnits.allocation_unit_id
    INNER JOIN sys.partitions Partitions ON AllocUnits.container_id = Partitions.hobt_id
    INNER JOIN sys.objects sysObj ON Partitions.object_id = sysObj.object_id
    WHERE bufferDescriptors.database_id = DB_ID()
    AND sysObj.is_ms_shipped = 0
    

    I hope above explanation has clear all your doubts regarding how to DROPCLEANBUFFERS, Keep Reading…

  • How to grant the execute permission to User on all the procedure of the database?

    SQL Server 2005 onward, we can create the roles in database. You can follow below simple script to grant the EXECUTE permission to user.

     

    -- creating the database role 
    CREATE ROLE Database_Executor
    -- granting the execute permission to database role
    GRANT EXECUTE TO Database_Executor
    
    -- Here I am granting the SQLDBPool login execute permission
    USE [DBName]
    GO
    CREATE USER [SQLDBPool] FOR LOGIN [sqldbpool]
    GO
    USE [DBName]
    GO
    EXEC sp_addrolemember N'Database_Executor', N'sqldbpool'
    GO