Pages

Monday, 9 June 2014

SQL Server: Hierarchical Data Set

The hierarchyid data type is a special variable-length, CLR-supplied data type that can be used to represent hierarchical data. When DBA define a hierarchyid data type column, DBA can use various system-supplied methods, such as GetRoot, GetDescendant, GetLevel, Read, and Write, which allow DBA to perform tasks on the hierarchy. Each value stored in the table is an internal binary value that represents the row's position in the hierarchy. In this scenario, DBA could use the GetLevel method to easily return an employee's level in the organization chart. Then, DBA could use statements similar to the following to perform tasks in the hierarchy:

-- Insert an employee into the table at the root node of the hierarchy

-- GetRoot returns the appropriate hierarchyid to store in the HierID column

INSERT INTO
abc.Employee (EmpID, HierID, LastName, FirstName, DeptID, HireDate, Salary)
VALUES(1, hierarchyid::GetRoot(), 'Fayyaz', 'Ahmed', 45,
GETDATE(), 97000.00);
GO

-- Insert an employee that is a subordinate to the root

-- GetRoot returns the hierarchyid of the root

-- GetDescendant returns the hierarchyid of the new employee's manager

DECLARE @parent hierarchyid
SELECT @parent = hierarchyid::GetRoot()
INSERT INTO abc.Employee (EmpID, HierID, LastName, FirstName, DeptID, HireDate,
Salary)
VALUES (2, @parent.GetDescendant(NULL, NULL), 'Sheikh', 'Ahmed', 45, GETDATE(), 52000.00)
GO

 -- Insert an employee that is a subordinate to EmpID 2

DECLARE @parent hierarchyid, @child hierarchyid
SELECT @parent = HierID FROM Employee WHERE EmpID = 2
INSERT INTO abc.Employee (EmpID, HierID, LastName, FirstName, DeptID, HireDate,
Salary)
VALUES (3, @parent.GetDescendant(NULL, NULL), 'Saad', 'Ahmed', 45,
GETDATE (), 37000.00)
GO

-- Query all employees including a string that represents the path in the hierarchy

SELECT HierID.ToString() AS 'HierString', * FROM abc.Employee
GO

The ToString method in the final SELECT statement converts the employee's hierarchical value to a string. The root employee will have a HierString value of '/'. The employee with an EmpID value of 3 has a HierString value of '/1/1/'. DBA should not implement separate tables for the employees and the employees' managers with a FOREIGN KEY constraint that relates the two tables, or implement a single Employee table that includes a FOREIGN KEY constraint on the MgrID column that references the EmpID column. In this scenario, using a hierarchyid data type would provide the desired functionality and be less complex to implement. If DBA implemented separate tables for the employees and the employees' managers, DBA would have to combine datasets each time DBA wanted to query all employees. If DBA implemented a single Employee table that included a FOREIGN KEY constraint on the MgrID column that references the EmpID column, it would require a single join to return an employee's manager, but determining the employee's level in the organization chart would be extremely complex to code.DBA should not implement a single Employee table that represents each employee using an xml data type. An xml data type is used to store XMLdata, such as XML documents or XML document fragments.

Sunday, 8 June 2014

SQL Server: XML Parser Optimization

DBA should create a stored procedure that accepts an xml input parameter and uses XML methods. The stored procedure would select the desired elements and attributes using the value() method in the SELECT list with the nodes() method specified in the FROM clause.
For example, for XML Parser Optimization DBA might use the following stored procedure to insert rows into the table:

CREATE PROCEDURE usp_InsertABLines (@xml xml)
AS
BEGIN
INSERT INTO ABDetails (InvID, Quantity, Cost, TaxFlag)
SELECT t.c.value('@InvID', 'int'), t.c.value('@Quantity', 'int'),
t.c.value('@Cost', 'money'), t.c.value('@Taxable', 'bit')
AS TaxFlag FROM @xml.nodes('/ABLines/ABLine')
AS t(c)
END

The nodes() method accepts an XQuery string and returns the specified nodes as a result set. In this scenario, the nodes() method returns a result set that contains one row for each purchase order line. Then, in the SELECT list, the table alias defined is used with the value() method to return the details for each line. The value() method accepts two arguments. The first argument is a string XQuery expression, and the second argument is a string containing a SQL Server data type.
The value() method extracts a value using the XPath expression and returns it as the specified type. DBA could use the value() method to extract the desired data as a specified data type to be inserted into the ABDetails table. DBA should not use a SELECT statement that uses the query() XML method in the SELECT list. The query() method is used to query and retrieve XML elements and attributes from an XML instance as untyped XML. The method accepts a string XQuery expression that determines which elements and element attributes are extracted. In this scenario, DBA needed to return the data from the XML with specific data types, not in XML format.DBA should not create a user-defined function that accepts an xml input parameter and returns a table data type. DBA would still need to insert the value returned by the function into a table. DBA should not create a stored procedure that accepts an xml input parameter and uses OPENXML. Although DBA can use OPENXML to shred XML into relational data, OPENXML is more memory-intensive, and in this scenario, DBA wanted to minimize memory consumption. OPENXML is a rowset provider function that creates a relational view of data contained in an XML document. OPENXML can be used in place of a table or view in a SELECT statement. To use OPENXML, DBA first call the sp_xml_preparedocument system stored procedure to return a document handle. Then, DBA include a query that contains the OPENXML function in the FROM clause and include a WITH clause to identify the columns in the output. After the data has been extracted from the XML document, DBA call the sp_xml_removedocument stored procedure to remove the internal document handle and free resources.

