Monday, 29 December 2014

Maintaining Database Integrity

TORN PAGE
Data pages are 8 kilobytes(KB) in size, but SQL Server divides a page into 16 blocks of 512 bytes a piece when performing write operations. 
If SQL Server begins writing blocks on a page and the disk system fails in the middle of the write process, only a portion of  page is written successfully, producing a problem called a torn page. 

Database Integrity Checks

CHECKSUM OR TORN PAGE DETECTION.
Databases have an option called PAGE_VERIFY. The page verification can be set to either TORN_PAGE_DETECTION or CHECKSUM. 

Note
 The PAGE_VERIFY TORN_PAGE_DETECTION option exists for backwards compatibility and should not be used. 

When the PAGE_VERIFY CHECKSUM option is enabled, SQL Server calculates a checksum for the page prior to the write. Each time a page is read off disk, a checksum is recalculated and compared to the checksum written to the page. If the checksums do not match, the page has been corrupted.

Important:
DBA should enable the PAGE_VERIFY CHECKSUM option on every production database.

CHECK INTEGRITY:
You can force SQL Server to read every page from disk and check the integrity by executing the DBCC CHECKDB command. 

Examples of DBCC CHECKDB is: 

 
Check the current database.DBCC CHECKDB-- Check the pubs database without nonclustered indexes.DBCC CHECKDB ('pubs', NOINDEX) 

When DBCC CHECKDB is executed, SQL Server performs all the following actions: 
·         Checks page allocation within the database 
·         Checks the structural integrity of all tables and indexed views 
·         Calculates a checksum for every data and index page to compare against the stored checksum 
·         Validates the contents of every indexed view 
·         Checks the database catalog. 

Integrity Errors
If an error occurs , it should be fixed.If an integrity error is found in       Index , drop and recreate the index.Table, use most recent backups to repair the damaged page.

     Validates Service Broker data within the database 

To accomplish these checks, DBCC CHECKDB executes the following commands: 

DBCC CHECKALLOC, to check the page allocation of the database 

DBCC CHECKCATALOG, to check the database catalog 

DBCC CHECKTABLE, for each table and view in the database to check the structural integrity .


DATABASE OPTION


A database has numerous options that control a variety of behaviors. These options are broken down into several categories, including the following:
  • Recovery
  •  Auto options
  •  Change tracking
  •  Access
  •  Parameterization

  • Recovery Options

The recovery options determine the behavior of the transaction log and how damaged pages are handled.

Recovery Models
Every database within a SQL Server instance has a property setting called the recovery model.
      The recovery model determines the types of backups you can perform against a database.

The recovery models available in SQL Server 2008 are:
  •  Full
  •  Bulk-logged
  •  Simple

THE FULL RECOVERY MODEL
When a database is in the Full recovery model, all changes made, using both data manipulationlanguage (DML) and data defi nition language (DDL), are logged to the transaction log. Because all changes are recorded in the transaction log, it is possible to recover a database in the Full recovery model to a given point in time so that data loss can be minimized.

THE BULK-LOGGED RECOVERY MODEL
Certain operations are designed to manipulate large amounts of data.
The Bulk-logged recovery model allows certain operations to be executed with minimal logging. The operations that are performed in a minimally logged manner with the database set in the Bulk-logged recovery model are: 
 BCP
 BULK INSERT
 SELECT. . .INTO
 CREATE INDEX
ALTER INDEX. . .REBUILD

THE SIMPLE RECOVERY MODEL
The third recovery model is Simple. A database in the Simple recovery model logs operations to the transaction log exactly as the Full recovery model does. However, each time the database checkpoint process executes, the committed portion of the transaction log is discarded. A database in the Simple recovery model cannot be recovered to a point in time because it is not possible to issue a transaction log backup for a database in the simple recovery model.
      Because the recovery model is a property of a database, you set the recovery model by using the ALTER DATABASE command as follows:

ALTER DATABASE database_name
SET RECOVERY { FULL | BULK_LOGGED | SIMPLE }


Damaged Pages

