Sunteți pe pagina 1din 34

Notes SQL PRO 70-451

Designing a Database Strategy


Identifying which SQL Server Components to Use to Support Business Requirements
 Linked Servers o Advantages:  Remote Server Access  The ability to issue distributed queries, updates, commands, and transactions on heterogeneous data sources across the enterprise.  The ability to address diverse data sources similarly. o Components of a linked server definition:  OLE DB Provider a DLL that manages and interacts with a specific data source. y OLE DB providers exist for a variety of files and file formats including databases, text files, spreadsheet data, and the results of full-text content searches.  OLD DB Data Source

Manage Linked Server Definitions  Sp_adlinkedserver / sp_dropserver Can also be done in SSMS

Sys.servers System catalog views used to see servers defined in a specific instance of SQL Server.  Ad Hoc Names used for infrequent queries against OLE DB data sources not defined as linked servers. By default ad hoc names are not supported. The DisallowAdhocAccess provider option must be set to 0 and the AD Hoc Distributed Queries advanced configuration option must be enabled. o OPENROWSET and OPENDATASOURCE are referred to as functions, but are truly macros that do not support T-SQL statements as arguments. o OPENROWSET can be used with any OLE DB provider that returns a rowset, and can be used anywhere a table or view reference is used in a T-SQL statement. o OPENDATASOURCE provides connection information as part of a four-part object name. This function supports only OLE DB providers that expose multiple rowsets by using the catalog.schema.object notation.

Design a Database Model


 Normalization Terminology o Candidate Key Minimal Super Key in that the set of attributes is a Super Key with no proper subset that could be a Super Key. o Super Key A set of attributes of a relation variable where no two distinct tuples (rows) that have the same values for the attributes in this set. o Prime Attribute An attribute that appears in any Candidate Key. o Functional Dependency Y has a Functional Dependency on a set of attributes X when each X value is associated with only one Y value ( Written X -> Y ).  Normalized Form each increasing level or normalcy must meet the criteria of lower levels. o 1NF: The table is a faithful representation of relation and is free of repeating groups.  Ordering (top bottom or left right) is not important  No duplicate rows or repeating groups  Every cell contains only one value  All columns are regular (no hidden timestamps, row ids, or object ids) o 2NF: All of a tables non-prime attributes are Functionally Dependent on the whole of every candidate key.  Create separate tables for sets of values that apply to multiple records.  Relate these tables with a Foreign Key. o 3NF: Every non-prime attribute is directly dependent of every candidate key.  Eliminate fields that do not depend on the key

Design a Data Model by using the Entity Framework


 Entity Framework Overview o Entity Framework is a set of technologies in ADO.NET that support the development of data-oriented software applications. o Developers can work at a higher level of abstraction when they deal with data and can create and maintain data-orientated applications with less code than in traditional applications.

 Entity Data Model ( EDM )- abstracts the conceptual model from the physical database schema. An EDM model contains a conceptual model, a logical storage model, and mappings between the two models, which are each stored in special XML files. o The EDM model consists entities and relationships  Entity can be exposed as objects, have properties and be instantiated in application code.  Relationships relate entities in the connectional model and support inheritance between entities. o The Entity Framework uses the special XML files representing the model to translate the operations performed on the data model to operations against the respective tables in the underlying data source.  Entity SQL Similar to SQL, but with different syntax. Developers can write data access code against the conceptual model with it.  Entity Data Model Inheritance o Table-per-hierarchy inheritance model Can create an EDM that supports inheritance within a single database table. A Base Entity represents all elements within the table; other entities inherit different properties from the Base Entity.  Steps: 1. Create a base entity (e.g. Products) 2. Create additional entities that inherit from it (e.g. Virtual Products and Physical Products) o Table-per-Type Inheritance Supports inheritance with multiple tables. A Base Entity contains all of the elements for a group of entities, where properties of each Entity are inherited from tables with a one-one relationship with the primary table.

Design an Application Strategy to Support Security


 Schema Analogous to separate Namespaces or containers used to store database objects. Can be owned by any user, and ownership is transferable o Objects can be moved between schemas. o A schema can be owned by any database principal, including roles.  Schema Owner When created, schemas are owned by a Principal. A Principal is any entity or object that has access to SQL Server resources o Permissions:  Grant permissions on any object in the schema.  Can drop the Schema if it is empty.  Can drop any objects within the schema.  By default the schema owner cannot create any create any objects.  Database-Level Roles Similar to Groups in the MS Windows Operating System. o Database Wide in permission scope. o Fixed and Flexible database roles.  Fixed Database Roles Predefined roles existing in each database. The db_owner and db_securityadmin roles can manage fixed database role membership. Only the db_owner can add members to the db_owner fixed database role.

 

 Flexible Database Roles are user created and can be modified. Application Roles Users can only access the database through a custom application. User Authentication is handled by the application. The role has its own user-like permissions. o No Members - Application Roles do not contain users, only a password. o Sp_setapprole Stored procedure to enable Application Roles. o Database Level Principal Cannot see other databases or server level meta-data by default. o Application Role connection steps:  User executes a client application  The client application connects to an instance of SQL Server as the user.  The application executes the sp_setapprole stored procedure with a password known only to the application.  The Application Role is authenticated by the database.  The connection switches to the permission of the Application Role DB Ownership o Only accounts in the db_owner role can create objects owned by dbo. o When a user is owner of one or more objects, the user cannot be deleted until either the objects are deleted or ownership is transferred. Ownership Chaining Referenced objects permissions are not checked when the calling object has the same owners as the object being called. Security Levels

Design a Full-Text Search Strategy

Thesaurus Files XML files that can be modified to contain synonyms of pattern replacement, initially the default file contains the basic structure and a commented out sample thesaurus. o Sys.sp_fulltext_load_thesaurus_file

Must be used after modifying a thesaurus file before the changes are effective. Defining Mappings (Synonyms) Specific for each language file. Include all synonyms.

   

Diacritics Setting Each thesaurus is either sensitive or insensitive to diacritical marks, such as tilde (~), acute accent mark (), etc. Expansion Set Contains a group of synonyms such as Writer and Journalist. Queries that match any synonym are expanded to include all synonyms. Replacement Set Contains a text pattern to be replaced by a substitute set.

