Tuesday, April 29, 2008

Mirroring notes...

In database mirroring, an originating SQL Server 2005 instance continuously sends a database's transaction log records to a copy of the database on another standby SQL Server instance.

The originating database and server have the role of principal, and the receiving database and server have the role of mirror.

The principal and mirror servers must be separate instances of SQL Server 2005.

Some important items to note about database mirroring:
• The principal database must be in the FULL recovery model. Log records that result from bulk-logged operations cannot be sent to the mirror database.
• The mirror database must be initialized from a restore of the principal database with NORECOVERY, followed by restores in sequence of principal transaction log backups.
• The mirror database must have the same name as the principal database.
• Because the mirror database is in a recovering state, it cannot be accessed directly. You can create database snapshots on the mirror to indirectly read the mirror database at a point in time.


Transaction Safety
If transaction safety is set to FULL, the principal and mirror servers operate in a synchronous transfer mode. As the principal server hardens its principal database log records to disk, it also sends them to the mirror. The principal then waits for a response from the mirror server. The mirror responds when it has hardened those same log records to the mirror's log disk. When safety is set OFF, the principal does not wait for acknowledgment from the mirror, and so the principal and mirror may not be fully synchronized (that is, the mirror may not quite keep up with the principal).
Synchronous transfer guarantees that all transactions in the mirror database's transaction log will be synchronized with the principal database's transaction log, and so the transactions are considered safely transferred. You set safety to FULL using
ALTER DATABASE [dbname] SET SAFETY FULL;
When safety is set to OFF, the communication between the principal and the mirror is asynchronous. The principal server will not wait for an acknowledgment from the mirror that the mirror has hardened a block of transaction records. The mirror will attempt to keep up with the principal, by recording transactions as quickly as possible, but some transactions may be lost if the principal suddenly fails and you force the mirror into service.




to get a list of all endpoints along with Port numbers
Select * from sys.tcp_endpoints


To get info about the state of mirroring....includes the lsn for failover..should be same as that in the mirror for failover with out data loss.

select * from sys.database_mirroring


Manually force a failover

ALTER DATABASE demo SET PARTNER FAILOVER
at the principal

Check the mirror failover lsn before doing this so as to ensure there is no data loss.


High Availability Mode
It requires that you set safety to FULL and define a witness server as part of the database mirroring session.
The High Availability mode is best used where you have fast and very reliable communication paths between the servers and you require automatic failover for a single database.

High Protection
The High Protection operating mode also has transactional safety FULL, but has no witness server as part of the mirroring session. The principal database does not need to form a quorum to serve the database. In this mode only a manual failover is possible, because there is no witness to fill the tie-breaker role.

High Performance
In the High Performance operating mode, transactional safety is OFF, and the transfer of log records is asynchronous. The principal server does not wait for an acknowledgement from the mirror that all transaction log records have been recorded on the mirror.
Manual failover is not enabled for the High Performance mode. The only type of failover allowed is forced service failover, which is also a manual operation:
ALTER DATABASE dbname SET PARTNER FORCE_SERVICE_ALLOW_DATA_LOSS


SQL Express and the Workgroup Edition can be used as a witness server, but they cannot be used as a partner server in database mirroring.


Principal Server States

When safety is FULL, the normal operating state for the principal database is the SYNCHRONIZED state. When safety is OFF, the normal operating state for the principal database starts as the SYNCHRONZING state. Once the mirror has caught up, the state goes to SYNCHRONIZED and stays there regardless of how far behind it is.
• If safety is FULL, the principal database always starts off in the SYNCHRONIZING state, and transitions to the SYNCHRONIZED state when the principal and mirror transactions logs are synchronized.
• If safety is FULL and the principal server is disconnected from the witness server but can still process transactions, the database is exposed.


Principal Database states with a witness set and with safety FULL and safety OFF




Principal Database states with no witness set and with safety FULL and safety OFF