It is possible to damage data pages during a write to disk if you have a power failure or failures in disk subsystem components during the write operation. If the write operation fails to complete, you can have an incomplete page in the database that cannot be read. Because the damage happens to a page on disk, the only time that you see a result of the damage is when SQL Server attempts to read the page off disk.
     The default confi guration of SQL Server does not check for damaged pages and could cause the database to go off-line if a damaged page is encountered. 
  The PAGE_VERIFY CHECKSUM option can be enabled, which allows you to discover and log damaged pages.
      When pages are written to disk, a checksum for the page is calculated and stored in the page header. When SQL Server reads a page from disk, a checksum is calculated and compared to the checksum stored in the page header. If a damaged page is encountered, an 824 error is returned to the calling application and logged to the SQL Server error log and Windows Application Event log, and the ID of the damaged page is logged to the suspect_pages table in the msdb database.


Auto Options

There are five options for a database that enable certain actions to occur automatically or Control certain automatic behaviors.


AUTO_CLOSE

When set to ON, the database is shut down cleanly and its resources are freed after the last user exits. The database automatically reopens when a user tries to use the database again.When set to OFF, the database remains open after the last user exits.True for all databases when using SQL Server 2000 Desktop Engine or SQL Server Express, and False for all other editions, regardless of operating system.


AUTO_CREATE_STATISTICS

When set to ON, statistics are automatically created on columns used in a predicate.
When set to OFF, statistics are not automatically created; instead, statistics can be manually created.

AUTO_UPDATE_STATISTICS

When set to ON, any missing statistics required by a query for optimization are automatically built during query optimization.
      When set to OFF, statistics must be manually created. 

AUTO_SHRINK

When set to ON, the database files are candidates for periodic shrinking. Both data file and log files can be shrunk automatically by SQL Server. AUTO_SHRINK reduces the size of the transaction log only if the database is set to SIMPLE recovery model or if the log is backed up.
When set to OFF, database files are not automatically shrunk during periodic checks for unused space.


Change Tracking

 One of the challenges for any multiuser system is to ensure that the changes of one user do not accidentally overwrite the changes of another. To prevent the changes of multiple users from overriding each other, applications are usually built within mechanisms to determine whether a row has changed between the time it was read and the time it is written back to the database. The tracking mechanisms usually involve columns with either a datetime or
timestamp column and also might include an entire versioning system.
       SQL Server 2008 introduces a new feature implemented through the CHANGE_TRACKING database option.


Access

 Access to a database can be controlled through several options.
 The status of a database can be explicitly set to ONLINE, OFFLINE, or EMERGENCY. 
When a database is in an ONLINE state, you can perform all operations that would otherwise be possible.
 A database that is in an OFFLINE state is inaccessible. 
A database in an EMERGENCY state can be accessed only by a member of the db_owner role, and the only command  allowed to be executed is SELECT.
     You can control the ability to modify data for an online database by setting the database to either READ_ONLY or READ_WRITE.
User access to a database can be controlled through the SINGLE_USER, RESTRICTED_USER and MULTI_USER options. When a database is in SINGLE_USER mode, only a single user is allowed to access the database. A database set to RESTRICTED_USER only allows access to
members of the db_owner, dbcreator, and sysadmin roles.
 If multiple users are using the database when you change the mode to SINGLE_USER or users that conflict with the allowed set for RESTRICTED_USER, the ALTER DATABASE command is blocked until all the non-allowed users disconnect.


 Parameterization

One of the “hot button” topics in application development is whether to parameterize calls
to the database. When a database call is parameterized, the values are passed as variables.

SQL Server caches the query plan for every query that is executed. Unless there is pressure on the query cache that forces a query plan from the cache, every query executed since the instance started is in the query cache. When a query is executed, SQL Server parses and compiles the query. The query is then compared to the query cache using a string-matching algorithm. If a match is found, SQL Server retrieves the plan that has already been generated
and executes the query.
A query that is parameterized has a much higher probability of being matched because the query string does not change even when the values being used vary. Therefore parameterized queries can reuse cached query plans more frequently and avoid the time
required to build a query plan.

Forced Parameterization

