Tuesday, 27 May 2014

Setting Up Resource Governor

-- CREATE RESOURCE POOL FOR SQL AGENT JOBS
CREATE RESOURCE POOL
       SQLAgentJobsPool
WITH
(
       MIN_CPU_PERCENT=0,
       MAX_CPU_PERCENT=85,
       MIN_MEMORY_PERCENT=0,
       MAX_MEMORY_PERCENT=85
)
GO

-- CREATE WORKLOAD GROUP for SQL AGENT JOBS
CREATE WORKLOAD GROUP
       SQLAgentJobsGroup
USING
       SQLAgentJobsPool;
GO

-- CREATE UDF TO ASSIGN WORK LOAD GROUP
CREATE FUNCTION dbo.UDFAssign_RG_WorkLoadGroup()
RETURNS SYSNAME
WITH SCHEMABINDING
AS
BEGIN

DECLARE @WorkloadGroup AS SYSNAME

IF(SUSER_NAME() = 'Put SQLSERVERAgent Service Account Here')

       SET @WorkloadGroup = 'SQLAgentJobsGroup'


ELSE

       SET @WorkloadGroup = 'default'

RETURN @WorkloadGroup
END
GO


-- ALTER RESOURCE GOVERNOR TO USE WORK LOAD GROUP UDF
USE [master]
GO

ALTER RESOURCE GOVERNOR WITH (CLASSIFIER_FUNCTION = [dbo].[UDFAssign_RG_WorkLoadGroup]);
GO

ALTER RESOURCE GOVERNOR RECONFIGURE;
GO

Wednesday, 4 September 2013

SQL PowerShell - Add Windows User as SysADM

SQL PowerShell - Add Windows User as SysADM

OK, So I guess its finally time to start Teaching myself Powershell commands to enable me to administer SQL Server on Windows Core.

I will share my findings along the way.


The first thing I need to know how to do its add  a Windows Domain User as a SQL Server SysAdm.


So here goes.


I borrowed the following code off the Web somewhere, but could not get the AddToRole statement to work, originally it was placed after the Create() Statement, I found that by moving the Create() to the End of the Statement, everything worked as it should.