Saturday, 7 June 2014

SQL Server: Failover Capability and Read-Only Reporting Access

The MERGE statement allows DBA to combine the inserts, deletes, and updates, and to use a single statement to perform multiple DML actions. Using a MERGE statement instead of issuing multiple DML statements can improve performance. In a MERGE statement, DBA must specify a source and a target and include a join. Then, DBA uses the MATCHED clauses to specify the actions to be performed. The basic syntax of the MERGE statement is as follows:

MERGE [INTO] target_table USING source_table
ON join_condition
[WHEN MATCHED THEN
matched_action]
[WHEN NOT MATCHED [BY TARGET] THEN
notmatched_action]
[WHEN NOT MATCHED BY SOURCE THEN
notmatchedsource_action];

The two WHEN NOT MATCHED THEN clauses specify the actions to take if the records from the source table are not in the target table, or vice versa. The WHEN MATCHED THEN clause specifies the action to take if the records from the source table are in the target table. When synchronizing tables, DBA can use BY TARGET or BY SOURCE to further control how the synchronization occurs when there are differences in the source data and the target data. In this scenario, you could use the following MERGE statement:

MERGE ARTrxMaster AS t
USING ARTrx AS s
ON (t.TrxID=s.TrxID) WHEN MATCHED THEN UPDATE SET
t.TrxType = s.TrxType, t.Quantity = s.Quantity, t.UnitPrice = s.UnitPrice, t.ExtAmt =
s.ExtAmt, t.TaxAmt = s.TaxAmt, t.LoadDate = GETDATE()

WHEN NOT MATCHED BY TARGET AND s.TrxType = 'TX1' THEN
INSERT(TrxID, TrxType, Quantity, UnitPrice, ExtAmt, TaxAmt, LoadDate)
VALUES (s.TrxID, s.TrxType, s.Quantity, s.UnitPrice, s.ExtAmt, s.TaxAmt, GETDATE())

WHEN NOT MATCHED BY SOURCE AND t.TrxType = 'TX1' THEN DELETE
OUTPUT $action, INSERTED.*, DELETED.*;

With this statement, the following results would occur under these conditions:

The WHEN MATCHED clause would execute and update the target table (ARTrxMaster) if a row existed in both tables.

The UPDATE statement does not include a table name because the table to be updated is implicit as the target table in the merge.

The WHEN NOT MATCHED BY TARGET clause would insert rows into the target table (ARTrxMaster) if the row does not exist in the source table and the additional condition specified in the WHEN clause is met. Only rows in the ARTrx table that have a TrxType of 'TX1' would be inserted into ARTrxMaster.

The WHEN NOT MATCHED BY SOURCE clause would delete rows from the target table (ARTrxMaster) which do not exist in the source table (ARTrx) if the additional condition specified in the WHEN clause is met. Only rows in ARTrxMaster with a TrxType of 'TX1' would be deleted.

This statement also includes an OUTPUT clause. The OUTPUT clause allows you to retrieve and display information about the rows affected by the MERGE statement. The OUTPUT clause can display this information to the user, insert the data into another permanent or temporary table or table variable using an INTO clause, or pass the data to a nested DML statement for processing. Within the OUTPUT clause, DBA specifies the column values that should be retrieved by using the column names with the INSERTED and DELETED prefixes. The DELETED prefix returns the column value before the DML operation, and the INSERTED prefix returns the column value after the DML operation but before executing any triggers. DBA can also use $action to return a string indicating which type of DML operation affected the row. In this statement, DBA specified action, INSERTED.*, DELETED.*. This statement would return a result set of the rows affected by the MERGE, action.

Tuesday, 3 June 2014

SQL Server Parallelism - MAXDOP and Resource Governor Option

MAXDOP only controls the number of parallel threads PER OPERATOR during query execution. Since queries can have multiple operators, it’s entirely possible for SQL Server to throw multiple threads at a single operation to handle different operators.

Resource Governor enables to manage SQL Server workloads and resources by specifying limits on resource consumption by incoming requests.