For both safety settings, if the session is paused or there are redo errors on the mirror, the principal enters the SUSPENDED state. If the mirror becomes unavailable, the principal will enter the DISCONNECTED state.
In the DISCONNECTED and SUSPENDED states:
• When a witness has been set, if the principal can form a quorum with the witness or mirror server, the principal database is considered exposed. That means the principal database is active with user connections and processing transactions. However, no log records are being sent to the mirror database, and if the principal should fail, the mirror will not have any of the transactions from the principal from the point the principal entered that state. Also, the principal's transaction log cannot be truncated, so the log file will be growing indefinitely.
• When a witness has been set, if the principal cannot form a quorum with another server, it cannot serve the database. All users will be disconnected and no new transactions will be processed.
• When safety is OFF, the principal database is considered exposed, because no transaction log records are being sent to the mirror.


Mirror Server Database States





Witness Server States

Monday, March 17, 2008

Codd's Rules

Codd's Rules for a Truly Relational Database System

1. Information is represented logically in tables.
2. Data must be logically accessible by table, primary key, and column.
3. Null values must be uniformly treated as "missing information," not as empty strings, blanks, or zeros.
4. Metadata (data about the database) must be stored in the database just as regular data is.
5. A single language must be able to define data, views, integrity constraints, authorization, transactions, and data manipulation.
6. Views must show the updates of their base tables and vice versa.
7. A single operation must be able to retrieve, insert, update, or delete data.
8. Batch and end user operations are logically separate from physical storage and access methods.
9. Batch and end user operations can change the database schema without having to re-create it or applications built upon it.
10. Integrity constraints must be available and stored in the metadata, not in an application program.
11. The data manipulation language of the relational system should not care where or how the physical data is distributed and should not require alteration if the physical data is centralized or distributed.
12. Any row processing done in the system must obey the same integrity rules and constraints that set-processing operations do.

Thursday, March 13, 2008

Redundancy methods

Hot Standby
A method of redundancy in which the primary and secondary (i.e., backup) systems run simultaneously. The data is mirrored to the secondary server in real time so that both systems contain identical information.

Warm Standby
A method of redundancy in which the secondary (i.e., backup) system runs in the background of the primary system. Data is mirrored to the secondary server at regular intervals, which means that there are times when both servers do not contain the exact same data.

Cold Standby
A method of redundancy in which the secondary (i.e., backup) system is only called upon when the primary system fails. The system on cold standby receives scheduled data backups, but less frequently than a warm standby. Cold standby systems are used for non-critical applications or in cases where data is changed infrequently.

Wednesday, February 13, 2008

Recovery models

Simple Recovery Model

This recovery model facilitates the maintenance of a database by making the transaction log virtually maintenance free. There are limitations placed on the recoverability of a database if this recovery model is used.

Bulk-Logged Recovery Model

A database in this recovery model will have minimum logging for bulk import operations. Space allocation and deallocation is only logged for bulk import operations. Point-in-time and point-of-failure recovery may be possible when a database is in Bulk-Logged Recovery model.

Full Recovery Model

SQL Server performs full transaction logging for any bulk load operations if a database is in Full Recovery model. Transaction log backups should be performed at regular intervals for maximum recoverability. This model provides the safest mode of operation for production systems.




Complete/Full - Backs up the entire database.

Differential - Backs up only modified extents since the previous complete backup.

Transaction Log - Backs up the active portion and truncates the inactive portion of the transaction log.

File / Filegroup - Backs up individual files and filegroups within a database.

T-SQL and DB tales

create database a
on
(
name = 'a_data',
filename = 'C:\a.mdf',
size = 2MB, --all default sizes are in MB
maxsize = 10MB, --Unlimited
filegrowth = 2
)
Log on
(
name = 'a_log',
filename = 'C:\a.ldf',
size = 1MB,
maxsize = 5,
filegrowth = 2
)



create database b
on
(
name = 'b_data',
filename = 'C:\b.mdf',
size = 2MB, --all default sizes are in MB
maxsize = 10MB,
filegrowth = 2
),
filegroup new
(
name = 'b_data2',
filename = 'C:\b.ndf',
size = 2MB, --all default sizes are in MB
maxsize = 10MB,
filegrowth = 2
)
Log on
(
name = 'b_log',
filename = 'C:\b.ldf',
size = 1MB,
maxsize = 5,
filegrowth = 2
)