[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | out-null
$SqlServer = New-Object ('Microsoft.SqlServer.Management.Smo.Server') 'SERVERNAME\INSTANCENAME'
$SqlUser = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Login -ArgumentList $SqlServer, 'DOMAIN\USERNAME'
$SqlUser.LoginType = 'WindowsUser'
$SqlUser.AddToRole('sysadmin')
$SqlUser.Create()


OK, so two days in and I'm starting to get the hang of this now, I have realised that what I have done above (although it does work) is basically wrong.


We have to do this in stages : -

#LOAD SQL Server Objects
Load the SQL Server SMO Libraries to Powershell
#CONSTRUCTORS
Define the type of Object we want to work with, Database, Login etc
#PROPERTIES
Set the Values for the Properties, Login type for example
#CREATE
Run the Create() Method to Create the Object
#METHODS
Now the object has been created, we can run Methods to amend the Objects, such as AddRole.
#EVENTS
Check Event such as HasDataChanged

So the Add Windows User as SQL Sysadm, should look like this :-



#LOAD SQL Server Objects
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | out-null
#CONSTRUCTORS
$Server = New-Object ('Microsoft.SqlServer.Management.Smo.Server') 'ServerName\InstanceName,Port'
$Login = New-Object Microsoft.SqlServer.Management.Smo.Login($Server, "Domain\User")
# PROPERTIES
$Login.LoginType = 'WindowsUser'
#CREATE
$Login.Create()
#METHODS
$Login.AddToRole('sysadmin')
#EVENTS


If you refer to the Microsoft Documentation on Microsoft.SqlServer.Management.Smo :-


We can use the above as the basis to do pretty much anything we desire in SQL Server with PowerShell.

So I don't think I am there yet, there will be plenty more to learn on the way, however, I think that I have passed a major Milestone in understanding just how these SQL PowerShell commands are constructed.

Monday, 29 April 2013

Disk Partition Alignment For SQL Server 2008


Overview

Disk partition alignment is a powerful tool for improving SQL Server performance, a Best Practice that is essential, yet often overlooked.

Windows Server 2008 attempts to align new partitions out-of-the-box, yet disk partition alignment remains a relevant technology for partitions created on prior versions of Windows or on volumes that have not been subject to a Windows 2008 Format such as volumes in a SAN environment.

This document is an extract of the Microsoft White paper

It documents performance for aligned and nonaligned storage and why nonaligned partitions can negatively impact I/O performance; it explains disk partition alignment for storage configured on Windows Server 2003, including analysis, diagnosis, and remediation; and it describes how Windows Server 2008 attempts to remedy challenges related to partition alignment for new partitions yet does not correct the configuration of pre existing partitions.

I have included the most relevant portions in to this document to try an make it a concise guide, however please refer to the Microsoft Document should you require any further background or explanation as to why Disk Partition Alignment is required.

Disk Partition Alignment.

Introduction

Noncompliance with storage configuration Best Practices for the Microsoft SQL Server is a common root cause of support calls to Microsoft for poorly performing Database Systems.

The reason is often shown to be misalignment between Windows, Storage, Disk Controllers, and Cache segment lines.

Failure to perform partition alignment may result in significant performance degradation.

Disk partition alignment is a requirement for partitions from which high performance is demanded and that were not created on RAID disk devices on Windows Server 2008.

Unless performed at the time of partition creation, the default alignment offset will result in unaligned partitions.

Earlier versions of Windows and some Manufacturers’ default SAN Settings create disk partitions by default to boundaries based on the Cylinder/Head/Sector (CHS) addressing scheme used by previous generation disk controllers.

Pre existing partitions attached to Windows Server 2008 maintain the original, flawed alignment under which they were created.
  • MBR basic
  • MBR dynamic
  • GPT basic
  • GPT dynamic 

 Scope

The information contained in this document applies to Windows basic and dynamic disks with master boot record (MBR) partitions.

However, disk partition alignment is a Best Practice, and it is required for optimal performance for each of these hard drive configurations:

Description 

The following is a simplified characterization of partition misalignment:

Disk array hardware reports 63 reserved (hidden) sectors, and Windows obediently implements this information, reserving these sectors at the beginning of the first partition of a disk.

The master boot record (MBR) resides within these hidden sectors.

The compliance by Windows with 63 hidden sectors reported by disk hardware results in misalignment with stripe units, disk controllers, and cache segment lines.

In all versions of Windows earlier than and including Windows Server 2003, these reserved sectors do not coincide with fundamental physical boundaries.

The result is that single clusters of user data are written across multiple stripe units of the RAID.

Every nth operation is affected (n depends on file allocation unit (cluster) size and stripe unit size).

Fundamental physical boundaries in disk controllers and other hardware are also violated.

Across a striped array, a single I/O coming into the array controller turns into multiple I/Os if the request crosses one or more stripe unit boundaries.

The cumulative effect can contribute to substantial performance degradation. 

In all cases, similar principals are at work:

Due to misalignment, clusters of data are written across physical boundaries, requiring unintended, unnecessary I/Os that result in performance degradation.

Partition Alignment in Windows Operating Systems 

The way partition alignment works depends on the version of Windows being used and the version in which the partition alignment was created.

Windows Server 2008 - New Partitions.

 In Windows Server 2008, partition alignment is usually performed by default.

The default for disks larger than 4 GB is 1 MB.

The setting is configurable and is found in the registry at the following location:
HKLM\SYSTEM\CurrentControlSet\Services\VDS\Alignment

However, if OEM setups are delivered (for example, with recovery partitions), even fresh installations of Windows Server 2008 having partitions with undesirable partition starting offsets are possible.

Whatever the operating system, confirm that new partitions are properly aligned.


Windows Server 2008 – Pre existing Partitions. 

New partitions on Windows Server 2008 are likely to be aligned.

Yet partitions created on earlier versions of Windows and become associated with Windows Server 2008 maintain the properties under which they were created.

That is, in the absence of partition alignment being explicitly performed, these partitions are not aligned.

Windows Server 2003. 

Partitions created on Windows Server 2003 by default are not aligned.

Partition alignment must be explicitly performed.




System Drives.

System drives in versions of Windows prior to Windows Server 2008 cannot be aligned.

Fortunately, workloads associated with system partitions of dedicated SQL Server computers are typically not as sensitive to partition misalignment as disks dedicated to I/O intensive uses, for example, SQL Server database files from which high-performance is demanded.

System drives on fresh installations of Windows Server 2008 should be aligned by default.

Virtual Drives.

 Virtual drives and the host drives on which they reside must be aligned for optimal performance.  



Starting Partition Offsets. 

The performance benefit of disk partition alignment requires configuration of valid starting partition offsets.

Several tools report the starting partition offset.

The results are reliable only in specific contexts.

Valid starting partition offsets and tools used to report them are discussed in this section.

Valid Starting Partition Offsets. 

Because versions of Windows earlier than and including Windows Server 2003 comply with the 63 hidden sectors reported by disk hardware, and because the most common sector size 512-byte sectors, the default (and suboptimal) starting partition offset is 32,256 bytes, exactly 31.5 KB.

Explicitly defining the starting offset from 31.5 KB to exactly 32 KB might seem like a legitimate approach.

In fact, as mentioned earlier, 64 KB is typically the minimum (and a common) valid starting partition offset for SQL Server because of the correlations described later.

When choosing a valid partition starting offset, refer first to the storage vendor best practices.

Make certain their recommendations correlate with the stripe unit size and file allocation unit size configured for SQL Server.

In the absence of definitive vendor information, choose the Windows Server 2008 default.
Windows Server 2008 partition alignment defaults to 1024 KB (that is, 1,048,576 bytes). 


This value provides a durable solution.

It correlates well (as described later) with common stripe unit sizes such as 64 KB, 128 KB, and 256 KB as well as the less frequently used values of 512 KB and 1024 KB.

Also, the value virtually guarantees hidden structures allocated by storage.


Dynamic Disk Partition Offsets: dmddiag.exe –v.

The command-line utility Dmdiag.exe is used to determine the partition offsets of Windows dynamic volumes.

Important: Neither the output of the wmic command nor any other tool designed only for basic disks, reliably reports starting partition offsets of Windows dynamic disks.

The tool is available in the support tools folder of Windows Server 2003.

In Windows Server 2008, the tool has been renamed diskdiag.exe.

To determine the starting partition offset of dynamic disks, execute the following command.

dmdiag -v
The output has several sections; the sections that are relevant to analyzing the starting partition offset of existing volumes are generated only if the -v switch is used.

Those sections (and relevant columns) are:
Dynamic Disk Information (Rel Sec)
LDM Volume Information (Rel Sectors)

Focus on these sections and columns.

Subsequent sections of the output that report offsets can be misleading, and they are unlikely to be reliable for interpreting starting partition offsets of dynamic volumes.

Note that Microsoft tools, including even dmdiag, may be unreliable for reporting starting partition offsets of dynamic volumes created by third-party vendors.

For example, dmdiag does not report correct starting partition offsets of dynamic volumes created by Veritas Enterprise Administrator.

In these cases, consult your vendor for the tools and techniques required for proper analysis.

 Command-Line Partition Alignment Tools. 

Disk partition alignment is not available from the Disk Management snap-in (diskmgmt.msc).
Windows provides two tools to implement disk partition alignment: diskpart.exe and diskpar.exe.

Note the presence or absence of a “t” in their names.

Both utilities are powerful and should be exercised with caution.

Diskpar.exe reliably reports partition alignment in terms of bytes.

However, results are valid only for MBR basic disks, and this tool is no longer supported by Microsoft.

Diskpart.exe reports alignment for basic disks in terms of kilobytes.

As noted, the Windows Server 2003 (and earlier) default alignment is 32,256 bytes, exactly 31.5 KB; unfortunately DiskPart rounds this up to 32 KB.

Though DiskPart is the tool of choice to implement partition alignment, the value it reports for partition offset is not sufficiently granular.

Therefore, use the wmic command to report partition offsets of basic disks; use dmdiag –v for Windows dynamic disks.

Implementation 

This section provides information about using specific measurements and tools to implement the recommendations presented earlier in this white paper.

It also addresses issues to consider regarding vendors.

Essential Correlations: Partition Offset, File Allocation Unit Size, and Stripe Unit Size

Use the information in this section to confirm the integrity of disk partition alignment configuration for existing partitions and new implementations.

There are two correlations which when satisfied are a fundamental precondition for optimal disk I/O performance.

The results of the following calculations must result in an integer value:

Partition_Offset ÷ Stripe_Unit_Size

Stripe_Unit_Size ÷ File_Allocation_Unit_Size

Of the two, the first is by far the most important for optimal performance.

The following demonstrates a common misalignment scenario:

Given a starting partition offset for 32,256 bytes (31.5 KB) and stripe unit size of 65,536 bytes (64 KB), the result is 0.4921875.

This is not an integer; therefore the offset & strip unit size are not correlated.

This is consistent with misalignment.

However, a starting partition offset of 1,048,576 bytes (1 MB) and a stripe unit size of 65,536 bytes produces a result of exactly 8, an exact integer, which is consistent with alignment.

Note that file allocation unit (cluster) size commonly correlates with common stripe unit sizes.

The performance question here is usually not one of correlation per the formula, but whether the cluster size is the NTFS default of 4,096 bytes or has been explicitly defined at 64 KB, which is a best practice for SQL Server.

 Stripe Unit Size. 

Windows does not have a reliable way to determine stripe unit sizes.

These values are obtained from vendor disk management software or from your SAN administrator.

File Allocation Unit Size. 

Run this command for each drive to see the file allocation unit size reported in bytes per cluster.

fsutil fsinfo ntfsinfo c:
fsutil fsinfo ntfsinfo e:
etc...

An appropriate value for most installations should be 65,536 bytes (that is, 64 KB) for partitions on which SQL Server data or log files reside.

 Vendor Considerations. 

Consult the storage vendor for their recommendation.

In the absence of definitive information, consider implementing disk partition offset with the Windows Server 2008 default of 1024 KB. 




Conclusion. 

Many factors contribute to optimal disk I/O performance.

For disk partitions created with Windows Server 2003, partition alignment properly correlated with stripe unit size and file allocation unit size is a best practice, and it provides a fundamental foundation for optimal performance.

Windows Server 2008 aligns partitions by default.

When servers are upgraded to Windows Server 2008, preexisting partitions are not automatically aligned and must be rebuilt for optimal performance.

Thus, until existing misaligned partitions created using Windows Server 2003 are rebuilt properly, disk partition alignment will remain a relevant technology.

The results of the following calculations must result in an integer value:

Partition_Offset ÷ Stripe_Unit_Size

Stripe_Unit_Size ÷ File_Allocation_Unit_Size

Of the two, the first is by far the most important for optimal performance.

A starting partition offset of 1,048,576 bytes (1 MB) and a stripe unit size of 65,536 bytes produces a result of exactly 8, an exact integer, which is consistent with alignment.

Note that file allocation unit (cluster) size commonly correlates with common stripe unit sizes.

The performance question here is usually not one of correlation per the formula, but whether the cluster size is the NTFS default of 4,096 bytes or has been explicitly defined at 64 KB, which is a best practice for SQL Server 2008.
























Installing SCCM with a Clustered SQL Server


Overview.

Traditionally there is has never been a High Availability solution for SMS, the only option available was to Backup the Databases.

Thankfully System Center Configuration Manager 2007 (SCCM 2007) now has the ability to support a Clustered SQL Server Instance.

Unfortunately, the documentation for how to install SCCM 2007 with clustered SQL Server is poor and can lead to problems with the installation and application.

Requirements pre SCCM Installation. 

Ensure Kerberos is enabled on SQL Cluster.

Ensure Kerberos is enabled on each of the SQL Cluster Nodes.

Although Kerberos must be enabled on the cluster for normal SCCM operation, the SCCM pre-requisite checker apparently does not check for this.

As a result it allows the install to go ahead and ends up installing directly to the individual nodes instead of to the SQL cluster.

This is because SCCM can not see the cluster since Kerberos is not enabled.

When using a clustered SQL Server instance to host the site database, TCP/IP network protocol communication is required for each SQL Server cluster node network connection to support Kerberos authentication.

The named pipes communication protocol is not required, but can be used to troubleshoot Kerberos authentication issues.

Create Dummy Files on Each Disk.

Create a dummy file NO_SMS_ON_DRIVE.sms on all disks at the cluster, execpt %systemdriv%.

This restricts the SCCM installer to install components on the SQL Cluster Instance %systemdrive% only!

Ensure SCCM Machine Has Rights in SQL.

Ensure primary site server has admin rights on each of the SQL Cluster Nodes.

If the local admins group has been removed from the sysadmin SQL Server role (SQL Server best practice), then we need to add the site server computer account to that group as well as the local admins group.

Site component manager needs admin rights on each node to install the SCCM backup and recovery components on the SQL Server nodes.

Note. This must also be performed for every new node added to the SQL cluster in the future.

Register SQL Server Cluster Virtual Name in Active Directory.

By default, Windows does not register SQL Server Cluster Virtual Name in Active Directory.

To facilitate a successful installation of SCCM 2007 on Clustered SQL Server, it must be registered.

Run setspn.exe utility as follows:-

It Can be Downloaded from the Microsoft Download Center


Execute setspn MSSQLSvc/SQLSERVER1:1433 

Execute setspn MSSQLSvc/SQLSERVER1.mydomain.com:1433

Where :-

SQLSERVER1 is the Virtual Name
1433 is the SQL Port
SQLSERVER1.mydomain.com is Fully qualified domain name for SQL Server cluster.


Support for SQL Server 2008. 

SQL Database Engine. 

Microsoft added support for SQL Server 2008 with SCCM along with this update they also made changes to the admin console OSD driver management node (under Operating System Deployment -> Drivers).

This results in an error in the Drivers node when running SCCM 2007 R2 SP2 with SQL Server 2008.

To overcome this, there is a hotfix available :-


SQL Reporting Services. 

The Microsoft SCCM 2007 R2 SQL Reporting Services feature provides a set of tools and resources that help you use the advanced reporting capabilities of SQL Reporting services from the Configuration Manager console.

Prerequisites for SQL Reporting Services.

Before we can use SQL Reporting Services in Configuration Manager 2007, we must configure a reporting services point.

The reporting services point is a site system role that must be configured on a server running Microsoft SQL Server with the Reporting Services component installed.

For more information about configuring Reporting Services in SQL Server,

When you install a SQL Reporting Services Point, Configuration Manager 2007 queries Windows Management Instrumentation (WMI) for installed instances of SQL Reporting Services.

The report folder is installed on the first instance of SQL Reporting Services found by the WMI query.

If you have installed multiple instances of SQL Reporting Services and are unsure about whether the instance on which you want to install the report folder is the first stored in WMI, it is recommended that you install the Reporting Services Point on a computer running only one instance of SQL Reporting Services.


 To Create a Reporting Services Point.


In the Configuration Manager console, navigate to System CenterConfiguration Manager / Site Database / Site Management / <site code>-<site name> / Site Settings / Site Systems.

Determine whether to create a new site system or to add the reporting services point site role to an existing site system, and then follow the associated step.

To create a new site system and add the reporting services point role:

Right-click Site Systems, click New, and then click Server.

On the General page of the New Site Role Wizard, specify the name of the server that will be used to host the reporting services point.


To add the reporting services point role to an existing site system:

Right-click the site system name, then click New Roles.

On the General page of the New Site Role Wizard, configure the general settings for this site system, and then click Next. For information about the options you can specify on this page, see How to Add New Site System Roles. Click Next.

On the System Role Selection page of the wizard, select Reporting Services point, and then click Next.

On the Reporting Services Point page, specify the folder that will be created on the report server to contain the SQL Reporting Services reports used in Configuration Manager and then click Next.

noteNote
The default name for this folder is Reports.

Review the information shown on the Summary page, and then click Next.

Click Close to exit the wizard.


How to Install Configuration Manager Using a Clustered SQL Server Instance.


The Configuration Manager 2007 site database can be installed using a clustered virtual Microsoft SQL Server instance during setup by using the following procedures.

noteNote
During Configuration Manager 2007 Setup, the Volume Shadow Copy Service (VSS) writer will be installed on each physical computer node of the Microsoft Windows Server cluster to support the Backup Site Server maintenance task.

1.1      To install Configuration Manager using a clustered SQL Server instance to host the site database

  1. Create the virtual SQL Server cluster to host the site database on an existing Windows Server cluster environment.
  2. Add the machine account of the primary site server machine to the Local Administrators group of each Windows Server cluster node computer. This is required to allow the site server to install and configure settings later.
  3. In the virtual SQL Server instance, assign the sysadmin SQL Server role to the user account running Configuration Manager Setup.
  4. Start Configuration Manager Setup, and select to install Configuration Manager using custom settings.
  5. On the Configuration Manager Database Server page of the Configuration Manager Setup Wizard, enter the name of the clustered virtual SQL Server instance to host the site database and the name for the site database to be created on that instance by setup.
Important
During setup, you must enter the name of the virtual SQL Server cluster instance, and not the virtual Windows Server name created by the Windows Server cluster. Installing the site database using the Windows Server cluster virtual instance name will result in the site database being installed on the local hard drive of the active Windows Server cluster node, and it will prevent successful failover if that node fails.

 


1.2      To verify that the site database was installed successfully


  1. Verify that Configuration Manager 2007 Setup completed successfully by reviewing the ConfigMgrSetup.log file located at the root of the system drive on the primary site server computer.
  2. In the SQL Server  Management Studio console, verify that the site database was created successfully.
  3. In the SQL Server  Management Studio console, verify that the following roles were created for the site database:
    • smsdbrole_MP
    • smsdbrole_SLP
    • smsdbrole_RP
    • smsdbrole_PSP
    • smsdbrole_DMP
    • smsdbrole_siteprovider
    • smsdbrole_siteserver
  4. Verify that the following SQL Server database roles for the site database have been assigned with the appropriate machine accounts:
    • smsdbrole_MP (if management point installation was selected)
    • smsdbrole_siteprovider (the site server or remote provider that was specified)
    • smsdbrole_siteserver (the primary site server computer)





SQL SERVER 2008 Top 20 Enterprise only Features


Overview.

You can find many articles, white papers, and books demonstrating the myriad features found in SQL Server 2008.

However it is sometimes easy to forget that there’s a big disparity in the features that the different editions of SQL Server 2008 support.

Chances are that the exciting new features you’re reading about are supported only by
SQL Server 2008 Enterprise Edition or SQL Server 2008 Developer Edition.

(Although Enterprise Edition and Developer Edition share the same feature set, Developer Edition is licensed solely for development work and can’t be used for production work.)

The following is a list of the 20 most important features.


1       Hot-add CPU. 

Recognizes newly added CPUs without a restart.

2       Hot-add RAM 

Recognizes additional RAM without a restart.

3       More instances. 

Up to 50 named instances (other editions support only 16).

4       Data Compression. 

Automatically compresses database data.

5       Transparent Database Encryption. 

Encrypts databases without making application changes.

6       Resource Governor. 

Allocates system resources per workload.

7       Partitioning.

Divides large tables and indexes into multiple file groups for better performance.

8       Partition Table Parallelism.

Uses separate threads for queries over multiple partitions.

9       Asynchronous Mirroring Mode.

SQL Server 2008 Standard Edition supports only Synchronous database mirroring.

10    More Fail-over Clustering Nodes.

 Up to 16 nodes (Standard Edition supports two nodes)

11    Database Snapshots.

For capturing point-in time database copies.

12    Fast Recovery.

System availability at the end of the transaction-log roll-forward phase.

13    On-line Indexing.

Rebuilds indexes while the base table is in use.

14    On-line Restore.

Restores file groups while a database is active.

15    Distributed Partitioned Views.

Creates scale-out clusters by dividing tables between multiple SQL Server systems.

16    Filtered Indexes.

Lets you selectively index column values.

17    Oracle Replication Publishing.

Lets Oracle act as replication publisher.

18    Peer-to Peer (P2P) Transactional Replication.

Replicates data changes to all nodes on the network.

19    Advanced Transformations.

Adds SQL Server Integration Services transformations such as Fuzzy Lookup and Data Mining.

20    Change Data Capture.

Ability to track changes on a table and capture them to a mirrored table.