Steps to Find Records with Similar Meanings.

1) Modify the Thesarus File to include all words with the same meaning. 2) Reload the Thesaurus file using the sys.sp_fulltext_load_thesaurus_file system stored procedure. 3) Run a full text search using either FREETEXT or CONTAINS using FORMSOF.

Designing Database Tables


Identify the Appropriate Usage of New Data Types
 Geography Data Type Used to represent data as GPS Latitude and Longitude coordinates. o Represents data in a round-earth coordinate system.  Storing Date and Time Information o Datetime2 Data Type Similar to Datetime data type, but has greater precision.  Includes fractional seconds up to seven digits of precision. o Datetime 3 digits to represent fractional seconds. o SmallDateTime Stores both date and time without any fractional seconds. o Datetimeoffset Includes Time Zone information as a time zone offset.  Reflects the difference between Coordinated Universal Time and Local Time.  FILESTREAM implemented to store large binary objects such as image or video files. o Files are stored on the file system and can be managed through T-SQL. o Can be accessed through WIN32 APIs. o Tightly integrated with most database functionality including Backup and Recovery.

Design Tables
 SPARSE Columns Useful to optimize the storage of NULL values, at the cost of more overhead to retrieve non-null values. o The columns must be nullable and cannot have either the IDENTITY property or FILESTREAM attribute. o Syntax for using the SPARSE Keyword: ColumnName DataType SPARSE NULL  Char and Varchar Both allow up to 8,000 characters. o Char is fixed length, whereas Varchar can vary.  MAX Specifier Allows storage of values up to 2 GB. o Previously text and ntext were used for lengths of this magnitude, however text should be avoided as it cannot be indexed. o Data Types using the max specifier  Varchar(max)  Nvarchar(max)  Varbinary(max)  Numeric Data Types o Bigint Range 9,223,372,036,854,775,808, Storage = 8 Bytes o Int Range +/- 2 billion, Storage = 4 Bytes o Smallint Range +/- 37.2 K, Storage = 2 Bytes o Tinyint Range 0 255, Storage = 1 Byte  Decimal and Numeric data types Numeric is functionally equivalent to Decimal. o Syntax: decimal[ (p[ , s] )] and numeric[ (p[ , s] )] o (P) Precision, the maximum number of digits that can be stored on either side of the decimal point.  Max Precision is 38 and consumes 17 Bytes o (S) Scale, the maximum number of digits that can be stored on the right side of the decimal point.

 Sp_tableoption Sets option values for tables containing large user-defined type columns, such as varbinary(max), nvarchar(max), varchar(max), image, text, etc. o Large Value Types Out of Row option when turned on, large value data types will be stored outside of the row, but still in the database.  FILESTREAM attribute for column values to be stored in the file system rather than the database.  Non-deterministic Views and Computed Columns Cannot be indexed, and PERSISTED cannot be used.  PERSISTED Keyword with Computed Columns the value is recalculated every time a column referenced in the calculation changes.  Sp_tableoption Sets optional values for user defined tables Syntax Sp_tableoption [@TableNamePattern = ] table , [@OptionName = ] option_name , [ @OptionValue = ] value

Value
table lock on bulk load

Description
When disabled (the default), it causes the bulk load process on user-defined tables to obtain row locks. When enabled, it causes the bulk load processes on user-defined tables to obtain a bulk update lock.

insert row lock

Not supported in SQL Server 2008. The locking strategy of SQL Server is row locking with possible promotion to page or table locking. This option has no effect on the locking behavior of SQL Server and is included only for compatibility of existing scripts and procedures.

text in row

When OFF or 0 (disabled, the default), it does not change current behavior, and there is no BLOB in row. When specified and @OptionValue is ON (enabled) or an integer value from 24 through 7000, new text, ntext, or image strings are stored directly in the data row. All existing BLOB (binary large object: text, ntext, or image data) will be changed to text in row format when the BLOB value is updated. For more information, see Remarks.

large value

1 = varchar(max), nvarchar(max), varbinary(max), xml and large user-defined type (UDT)

types out of row columns in the table are stored out of row, with a 16-byte pointer to the root. 0 = varchar(max), nvarchar(max), varbinary(max), xml and large UDT values are stored

directly in the data row, up to a limit of 8000 bytes and as long as the value can fit in the record. If the value does not fit in the record, a pointer is stored in-row and the rest is stored out of row in the LOB storage space. 0 is the default value.

vardecimal storage format

When TRUE, ON, or 1, the designated table is enabled for vardecimal storage format. When FALSE, OFF, or 0, the table is not enabled for vardecimal storage format. Vardecimal storage format can be enabled only when the database has been enabled for vardecimal storage format by using sp_db_vardecimal_storage_format. For information about vardecimal storage format, see Storing Decimal Data As Variable Length. This option requires SQL Server 2005 Service Pack 2. Vardecimal storage format is available only in SQL Server Enterprise, Developer, and Evaluation editions. In SQL Server 2008 and later, all user databases are enabled for the vardecimal storage format. In SQL Server 2008 and later, vardecimal storage format is deprecated. Use ROW compression instead. For more information, see Creating Compressed Tables and Indexes. 0 is the default value.