backup database b to disk = 'C:\b.bak'

restore database c from disk = 'C:\b.bak'
with
move 'b_data' to 'C:\c.mdf',
move 'b_data2' to 'C:\c.ndf',
move 'b_log' to 'C:\c.ldf'

alter database c add filegroup new2

alter database c
add file
(
name = 'c_data3',
filename = 'C:\c3.ndf',
size = 2MB, --all default sizes are in MB
maxsize = 10MB,
filegrowth = 2
) to filegroup new2



backup database a to disk = 'c:\a.bak' with differential


restore database b from disk = 'c:\a.bak' with file = 1, norecovery,
move 'a_data' to 'D:\Data\b.mdf',
move 'a_log' to 'E:\Log\b.ldf'


restore database b from disk = 'c:\a.bak' with file = 2, norecovery,
move 'a_data' to 'D:\Data\b.mdf',
move 'a_log' to 'E:\Log\b.ldf'

restore database b with recovery

Server Startup parameters

Default parameters

-d master_file_path
The fully qualified path for the master database file (typically, C:\Program Files\Microsoft SQL Server\MSSQL.n\MSSQL\Data\master.mdf). If you do not provide this option, the existing registry parameters are used.

-e error_log_path
The fully qualified path for the error log file (typically, C:\Program Files\Microsoft SQL Server\MSSQL.n\MSSQL\LOG\ERRORLOG). If you do not provide this option, the existing registry parameters are used.

-l master_log_path
The fully qualified path for the master database log file (typically C:\Program Files\Microsoft SQL Server\MSSQL.n\MSSQL\Data\mastlog.ldf).



-c
Shortens startup time when starting SQL Server from the command prompt. Typically, the SQL Server Database Engine starts as a service by calling the Service Control Manager. Because the SQL Server Database Engine does not start as a service when starting from the command prompt, use -c to skip this step.

-f
Starts an instance of SQL Server with minimal configuration. This is useful if the setting of a configuration value (for example, over-committing memory) has prevented the server from starting.

-g memory_to_reserve
Specifies an integer number of megabytes (MB) of memory that SQL Server will leave available for memory allocations within the SQL Server process, but outside the SQL Server memory pool. The memory outside of the memory pool is the area used by SQL Server for loading items such as extended procedure .dll files, the OLE DB providers referenced by distributed queries, and automation objects referenced in Transact-SQL statements. The default is 256 MB.

-h
Reserves virtual address space for Hot Add memory metadata when AWE is enabled with 32-bit SQL Server 2005. Required for Hot-Add memory with 32-bit AWE, but consumes about 500 MB of virtual address space and makes memory tuning more difficult. Not required for 64-bit SQL Server. Hot Add Memory is only available for Windows Server 2003, Enterprise and Datacenter editions.

-m
Starts an instance of SQL Server in single-user mode. When you start an instance of SQL Server in single-user mode, only a single user can connect, and the CHECKPOINT process is not started. CHECKPOINT guarantees that completed transactions are regularly written from the disk cache to the database device.

-n
Does not use the Windows application log to record SQL Server events. If you start an instance of SQL Server with -n, we recommend that you also use the -e startup option. Otherwise, SQL Server events are not logged.

-s
Allows you to start a named instance of SQL Server 2005. Without the -s parameter set, the default instance will try to start. You must switch to the appropriate BINN directory for the instance at a command prompt before starting sqlservr.exe. For example, if Instance1 were to use \mssql$Instance1 for its binaries, the user must be in the \mssql$Instance1\binn directory to start sqlservr.exe -s instance1.

-T trace#
Indicates that an instance of SQL Server should be started with a specified trace flag (trace#) in effect.

-x
Disables the keeping of CPU time and cache-hit ratio statistics. Allows maximum performance.