Not all applications parameterize calls to the database, you can force SQL Serverto parameterize every query for a given database by setting the PARAMETERIZATION
FORCED database option.
     The default setting for a database is not to force parameterization. The reuse of query plans provides a benefi t so long as the query plan being reused is the most effi cient path through the data. For tables where there is signifi cant data skew, one value produces an efficient
query plan, whereas another value causes a different query plan to be created. In addition,applications see the effect of parameterization only if the majority of database calls have an extremely short duration.

REFRENCES

Microsoft SQL Server 2008 Implementation and Maintenance















DATABASE CONFIGURATION AND MAINTENANCE

Data is store within database on disk in one or two files.

TYPES OF FILE

SQL Server uses two different types of files.

1.Data files 
2.Transaction log files.

DATA FILES


 Data files are responsible for the long-term storage of all the data within a database.
      Data files contain data and objects such as tables, indexes, stored procedures, and views. Data files can be grouped together in filegroups for allocation and administration purposes.


TRANSACTION LOG FILES

Transaction log files are responsible for storing all the transactions that are executed against a database. Log files contain the information that is required to recover all transactions in the database.


FILEGROUPS

Filegroups are a logical structure,  defined within a database, that map a database and the objects contained within a database, to the data files on disk. Filegroups can contain more than one data file.

DATA MANIPULATION

All data manipulation within SQL Server occurs in memory within a set of buffers. If you are adding new data to a database, the new data is first written to a memory buffer, then written to the transaction log, and Finally persisted to a data file via a background process called check pointing.


FILE EXTENTION

Designing the data layer of a database begins with the database creation. When you create a database, it should have three Files and two filegroups. You should have a file with an mdf extension within a filegroup named PRIMARY, a file with an ndf extension in a filegroup with any name that you choose, and the transaction log with an ldf extension.
     The file extensions SQL Server uses conventionally are .MDF, .NDF and .LDF respectively.

MDF
MDF is more commonly known as the primary data file. It is believed that it originally stood for the master database file before the master database concept arrived. It contains a database header and system tables and points to other files in the database . User objects and relational data can be stored in the primary data file and/or in many secondary files.
NDF
NDF is an instance of a secondary data file, N denoting the canonical term for ‘1..N’. Some databases do not need or have any secondary files .  Separation of a primary and N secondary data files is primarily useful to allow systematic growth of the database across multiple disks. It is also very useful to increase IO performance, allowing less contention for file locks and allowing multi-threaded access to the data.
LDF
LDF is the Log Data File (the transaction log). One or more transaction log files can exist for a database and are used to help disaster recovery scenarios as well as ensure that operations on the data are ACID.

Default Filegroup
When objects are created in the database without specifying which filegroup they belong to, they are assigned to the default filegroup.  The files in the default filegroup must be large enough to hold any new objects not allocated to other filegroups.
        The PRIMARY filegroup is the default filegroup unless it is changed by using the ALTER DATABASE statement. Allocation for the system objects and tables remains within the PRIMARY filegroup, not the new default filegroup.

ALTER DATABASE <database name> MODIFY FILEGROUP <filegroup name> DEFAULT

TRANSACTION FILES...

A transaction log is a sequential record of all changes made to the database while the actual data is contained in a separate file. The transaction log contains enough information to undo all changes made to the data file as part of any individual transaction. The log records the start of a transaction, all the changes considered to be a part of it, and then the final commit or rollback of the transaction. Each database has at least one physical transaction log and one data file that is exclusive to the database for which it was created.
     SQL Server keeps a buffer of all of the changes to data for performance reasons. It writes items to the transaction log immediately, but does not write changes to the data file immediately. A checkpoint is written to the transaction log to indicate that a particular transaction has been completely written from the buffer to the data file. When SQL Server is restarted, it looks for the most recent checkpoint in the transaction log and rolls forward all transactions that have occurred from that point forward since it is not guaranteed to have been written to the data file until a checkpoint is entered in the transaction log. This prevents transactions from being lost that were in the buffer but not yet written to the data file.


FILESTREAM DATA..

