Вы находитесь на странице: 1из 8

3.

Working with database


Sql server supports

3 types of databases

1.System databases
2.user defined
3.Sample databases

1System databases
At the time of installation 5 system databases are installed
1.Master
2.Model
3.Msdb
4.Temp db
5.Resource DB

These database consists of meta data


1. Master:
it acts as a entry point of sql server
It consists of all server level information
Like Server level settings ,logins ,endpoint ,linked servers
Master data file & log files are two startup parameters for SS
It recovery model is simple
2.Model :

It acts as template for new database


It consists all system level information for new database
It consists of 1978 system objects in sql server 2008
Its recovery model full

3.MSDB: it consists of all automation information


Like Backup, Restores, jobs , alerts, maintenance plans,Db mail information
It recovery model is simple

4.Temp db: it is global temporary area available for an instance


It consists of
Temporary table ,triggers,views, joints results & hash tables
It is cleared once we can restart the server
The process of ordering & grouping done here
It recovery model is simple

5. Resource Database : it is one of hidden database


It consists of all system defined objects physically
It maintain the service pack changes
We can not work on this .its data & log files are present in Binn folder.
6 .Distribution: once we configure replication distribution db is created
It consistis of complete replication details

User defined Database


It is created by the user .consists of 1788 (2005) & 1978 (2008) system
defined objects
Every db consists of
Logically collection of objects
Physically collection of files
Every db consists of 2types of files
1. Data file (default name = dbName)
2. Transactional log files (default name = dbName_log)

1 .Filegroup: To group of similar data files logically. By default every db


consists of primary file group
We can add our own file group
To view file groups
Select * from sys.filegroups
Advantages:
For better performance
Take more than one file at a time
To store table data into required file
2. Data files: It consists of actual data ,meta data and services
Check point takes data from transaction log file into data file.
Max size for a data file 16 TB
To view the details of files of a db
Sp_helpdb <database name>
To view the free space present in data file

Dbcc showfilestats
Total extents-used extents*64=free space in data file

Sql server supports 2 types of data files


Primary data file (.mdf)
Secondary data file (.ndf)
Primary data file(.Mdf): it consists of complete meta data of db
System defined objects
Details of other files
Only one primary data file per database
Secondary data file(.Ndf): it consists data & user defined objects & we
can add our own no of .ndf files max 32767

3.Extent: to group pages of same object is called extent .in order to


improve performance we can use extents
Each extent consists of 8 pages i.e 64KB
Sql server supports 2 types of extents
Uniform extents
Consists of pages from same object
Mixed extent
Consists of pages from different object .

Introduction to page: it is the physical unit where actual data is present


Page size is 8kb
Page header takes 96 bytes of memory

To check pages of a table


Dbcc ind (dbname,table name,-1)
Or using DMV (sys.dm_db_database_page_allocations) this script

Types of pages:
1. Data page: it consists of actual data default size is 8kb
2. Index allocation map(IAM) page: it consists of page no s which are
allocated to a table or index
3. index page: it consists of index entries ie column name & row reference
4. Differential change map(DCM) page : it consists of information of
extents modified after backup command.
5 .bulk changed map (BCM) page : it consists of information of extents
modified after bulk operation
6.page free space: It consists of page no & free spaces
7.Text & image page: it consists of information of text ,image .can contain
2gb data

8. Global allocation map :consists of information of extents how many are


free ,how many are used
9. Shared global allocation map: information of mixed extents

Transactional Log file (.Ldf): it consists of both committed & uncommitted transactions
Once check points occurs all the committed transactions are taken into data
file.
Every transactions is recorded with unique no called LSN (Log sequence no)
Max size 2TB
To view the percent of log used
Dbcc sqlperf(logspace)
Or using DMV
Select *from sys.dm_db_log_space_usage
The main advantage is to recover data point of failure
Implementing log shipping , transactional replication.

VLF types
1.Active vlf : it is at least one transaction which should be delivered to some
other server
2.Recoverble: if the transaction are delivered from active vlf to other server
like mirror or distributor then its become recoverable
3.Reusable vlf:once t-log backup was generated or check point occurs in
simple recovery model recoverable vlf is truncated and becomes reusable vlf.
4.unused : never used
To check vlfs we can use

Dbcc loginfo
Select * from sys.databases
Sp_helpdb

Database states:
1.Online : allows manipulations selections ets .always production
db in online state
2.Offline: for moving data files
3.Restoring : when restoring database .in mirroring mirror server
should be in restoring mode .
4.Suspect mode: If the file of the data base is damaged or
corrupted .
If more than 100 pages are corrupted or when
restoration failed
5.Standby: DB is available in read only mode . In log shipping
secondary server in standby mode
To find state of database
Select name , state_desc from sys.databases
Select databaseproperyex(databasename,status)

Questions & answers


1.Tempdb is growing fastly .how to know which transactions?

Ans. These are the resons for tempdb growing fastly


1.for long running quries.
2.creation or rebuilding index
3.run dbcc checkdb command for consistency of database.
4.temparary tables ,cursors etc. & orphan objects

How to resolve
Add extra data file if possible
Verify long running quires and stop
Dbcc opentran(tempdb)
Find out which sessions taking more space and trouble shoot the quiries
Select * from sys.dm_db_session_space_usage .
Try to shrink the log file
Dbcc shrinkfile(tempdb,0)
Restart the server if possible.
2.My transaction log file is full .how to troubleshoot the scenario?
Ans. If there is no scheduled log backups or log backups are not working .
If there are active transactions in the log file .
If there are long running queries .
In data base mirroring mirror server fails .
In replication if distribution server fails.
Trouble shooting:
Select name,log_reuse_wait_desk from sys.databases where
name=databasename
Depending on 2nd value we have to trouble shoot by using the following
solution
Backup the log
Adding log file to a different disk .
Trouble shoot if any mirroring or replication issues.

3.If database goes into suspect mode ,how to recover it?


Ans. First we need to check db state
Select databasepropertyex(database name,status)
If it is in suspect mode .then follow the
Step-1 make the database into single user mode

Alter database dbname set single_user


Step-2 make the database into Emergency mode
Alter database dbname set emergency
Step-3 Run the command with query
Dbcc checkdb (dbname,repair_allow_data_loss)
Step-4 set multi user mode
Alter database dbname set multi_user