Design Views
CREATE VIEW [schema name].ViewName [ (Column1 [,.n)] [WITH <View_Attribute> [,.n] AS SELECT Statement [WITH CHECK OPTION]  Partitioned Views Implemented using the UNION ALL operator. o Query Optimizer can use CHECK constraints on the individual tables to optimize SELECT statements on the partitioned view. o The preferred method for using partitioned data local to one server is with partitioned tables. However, Partitioned Views can access data spread across multiple Federated Database Servers.  View Attributes o ENCRYPTION Used to encrypt the CREATE VIEW statement that is stored in the sys.syscomments table. o SCHEMABINDING Prevents the base table(s) from being modified in a way that would affect the view definition.  The SELECT statement must use the two part name when this option is specified (e.g. Schema.Object) o VIEW_METADATA Various APIs will be able to access metadata about the view instead of the base table or views, when browse mode metadata is being requested for a query that references the view.

Design Data Integrity


 Disable and enable a CHECK constraint o ALTER TABLE table_name NOCHECK CONSTRAINT constraint__name o ALTER TABLE table_name CHECK CONSTRAINT constraint__name  CHECK Constraints Ensures that no data modifications can be made through the view that would cause the underlying data to violate the views definition. o Can be created with any logical (Boolean) expression. o Limitations  Cannot be created on computed columns unless they are created using the PERSISTED keyword.  NULL values are not rejected because they evaluate to UNKNOWN.  CHECK CONSTRAINST s are not validated during DELETE statements.  INSTEAD OF INSERT Trigger fired instead of the insert operation. o Can be used to capture errors when an INSERT operation fails. o CREATE TRIGGER trigger_name on table_or_view_name INSTEAD OF INSERT AS BEGIN INSERT INTO table_or_view_name SELECT col1, col2, FROM inserted  Hierarchyid Data Type o CLR Supplied Data Type o Methods (select examples)  GetRoot used to determine the root node in a hierarchy tree.  GetDecendant used to get the descendant of a parent node
y parent.GetDescendant ( child1 , child2 )

if child1 is not null, returns a child of parent greater than child1.

Design Programming Objects


Evaluate Special Programming Constructs
 sp_executesql System stored procedure that can be used to create Parameterized Stored Procedures that assemble dynamic SQL more efficiently and safely. o Should be used instead of EXECUTE with dynamic SQL  More likely to use cached execution plans.  Minimize the likelihood of a SQL Injection attack. Sp_executesql Example Syntax

DECLARE @IntVariable int, @SQLString nvarchar(500) DECLARE @ParmDefinition nvarchar(500) SET @SQLString = NSELECT Col1, Col2, n FROM Dbname.SchemaName.Table WHERE Col1 = @Var SET @ParmDefinition = N@Var int SET @IntVariable = 123 EXECUTE sp_executesql @SQLString, @ParmDefinition, @Var = @IntVariable

 SQL Injection Attack Malicious code is inserted into strings that are later passed to an instance of SQL Server for processing and execution. o How: Prematurely terminates one text string & Appending another, then terminating with a comment mark - - o String concatenation is the primary point of entry for script injection.  Ownership Chain Created when a database object, such as a stored procedure, accesses another database object, such as an underlying table. o For example, if an underlying table is owned by the creator of a stored procedure, someone with permission to execute the stored procedure does not require permission to access the underlying table in order see the result of the Stored Procedure. o Ownership chaining does not occur when the Stored Procedure contains DDL.  EXECUTE AS Clause Specifies the security context under which a stored procedure should execute. o SELF Eauilivant to the EXECUTE AS User_Name where User_Name is the person creating or modifying the stored procedure. o OWNER The owner of the module, if the module does not have a specified owner, the owner of the schema model is used. o CALLER (Default Option) The user must have access not only to the module itself, but also any underlying modules that are accessed. o User_Name The statements in the module execute under the context of the specified user.  Must be a singleton account (e.g. cannot be a group, role, certificate key, or built in account such as NTAUTHORITY\Local System Cannot be specified for DDL triggers with server scope or logon triggers. Use login_name instead.  Application Roles Used to authenticate applications, and can be setup with either authentication mode. These are database-level connections. To access another database, the Guest profile would have to be enabled. 

Sp_setuprole Command to establish an application role connection, requires a password.  Data Reader (ADO.NET) broad category of objects used to sequentially read data from a data source. o There is no DataReader class in ADO.NET but, there are a number of classes that implement the IDataReader interface. o Classes employing IDataReader (Selection)  System.Data.SqlClient.SqlDataReader  System.OLEDB.OLEDBDataReader  Oracle.OracleClient.OracleDataReader o

Design T-SQL Table-valued and Scalar Functions


Scalar Function Syntax CREATE FUNCTION fn_name ([@Variable data_type] [=default] [READONLY] [, n]) RETURNS data_type [WITH function_option [, n]] AS The RETURN statement must be last. BEGIN Function_body RETURN scalar_expression END;

 Function Properties and Limitations o Functions can contain multiple or no parameters. o Cannot use IFELSE construct with the RETURN clause It causes an error, that states that the last statement of a function must be a RETURN clause, even when the last statement is a RETURN clause  Inline Table-Valued Functions Returns a result set as a table data type, like the multistatement table-valued function. o Like creating a parameterized view, with parameters specified in the select statement. o Does not have to specify the structure of the table being returned.

Design T-SQL Stored Procedures


 Table Valued Parameters Used to pass multiple rows of data to a stored procedure or function. o Improves performance by reducing the number of round-trips to the server. o Requires a User Defined Data Type (UDT)  CREATE TYPE .. AS TABLE

Design CLR Table-Valued and Scalar Functions


 CLR Integration enables the use of Functions created in a .NET Framework language to be used directly from SQL Server. After the function is created in .NET with the appropriate attributes to access it from SQL Server, the following actions must be performed: o Register the Assembly using the CREATE ASSEMBLY statement. o Create a function using the CREATE FUNCTION that can be used to reference the appropriate .NET UDF.

 CLR Table Valued Function Syntax CLR Table Valued Function Syntax Example CREATE FUNCTION Example (@parm data_type ) RETURNS TABLE (col1 datatype, n) AS EXTERNAL NAME mydotnetassembly.mynamespace.myclassname  CLR Function results are Streaming When a CLR Table valued function is used the results are streamed from the CLR table, therefore, using the ORDER clause in the Function definition rather than in SPs consuming the results is more efficient.

Design CLR Stored Procedures, Aggregates, and Types


 Assembly Permission Sets: o Safe Database Only o External Access External resources (File System, Registry, Environmental Variables, Unmanaged Code) o Unsafe Full Access  User Defined Aggregates Custom aggregate functions that users can call like built in aggregates. o Implementation is defined in a class on an assembly in the .NET Framework. User Defined Aggregate Syntax CREATE AGGREGATE aggregate_name (@Parm_name [,n] RETURNS <return_sql_type> EXTERNAL NAE assembly_name

 CLR User Defined Types Especially beneficial for situations where the data type is employed for a global variable with a definition limiting the domain. o The CREATE ASSEMBLY and CREATE TYPE statements are used to create a CLR UDT. o Specific and limited to the database where it is created in. The UDT must be recreated in every database in which it is employed.

Designing a Transaction and Concurrency Strategy


Design for Concurrency
 Isolation Levels in the Database Engine Define the degree to which one transaction must be isolated from data modifications made by other transactions. o Described in terms of read phenomenon.

I/O and Isolation The isolation level only determines the level of protection for read operations.  Transaction Isolation Levels do not affect the locks acquired to protect data modifications. A Transaction always gets an exclusive lock on any data it modifies. Read Phenomenon ANSI/ISO standards for the three types of Concurrency Side Effects. o Dirty Reads Transaction can read data from another running transaction that has not committed the data. o Non-Repeatable Reads The data read by one transaction could be altered by a second transaction before the first transaction has committed. o Phantom Reads Two identical queries executing at the same time return different results. This could occur if, for example, another transaction commits [inserts] (an) additional row(s) into a table being referenced. Isolation Levels Described Ordered from the least to most restrictive. o Read Uncommitted Ensures that physically corrupt data is not read.  Dirty Reads  Non-Repeatable  Phantom o Read Committed The Default Database Engine Level  Non-Repeatable  Phantom o Repeatable Read Ensures that no other transactions can update or delete rows in the results set of each SELECT statement in the current transaction until the current transaction has been committed or rolled-back.  Phantom o Serializable The highest level, transactions are completely isolated from one another. Lock_Escalation Process of converting many fine-grain locks into fewer coarse-grain locks. o Reduces overhead at the expense of increasing the likelihood of concurrency contention. o The Lock_Escalation settings for a table can be determined from querying sys.tables o Lock Escalation Settings:  Table The default option  Auto Lock escalation is performed to the table level for non-partitioned tables, and to the partitioned level for partitioned tables.  Disable Lock escalation is generally disabled and only occurs in specific situations where it is absolutely necessary to endure data integrity. Rules for Efficient Transactions o Transactions should be as short as possible  Do not require input from users during transactions o Reduce versioning when appropriate with row versioning-based isolation level for readonly queries. Deadlocking Two or more tasks block each other by each having a lock on a resource which the other tasks are trying to lock o The system resolves these situations by choosing one task to be the Victim, and killing it. o

Design for Implicit and Explicit Concurrency


 Starting Transactions o Explicit Transactions Commenced using a BEGIN TRANSACTION statement, or through an API Function. o AutoCommit Transactions Each T-SQL statement is automatically committed when it completes.  The default mode for the Database Engine o Implicit Transactions Automatically starts a new transaction after the current transaction is committed or rolled back.  Enable Implicit Transactions: SET IMPLICIT TRANSACTIONS ON  Uncommitted transactions are automatically rolled-back. o Batched-Scoped Transactions Applicable only to a Multiple Active Results Set (MARS). A T-SQL transaction that starts under a MARS session becomes a batchscoped transaction that is not committed or rolled back when the batch completes is automatically rolled back by SQL Server.  Nested Transactions Explicit transactions can be nested. o Committing inner transactions is ignored by SQL Server. o The system treats a trigger as a nested transaction, even when in Auto-Commit Mode. o A roll-back in a nested transaction will roll-back to the outermost transaction.  Run-Time Errors and Transactions By default Run-Time errors only cause a roll-back of the offending statement. o Run-Time errors are not caused by incorrect syntax, which would produce a compile error, and prevent all statements from executing.  Ex1: code violates a check constraint.  Ex2: server is unable to open a corrupt or missing file.  Database Restore Options: o Restore an entire database from a full database backup (a complete restore). o Restore part of a database (a partial restore). o Restore specific files or filegroups to a database (a file restore). o Restore specific pages to a database (a page restore). o Restore a transaction log onto a database (a transaction log restore). o Revert a database to the point in time captured by a database snapshot.  Restoring a database to a specific point o Restore Point Options:  Specific Point in Time  Named Mark in the Transaction Log y Related databases can only be recovered to a marked transaction, not a specific point in time  Log Sequence Number (LSN) Restoration Path Diagram

 Methods of Recovery with Marked Transactions o STOPATMARK = mark_name Rolls the recovery forward to the mark and includes the marked transaction. o STOPBEFOREMARK = mark_name Does not include the marked transaction.

Example of Restoration to a marked transaction USE AdventureWorks2008R2; GO BEGIN TRANSACTION ListPriceUpdate WITH MARK 'UPDATE Product list prices'; GO UPDATE Production.Product SET ListPrice = ListPrice * 1.10 WHERE ProductNumber LIKE 'BK-%'; GO COMMIT TRANSACTION ListPriceUpdate; GO -- Time passes. Regular database and log backups are taken.

-- An error occurs in the database. USE master GO RESTORE DATABASE AdventureWorks2008R2 FROM AdventureWorksBackups WITH FILE = 3, NORECOVERY; GO RESTORE LOG AdventureWorks2008R2 FROM AdventureWorksBackups WITH FILE = 4, RECOVERY, STOPATMARK = 'ListPriceUpdate';  Run-Time Errors and Transactions o A Run-Time Error occurs when the codes syntax is correct, but the operation is still not able to complete.  Ex1: Program cannot open a corrupt file  Ex2: Statement violates a Check Constraint.  XACT_ABORT Option Controls how SQL Server handles a transaction when a run-time error occurs. o When OFF, only the T-SQL statement that raised the error is rolled back o Default value is OFF.

Designing the Locking Granularity Level


 Setting Transaction Isolation Levels Controls the locking and row versioning of statements issued by a connection to SQL Server, unless a Table Hint is used in the FROM Clause of a Select Statement. Syntax SET TRANSACTION ISOLATION LEVEL { READ UNCOMMITTED | READ COMMITTED | REPEATABLE READ | SNAPSHOT | SERIALIZABLE } [;]

Using Table Hints


Syntax: WITH ( <table_hint> [ [, ]...n ] )  o NOLOCK Allows SELECT Statements to ignore table locks.

 

 

 Allows Dirty Reads TABLOCKX Exclusive lock is taken on the table. o Only Locks during a query XLOCK Exclusive locks are taken on the appropriate granularity level when specified with ROWLOCK, TABLOCK, or PAGLOCK. o The lock is held until the transaction completes. READPAST Skips rows that are locked by other transactions rather than waiting for the lock to be released. UPDLOCK Update locks are to be taken and held until the transaction completes o Locks at row or page level only.

Row Versioning-Based Isolation Levels


 Database Versioning Options for Concurrency o READ_COMMITTED_SNAPSHOT Queries running under the Read Committed isolation level use row versioning.  Read Operations do not block update operations.  Version of the data read: Data committed before the start of each statement. o ALLOW_SNAPSHOT_ISOLATION Transaction Level Read Consistency.  Transactions can only access tables that have been enabled for snapshot.  Version of the data read: Data committed before the start of the transaction.  This isolation level is preferable when an application needs to generate reports based on long-running, multi-statement queries that must have point-in-time consistency. y Provides the benefit of repeatable reads without using shared locks.  When either READ_COMMITTED_SNAPSHOT or ALLOW_SMAPSHOT_ISOLATION database options are ON, logical copies (versions) are maintained for all data modifications performed in the database. o Previously committed images of rows that are modified are stored in tempdb database. o Each version is marked with the transaction sequence number of the transaction that made the change.  Transaction Sequence Number (XSN) - When either READ_COMMITTED_SNAPSHOT or ALLOW_SMAPSHOT_ISOLATION database options are ON, an XSN is assigned to every transaction that modifies data using row versioning.

Designing a Database for Optimal Performance


Resolve Performance Problems Using Plan Guides
 Plan Guides Influence optimization of queries by attaching query hints or a fixed query plan to them. Plan Guide Syntax

sp_create_plan_guide [ @name = ] N'plan_guide_name' , [ @stmt = ] N'statement_text' , [ @type = ] N'{ OBJECT | SQL | TEMPLATE }' , [ @module_or_batch = ] { N'[ schema_name. ] object_name' | N'batch_text' | NULL } , [ @params = ] { N'@parameter_name data_type [ ,...n ]' | NULL } , [ @hints = ] { N'OPTION ( query_hint [ ,...n ] )' | N'XML_showplan' | NULL }

Types of Plan Guides:  Object Stored Procedures, scalar UDFs, Multi-Statement table-valued UDFs, DML triggers.  SQL Stand-alone T-SQL, batches that arent part of an object.  Template Applies to any query that parameterized to the form indicated in statement_text. If TEMPLATE is specified, only the PARAMETERAZATION {FORCED | SIMPLE} query hint can be specified in the @hints parameter.  Sp_Control_Plan_Guide Used to control Plan Guides in the following ways. o Drop or disable a single plan guide. o Drop or disable all plan guides in the current database  Sp_Get_Query_Template Used to create a template plan guide by returning the parameterized version of a query. o Syntax for creating a Template Plan Guide EXEC Sp_Create_Plan_Guide NMyTemplatePlanGuide, @stmt, NTemplate, NULL, @Params, NOPTION(PARAMETERIZATION FORCED), o  PARAMETERIZATION Reusing execution plans for syntactically similar queries with different parameters. o Databases can be set to either simple or forced parameterization.  Simple Mode (Default option): The relational engine determines which queries can be parameterized.  Forced Mode: Any literal value in a DML statement is converted to a parameter during query compilation with some exceptions (EX: NO ANSI PADDING, RECOMPILE Query hint, etc.)

Design a Table and Index Compression Strategy


 DATA_COMPRESSION option At the page or row level, this option is only available with SQL Server 2008 Enterprise and Developer editions. Example: Apply compression to a specific table partition ALTER TABLE Table_Name

REBUILD PARTITION = 1 WITH (DATA_COMPRESSION=ROW);  Row Compression Uses variable length storage for numeric and fixed length text fields to save space. o Columns with variable length fields are not affected (i.e. Varchar and Varbinary)  Page Level Compression o Compressing the leaf level of tables and indexes with page compression consists of three operations in the following order:

1. Row compression 2. Prefix compression A value is identified for each column that is repeated in the data. The value is stored in the Compression Information structure (CI) and replaced from the data with a reference to the CI. 3. Dictionary compression Dictionary Compression searches for repeated data anywhere on the page (not just in the specific column), stores the data in CI and replaces the occurrences with a reference to CI.  Sp_estimate_data_compression_savings measures the space savings from using compression. Syntax: sp_estimate_data_compression_savings [ @schema_name = ] 'schema_name' , [ @object_name = ] 'object_name' , [@index_id = ] index_id , [@partition_number = ] partition_number , [@data_compression = ] 'data_compression' [;]

Physical Storage Review

 Direct Attached Storage (DAS) A digital Storage System directly attached to a server or workstation without a storage network in between. o The primary characteristic is that it is directly connected through a Host Bus Adapter (HBA) rather than via a network device such as a hub, switch, or router. o DAS is a retronym devised after the advent of storage sharing.  Storage Sharing OSs maintain their own file systems on their own dedicated (nonshared) LUNs. o LUN (Logical Unit Number) A number used to identify a logical unit.  Logical Unit A device addressed by the SCISI protocol or similar protocols such as Fibre Channel or iSCISI.  Common Example: A disk array has multiple physical SCISI ports, each one with a SCISI target address. The array is formatted as a RAID with multiple partitions. The SCISI target is configured to provide multiple LUs (one for each partition). A computer accesses a volume with the LUN assigned to it.  Types of Shared Storage: o Network Attached Storage (NAS) A computer connected to a network which only provides file based data storage services to other devices on the network.

Designed as an easy and self-contained solution for sharing files over a network.  Contrast with a SAN which only provides block-based storage, leaving file-system concerns to the client side. o Storage Area Network (SAN) Dedicated network that provides access to consolidated block level data storage.  Block Devices Support s random access and generally use buffered I/O routines. The OS allocates a single data buffer to hold a single block for each input and output. y I/O requests initiate the storage of data in the appropriate buffer. When all information is received or the buffer fills up, the appropriate action is performed (e.g. writing the data to disk)  SAN Benefits: y Servers can boot directly from the SAN. y Allows the quick and easy replacement of faulty servers by reconfiguring the SAN to give the LUN of the faulty device to the replacement server. y Can enable a more effective Disaster Recovery process. A SAN can span great distances which could contain a secondary storage array.

Design Scalable Database Solutions


 Scalable Shared Databases Scale out read-only database build exclusively for reporting purposes. In other words, they allow you to distribute multiple read-only databases to multiple instances for read-only reporting. o DB must reside on a set of dedicated, read-only volumes. o Volumes are mounted to multiple reporting servers and attached to instances of SQL Server 2005 or later, at which point they are available as shared scalable databases. o Benefits:  Easy, cost-effective way to scale out using commodity servers and hardware.  Workload Isolation Can prevent poorly tuned queries from monopolizing resources.  Each Server has an identical view of reporting data. o Restrictions:  Must be a read-only volume  Data files must be accessible via a SAN.  Databases are supported by Windows Server 2003 or later.  Does not support snapshots.  Building and Scaling Out Shared Databases

 Federated Database Servers Share the database processing load across a group of servers by horizontally partitioning the data in a database. o Servers are managed independently, but cooperate to process the database requests from applications.  Distributed Partitioned Views A partitioned view where the partitions are stored on multiple servers. o Linked Server definitions must be added. o Optimize Performance by setting the Lazy Schema Validation option by using the sp_serveroption for each linked server definition that employs a Distributed Partitioned View. This prevents the query processor from requesting meta-data for any of the linked tables until the data is needed.  Database Mirroring Primary benefits are that it is easier to administer than log shipping, and it has failover capability. o The mirroring schema consists of one Principal (production) server, and one Minor (mirror) server.  All actions performed on the Principal are replicated on the minor.  The minor re-does every insert, delete, etc.  All modes only support one minor and one principal. o Operating Modes  Asynchronous Transactions commit on the Principal before the mirror.  Synchronous increases transaction latency.  High Safety (Automatic Failover)  Requires a third server known as a Witness, which supports automatic failover verifying whether the principal server is up and functioning.

Optimizing Index Strategies


 Indexed Views The View result set is stored in the database (similar to storing a table with a clustered index). o Indexed views must be clustered, indexes cannot have non-clustered indexes. o Increases performance because the result set does not have to be generated each time the view is referenced. o The view must include the WITH SCHEMABINDING clause in the definition.  Filtered Indexes A non-clustered index defined with a specific WHERE clause to optimizer the index for specific queries. o Improves performance and saves space o Cannot reference the following column types:  Computed Columns  User Defined Types (UDTs)  Geography and Geometry  HierarchyID Syntax Example: Filtered Index CREATE NONCLUSTERED INDEX FI_Name ON Table_Name ( Col_Name1 [,] [Include (col1 [,.] )] WHERE Col_Name = Value [,..] ;  Indexes on Partitioned Tables Create an index on existing partitioned tables using the same partitioned scheme and key as the table.  Primary Keys automatically create a clustered index on a table if one does not already exist.  Covering Index- An index that covers all the columns in a SELECT list. o Can be used instead of the actual table to return the results.

Design a Table and Index Partition Strategy


 Steps to Creating a Partitioned Table 1. Create Partition Function a. The RANGE [LEFT | RIGHT] - determines the partition in which the boundary will reside. For example, if RANGE LEFT was specified (the default) for VALUES (1, 10, 20), the value 1 would be in the first partition. Partition Function Syntax CREATE PARTITION FUNCTION partition_function_name ( input_parameter_type ) AS RANGE [ LEFT | RIGHT ] FOR VALUES ( [ boundary_value [ ,...n ] ] ) [;]

2. Create Partition Schema based on the partition function. The filegroups can all reside on the same disk set: Whats important is to maximize I/O performance by using as many underlying disk spindles as possible. a. The partition schema maps particular partitions to filegroups. i. Using multiple filegroups is necessary to take advantage if filegroup backups or piecemeal restore are needed. ii. With multiple filegroups, multiple LUNs can be mapped on a SAN. b. ALL partition scheme is mapped to a single dedicated file group. Syntax: CREATE PARTITION SCHEME partition_scheme_name AS PARTITION partition_function_name [ ALL ] TO ( { file_group_name | [ PRIMARY ] } [ ,...n ] ) [;]

3. Create the table using the partition schema. a. The Partition Column must be a part of: i. The clustered index key ii. The primary key. iii. Unique index and uniqueness constraint keys.

 Altering a Partition Function partition functions are not static, and changes to them will propagate through the partition scheme to the partitioned table. o $PARTITION Function used to determine which partition a particular value would be placed in.  SELECT $PARTITION.partition_function_name (test value) y Could be used to measure the distribution of values within partitions if, for example, a column name was used instead of a static value.

o  SWITCH operation o Three ways to use the SWITCH operation:  Switch from a partition of a partitioned table to an empty non partitioned staging table.  Switch from a staging table to an empty partition of the partitioned table.  Switch a partition from one partitioned table TO an empty partition in another partitioned table. o Requirements for Indexes during a SWITCH operation  All indexes must be aligned.  No foreign keys can reference the partitioned table. o Syntax:  ALTER TABLE table_name SWITCH PARTITION1 TO tbl_2_name PARTITION4 y Will fail if PARTITION4 contained data. 

Designing Queries for Performance


Optimize and Tune Queries
 Cubing Vs. Rollup Cubing calculates the totals for every possible combination of variables, whereas rollup calculates the totals from most to least specific based on the hierarchy specified. o GROUPING SETS can explicitly specify the groups for which aggregate

information should be displayed.  Syntax: GROUP BY GROUPING SETS (groupingset1[.,groupingsetn])  Adding () into the grouping sets also displays a grand total grouping. o GROUPING_ID Function that computes the level of grouping. GROUPING_ID can only be used in the SELECT list, HAVING, or ORDER BY clauses when GROUP BY is specified  GROUPING_ID increases as the level of aggregation increases
SELECT D.Name ,E.Title ,GROUPING_ID(D.Name, E.Title) AS 'Grouping Level' ,COUNT(E.EmployeeID) AS N'Employee Count' FROM HumanResources.Employee E INNER JOIN HumanResources.EmployeeDepartmentHistory DH ON E.EmployeeID = DH.EmployeeID INNER JOIN HumanResources.Department D ON D.DepartmentID = DH.DepartmentID WHERE DH.EndDate IS NULL AND D.DepartmentID IN (12,14) GROUP BY ROLLUP(D.Name, E.Title) --HAVING GROUPING_ID(D.Name, E.Title) = 0; --All titles

--HAVING GROUPING_ID(D.Name, E.Title) = 1; --Group by Name

 APPLY Used to join the results of a table valued function by an outer table expression of a query. o OUTER APPLY Returns the results that produce a result, as well as those that dont o CROSS APPLY - Returns rows from the outer table only when they produce a result set from the table-valued function. Syntax Example: APPLY SELECT D.deptid, D.deptname, D.deptmgrid ,ST.empid, ST.empname, ST.mgrid FROM Departments AS D CROSS APPLY fn_getsubtree(D.deptmgrid) AS ST;  Table Hints o INDEX ( 0 ) Forces a clustered index scan if one is present, otherwise a full table scan is completed. o INDEX( index_value [,n] ) Specifies the indexes that will be used by the query optimizer, by using the name or ID of the indexes. o FORCE ORDER The query optimizer does not affect the join order specified in the query. o FORCESEEK Specifies that the query optimizer use only an index seek operation as the access path to the data in the table or view.
 FORCESEEK [ (index_value(index_column_name [ ,... n ] )) ]

Analyze Execution Plans


 3 SQL Server Join Operations o Nested Loop (AKA Nested Iteration) The best for a small table (<= 10 rows) joined to a large table indexed on its join columns.  Fastest because it requires the least I/O time, and fewest comparisons. o Merge Reads through both inputs at the same time, performing comparisons to determine which rows to return.  Merge join is usually the fastest with big data that can be obtained pre-sorted from existing b-tree indexes.  Requires that tables are presorted on the join columns. o Hash Efficiently process large, unsorted, non-indexed inputs. Hash joins read rows in a table, hash the join column, and store the results in a hash table to use with the remaining results.  Consumes the most memory, and can cause the tempdb to grow significantly.

Hash Joins have two inputs the Build Input and the Probe Input.  The Build input is the smaller of the two inputs.  Three types of Hash Joins:  In-Memory Hash Join The hash table is built in memory. After a table scan of the build input the results are put into hash buckets. Then the probe table is scanned one row at a time. The rows hash key is created and the appropriate bucket searched.  Grace Hash Join The build and probe are partitioned into smaller segments based on the hash function, and processed in batches. o Occurs when the input does not fit into memory. o The Hash join is applied to each pair of partitioned files after the partitioning.  Recursive Hash Join Employs large asynchronous I/O operations and additional partitioning steps for the larger partitions. o Occurs when the input is so large that it would require multiple partitioning steps, merge levels, and partitioning levels.  Useful for intermediate results queries because:  Intermediate results arent generally indexed or sorted for the next join operation.  Hash Joins reduce the need for de-normalization because  Logical and Physical Operators o Tempdb stores temporary objects created during SQL server operations.  Hash Joins, Hash Aggregates, and Sort operations all create intermediate results stored in tempdb.

Evaluate the use of Row-based vs. set-based operations.


 Cursors Provide a mechanism to work with one row or a small block of rows at a time. They are an extension to result sets. o Features extending results processing  Positioning at specific rows  Returning one or a block of rows at a time  Support different levels of visibility to changes made by other users. o Request a Cursor T-SQL modeled this method after the ISO standard and database API function through the following:  ADO  OLE DB  ODBC o Cursor Process: 1. Associate cursor with a result set and defines characteristics.

2. Execute T-SQL to populate cursor. 3. Retrieve Rows This operation which can retrieve one or multiple rows is called a FETCH. a. A series of Fetches to retrieve rows in either a forward or backward direction is called scrolling. 4. [Optional] Perform modification operations on the row at the current position in the cursor. 5. Close the cursor.
Transact-SQL Extended Cursor Syntax: DECLARE cursor_name CURSOR [ LOCAL | GLOBAL ] [ FORWARD_ONLY | SCROLL ] [ STATIC | KEYSET | DYNAMIC | FAST_FORWARD ] [ READ_ONLY | SCROLL_LOCKS | OPTIMISTIC ] [ TYPE_WARNING ] FOR select_statement [ FOR UPDATE [ OF column_name [ ,...n ] ] ] [;]

FOR UPDATE Identifies that all or some of the columns can be updated unless the READ_ONLY option is specified.  When column_name is supplied only the listed columns can be updated. o Cursor Types The four cursor types are available either through the APIs or via TSQL.  Static Detect few or no changes and consume few resources when scrolling.  Keyset-Driven Detect some changes but less than dynamic.  Dynamic Detect all changes, but consume the most amounts of resources.  Forward-Only The APIs see this as a type, T-SQL sees as an option.

Design an XML Strategy


Design a strategy to query and modify XML Data
 FLWOR Statement and Iteration (XQuery) o Acronym stands for: for, let, where, order by, and return  .query method example

Design a query strategy by using FOR XML


 PATH Each row in the row-set can be defined in detail, but column names and column aliases are specified as XPath expressions. o Root (ProspectData) Generates a root element named <ProspectData> which will enclose the other elements. o PATH(Prospect) creates a <Prospect> element for each row returned. o CompanyName AS @Name Supplies the alias of Name. o @ID and @Name The ampersand (@) generates these data as attributes for each <Prospect> element. o Detail/Type This alias generates a <Detail> element within the <Prospect> element that contains a <Type> sub-element. o Rating AS Detail/Rating Generates an additional <Rating> sub-element within the <Detail> element.

SELECT ID AS @ID, CompanyName AS @Name, Type AS Detail/Type, Rating AS Detail/Rating FROM Prospect FOR XML PATH(Prospect), Root (ProspectData);  TYPE directive used when the output of a query using FOR XML requires the output returned in the xml data type. SELECT BusinessEntityID, FirstName, LastName FROM Person.Person ORDER BY BusinessEntityID FOR XML AUTO, TYPE;

Design XML Storage


 Typed XML Instance XML Schemas are associated with the XML instance, such as a variable, parameter, or column of XML type. o Storage and query efficiency gains are possible with typed XML  XML Schemas Contain two groups of information Validation Constraints and Data Type Information. o Validation Constraints Whenever a typed XML instance is assigned to or modified, SQL server validates it against the constraints in the schema. o Data Type Information Provides information about the types of attributes and elements in the XML Data Type Instance.  XML Data Type Methods methods can only be used on the XML data type. They cannot, for example, be used on xml stored as a large object type. 

XML Data Type Methods


 Query() query(XQuery) : Where XQuery is a an XQuery expression, that queries for XML nodes such as elements, attributes in an XML instance. o Returns an instance of un-typed XML. o Example: SELECT @myDoc.query('/Root/ProductDescription/Features') o Used to query and retrieve XML elements and attributes from a XML instances as untyped xml.  Value() 'value(XQuery expression, SQL type) : Similar to the Query() method, this also performs an XQuery, but instead returns a SQL data type.  Exist() Returns either 1 to indicate that at least one value matching the XQuery exists, or 0 when no matches are present.  Modify() modify(XML DML) : Used in conjunction with an update statement, this method can be used to insert, delete, or replace the xml using a DML statement.  Nodes() Useful for shredding xml into relational data. It returns the data as a column within a table.

o Best way to move data from an xml data type column to a relational table, because it requires minimal effort and memory use. o Simple Example: Create a Stored Procedure using nodes to retrieve multiple lines for each Purchase Order line. The value() method is used with the table alias to return the details in SQL server data types.  CREATE PROCEDURE usp_name (@XML, xml) AS BEGIN SELECT t.c.value(@var1, int), t.c.value(@var2,varchar(50)) FROM @XML.nodes(/POlines/POline AS t(c) END y t. Is optional y Original Format: <POlines> <POline var1=### var2 = ### . /> <POline var1=### var2 = ### . /> .. </POlines> o Apply Nodes to a Column requires the use of the CROSS APPLY clause.  Sample XML from AdventureWorks Db
insert into #temp values('<root> Adventure Works CyclesFR-210B Instructions for Manufacturing HL Touring Frame. Summary: This document contains manufacturing instructions for manufacturing the HL Touring Frame, Product Model 7. Instructions are work center specific and are identified by Work Center ID. These instructions must be followed in the order presented. Deviation from the instructions is not permitted unless an authorized Change Order detailing the deviation is provided by the Engineering Manager.<Location LaborHours="2.5" LotSize="100" MachineHours="3" SetupHours="0.5" LocationID="10"> Work Center 10 - Frame Forming. The following instructions pertain to Work Center 10. (Setup hours = .5, Labor Hours = 2.5, Machine Hours = 3, Lot Sizing = 100)<step> Insert <material>aluminum sheet MS-2341</material> into the <tool>T-85A framing tool</tool>. </step><step> Attach <tool>Trim Jig TJ-26</tool> to the upper and lower right corners of the aluminum sheet. </step><step> Using a <tool>router with a carbide tip 15</tool>, route the aluminum sheet following the jig carefully. </step><step> Insert the frame into <tool>Forming Tool FT-15</tool>

and press Start. </step><step> When finished, inspect the forms for defects per Inspection Specification <specs>INFS-111</specs>. </step><step>Remove the frames from the tool and place them in the Completed or Rejected bin as appropriate.</step></Location><Location LaborHours="1.75" LotSize="1" MachineHours="2" SetupHours="0.15" LocationID="20"> Work Center 20 - Frame Welding. The following instructions pertain to Work Center 20. (Setup hours = .15, Labor Hours = 1.75, Machine Hours = 2, Lot Sizing = 1)<step> Assemble all frame components following blueprint <blueprint>1299</blueprint>. </step><step> Weld all frame components together as shown in illustration <diag>3</diag></step><step> Inspect all weld joints per Adventure Works Cycles Inspection Specification <specs>INFS-208</specs>. </step></Location><Location LaborHours="1" LotSize="1" LocationID="30"> Work Center 30 - Debur and Polish. The following instructions pertain to Work Center 30. (Setup hours = 0, Labor Hours = 1, Machine Hours = 0, Lot Sizing = 1)<step> Using the <tool>standard debur tool</tool>, remove all excess material from weld areas. </step><step> Using <material>Acme Polish Cream</material>, polish all weld areas. </step></Location><Location LaborHours="0.5" LotSize="20" MachineHours="0.65" LocationID="45"> Work Center 45 - Specialized Paint. The following instructions pertain to Work Center 45. (Setup hours = 0, Labor Hours = .5, Machine Hours = .65, Lot Sizing = 20)<step> Attach <material>a maximum of 20 frames</material> to <tool>paint harness</tool> ensuring frames are not touching. </step><step> Mix <material>primer PA-529S</material>. Test spray pattern on sample area and correct flow and pattern as required per engineering spec <specs>AWC-501</specs>. </step><step>Apply thin coat of primer to all surfaces. </step><step>After 30 minutes, touch test for dryness. If dry to touch, lightly sand all surfaces. Remove all surface debris with compressed air. </step><step> Mix <material>paint</material> per manufacturer instructions. </step><step> Test spray pattern on sample area and correct flow and pattern as required per engineering spec <specs>AWC-509</specs>. </step><step>Apply thin coat of paint to all surfaces. </step><step>After 60 minutes, touch test for dryness. If dry to touch, reapply second coat. </step><step> Allow paint to cure for 24 hours and inspect per <specs>AWC-5015</specs>. </step></Location><Location LaborHours="3" LotSize="1" SetupHours="0.25" LocationID="50"> Work Center 50 - SubAssembly. The following instructions pertain to Work Center 50. (Setup hours = .25, Labor Hours = 3, Machine Hours

= 0, Lot Sizing = 1)<step>Add Seat Assembly. </step><step>Add Brake assembly. </step><step>Add Wheel Assembly. </step><step>Inspect Front Derailleur. </step><step>Inspect Rear Derailleur. </step></Location><Location LaborHours="4" LotSize="1" LocationID="60"> Work Center 60 - Final Assembly. The following instructions pertain to Work Center 60. (Setup hours = 0, Labor Hours = 4, Machine Hours = 0, Lot Sizing = 1)<step> Perform final inspection per engineering specification <specs>AWC-915</specs>. </step><step>Complete all required certification forms.</step><step>Move to shipping.</step></Location></root>');

The following returns the material data element:

SELECT c.value('.', 'Varchar(MAX)') AS result FROM #temp CROSS APPLY x.nodes('/root/Location/step/material') as GO

T(c)

The following returns the LaborHours data attribute:


SELECT c.value('@LaborHours', 'Varchar(MAX)') AS result FROM #temp CROSS APPLY x.nodes('/root/Location') as T(c) GO

S-ar putea să vă placă și