To tackle the problem of storing, managing, and combining the large volumes of unstructured databases with the structured data in your databases, SQL Server 2008 introduced FILESTREAM.
     The FILESTREAM feature allows you to associate files with a database. The files are stored in a folder on the operating system, but are linked directly into a database where the fi les canbe backed up, restored, full-text-indexed and combined with other structured data. 

Thursday, 25 December 2014

Configuring Processor and I/O Affinity [SQL SERVER 2012]

Configuring Processor and I/O Affinity


       Processor affinity assigns specific server processors to specific threads. This eliminates processor reloads and reduces thread migration across processors.
         I/O affinity binds an instance’s disk I/O to a specific set of CPUs. You configure processor and I/O affinity on the Processors page of an instance’s Server Properties dialog box.The default setting for each instance is to configure processor affinity mask and I/O affinity mask automatically for all processors on the host server.
           When configuring affinity through SQL Server Management Studio, you cannot configure both processor affinity and I/O affinity for the same processor.
      It is possible to configure processor affinity and I/O affinity for the same processor by using Transact-SQL, although this is not recommended and will decrease performance.
             To distribute SQL worker threads across CPUs 2 and 3 based on server workload, use the following Transact-SQL statement:

ALTER SERVER CONFIGURATION SET PROCESS AFFINITY CPU = 2,3

      To distribute SQL worker threads across all CPUs based on server workload, use the following command:

ALTER SERVER CONFIGURATION SET PROCESS AFFINITY CPU = AUTO


You can’t configure I/O affinity by using the ALTER SERVER CONFIGURATION statement
and must instead use sp_configure with the affinity I/O mask option. Microsoft recommends
that you do not modify affinity I/O mask from the default setting.

Wednesday, 15 October 2014

Instance Level Settings [SQL SERVER 2012]

Instance Level Settings

Instance-level settings is also known as SQL Server options include memory use, fill factor, processor, I/O Affinity etc.SQL Server  settings apply only for one instance without affecting other instances on the same host computer.
                   Instance-level settings can be configured through the Server Properties dialog box. The Server Properties dialog box contains the following pages:

General
View general information about the instance, including the host operating system, root directory, and server collation. You can’t modify settings on this page.
Memory 
Configure instance memory options, including minimum, maximum, index creation memory, and minimum memory per query.

Processors
Configure processor affinity and I/O affinity, limit worker threads, boost SQL Server priority, and use Windows fibers (lightweight pooling).

Security 
Configure server authentication, log on auditing, server proxy account, Common Criteria compliance, C2 audit tracing, and cross-database ownership Chain

Connections
Configure maximum connections; query governor and default connection options, whether to allow remote connections, and whether to require distributed transactions for server-to-server communication.

Database Settings
Configure default index fill factor, default backup media retention, recovery filter and database default locations.

Advanced 
Configure FILESTREAM options; enable contained databases; and trigger firing, two-digit year cutoff, network packet size, remote logon timeout, and parallelism options.

Permissions  

Configure instance-level permissions.

Configuring Memory Allocation

         By default, a SQL Server 2012 instance uses memory dynamically, querying the host operating system on a periodic basis to determine how much free memory is available and releasing memory back to the host as needed to avoid paging.
       
Instance-Level Settings of Memory:

Min Server Memory  

Max Server Memory 
 
Lowest Min Memory for SQL Server 2012
32-bit = 64 MB
64-bit = 128 MB

Default Settings

Min memory  =  0 MB
Max memory =  2,147,483,647 MB

Use sp_configure when  advanced options are enabled to configure maximum

and minimum server memory. 


Configuring Instance to use Min = 1024 MB , Max = 8,096 MB
EXEC sys.sp_configure 'show advanced options', 1;
GO
RECONFIGURE; GO
EXEC sys.sp_configure 'min server memory', 1024;
GO
EXEC sys.sp_configure 'max server memory', 4096;
GO
RECONFIGURE;
GO

REFERENCES:

Training Kit(Exam 70-462): Administering Microsoft SQL Server 2012 Databases

Memory Allocation on MSDN