For example if a server has four processors and there is a need to use only three for parallelism, then DBA should set the max degree of parallelism option to 3. When DBA want to use a specific number of processors for an individual query or index operation, she can use the MAXDOP query hint or index option to provide better control. MAXDOP overrides the settings defined in the max degree of parallelism option. The use of MAXDOP is limited to theexecution of a query that contains the MAXDOP query hint or index option.

Following are the sequence of steps that can be used for MAXDOP query hint or index option to provide better control:

1- Create Resource Pool

CREATE RESOURCE POOL [indexMaintenance] WITH(min_cpu_percent=0,
             max_cpu_percent=100,
             min_memory_percent=0,
             max_memory_percent=100,
             AFFINITY SCHEDULER = AUTO
)
GO

2- Create a Workload Group

CREATE WORKLOAD GROUP [wg_indexMaintenance] WITH(group_max_requests=0,
        importance=Medium,
        request_max_cpu_time_sec=0,
        request_max_memory_grant_percent=100,
        request_memory_grant_timeout_sec=0,
        max_dop=8) USING [indexMaintenance]
GO

3- Create the user that will be associate with workload group and give them permissions:

USE [master]
GO
CREATE LOGIN [indexMaintenance_user] WITH PASSWORD=N'ChangeThis',
DEFAULT_DATABASE=[master], CHECK_EXPIRATION=OFF, CHECK_POLICY=OFF
GO
ALTER SERVER ROLE [sysadmin] ADD MEMBER [indexMaintenance_user]
GO

4- create a classifier function, which is the function SQL Server will run against new logins in order to determine their proper workload group:

CREATE FUNCTION fnRGClassifier()
RETURNS sysname
WITH SCHEMABINDING
AS
BEGIN
       DECLARE @group sysname
     IF(SUSER_NAME() = 'indexMaintenance_user')
     BEGIN
          SET @group =  N'wg_indexMaintenance';
     END
ELSE
        BEGIN
             SET @group =  N'default';
       END
       RETURN @group;
END;
GO

5- Set Resource Governor to use this function:

ALTER RESOURCE GOVERNOR with (CLASSIFIER_FUNCTION = dbo.fnRGClassifier);
ALTER RESOURCE GOVERNOR RECONFIGURE;
GO

6- Index maintenance processes run as indexMaintenance_user

EXECUTE AS LOGIN = 'indexMaintenance_user';

Monday, 2 June 2014

RAID Levels of MS SQL Server 2008

To meet varying requirements for response time, data reliability, and performance, typically different RAID levels are used depending on how to design the SQL Server deployment.

These RAID levels are:

RAID 5: Known as Striping with Parity. The parity information is written across all disks in the array. Minimum of three disks to form a RAID 5 array are required. This RAID level offers better read/write performance as long as all disks in the RAID 5 are present. If one disk is missing, the read performance is degraded. RAID 5 stripes data across all disks in the array.
RAID 5 volume, needs to be formatted to ensure that it is usable for storing data.  By default, the allocation unit for a logical unit number (LUN) is 4096 bytes (4 KB) on an NTFS partition. When storing data with large files. For RAID 5 volume, change the allocation unit to 64 KB, which is 65536 bytes. .

RAID 0: Known as Disk Striping. This RAID level stripes the data across disks in the array, offering better throughput on the read/ write operations. However, there is no data protection offered in this RAID level. If one disk fails, the data stored on the disk will be lost.

RAID 1: Known as Disk Mirroring. Minimum of two disks to form a RAID 1 array are required. One primary disk is used for read/write operations, and the data is replicated to the second disk. This RAID level offers better read performance and fault tolerance, but slower write performance than RAID 0.

The scenario states that the database must be functional even if a drive fails in the system. To meet this goal, the operating system files must be on RAID 1. Using RAID 1, Mirror the operating system so that even if a disk fails, the operating system will function. The transaction logs should be placed on a separate

RAID 1 array. SQL Server writes data to the transaction logs and maintains serial information of all the modifications that occurred in a SQL database. The transaction log files can be used for rollback and roll forward operations from a SQL Server database. To enhance the performance of SQL databases, Place the transaction log files on a dedicated RAID 1.

Finally, the SQL Server files and filegroups should be placed on a RAID 5 array to achieve the best performance throughput.

Wednesday, 28 May 2014

DBA Tools

SQL SERVER Activity Monitor Tool:


To analyze the wait time for running a query and to identify whether the query is blocked; SQL Server activity monitor tool is used.
An operational server contains a default SQL Server 2005 instance. DBA needs to install a SQL Server 2008 instance for a new application on an existing operational server. She also needs to ensure that certified third-party applications have access to their respective database instances.
The client requires: 
1- The existing application environments will not be changed
2- Administrative efforts are minimal
Best solution for above scenario is to install SQL Server 2008 as a named instance and configure the new application to use the new instance.
Further Reading: