You are on page 1of 27

1

Restore database testing from disk='G:\SQLDATA\Testing.bak'


with noreo!er"
# Go
$
Restore %og testing from disk='G:\SQLDATA\1.trn' with
noreo!er"
& Go
'
Restore %og testing from disk='G:\SQLDATA\#.trn' with reo!er"(
sto)at='Se) &( #*1# +:'$:** ,-'
.orma%i/ation
A))%" the so0a%%ed normalization rules to hek whether "o1r database is
str1t1ra%%" orret and o)tima%.
First Normal Form (1NF): A tab%e is 1.2 if e!er" e%% ontains a sing%e
!a%1e( not a %ist of !a%1es. This )ro)erties is known as atomic. 1.2 a%so
)rohibits re)eating gro1) of o%1mns s1h asitem1( item#(..( item.. 3nstead(
"o1 sho1%d reate another tab%e 1sing one0to0man" re%ationshi).
Second Normal Form (2NF): A tab%e is #.2( if it is 1.2 and e!er" non0ke"
o%1mn is f1%%" de)endent on the )rimar" ke". 21rthermore( if the )rimar" ke"
is made 1) of se!era% o%1mns( e!er" non0ke" o%1mn sha%% de)end on the
entire set and not )art of it.
2or e4am)%e( the )rimar" ke" of the 5rderDetai%s tab%e
om)rising order3D and )rod1t3D. 3f 1nit,rie is de)endent on%"
on )rod1t3D( it sha%% not be ke)t in the 5rderDetai%s tab%e 6b1t in
the ,rod1ts tab%e7. 5n the other hand( if the 1nit,rie is de)endent on the
)rod1t as we%% as the )arti1%ar order( then it sha%% be ke)t in
the 5rderDetai%s tab%e.
Third Normal Form (3NF): A tab%e is $.2( if it is #.2 and the non0ke"
o%1mns are inde)endent of eah others. 3n other words( the non0ke" o%1mns
are de)endent on )rimar" ke"( on%" on the )rimar" ke" and nothing e%se. 2or
e4am)%e( s1))ose that we ha!e a ,rod1ts tab%e with
o%1mns )rod1t3D 6)rimar" ke"7( name and 1nit,rie. The
o%1mn diso1ntRate sha%% not be%ong to ,rod1ts tab%e if it is a%so de)endent
on the 1nit,rie( whih is not )art of the )rimar" ke".
Higher Normal Form: $.2 has its inade81aies( whih %eads to higher
.orma% form( s1h as 9o"e:;odd .orma% form( 2o1rth .orma% 2orm 6&.27
and 2ifth .orma% 2orm 6'.27( whih is be"ond the so)e of this t1toria%.
At times( "o1 ma" deide to break some of the norma%i/ation r1%es( for
)erformane reason 6e.g.( reate a o%1mn a%%ed tota%,rie in 5rders tab%e
whih an be deri!ed from theorderDetai%s reords7< or bea1se the end01ser
re81ested for it. -ake s1re that "o1 f1%%" aware of it( de!e%o) )rogramming
%ogi to hand%e it( and )ro)er%" do1ment the deision.
3ntegrit" R1%es
=o1 sho1%d a%so a))%" the integrit" r1%es to hek the integrit" of "o1r design:
Entity Integrity Rule: The )rimar" ke" annot ontain .>LL. 5therwise( it
annot 1ni81e%" identif" the row. 2or om)osite ke" made 1) of se!era%
o%1mns( none of the o%1mn an ontain .>LL. -ost of the RD9-S hek
and enfore this r1%e.
Referential Integrity Rule: ?ah foreign ke" !a%1e m1st be mathed to a
)rimar" ke" !a%1e in the tab%e referened 6or )arent tab%e7.
=o1 an insert a row with a foreign ke" in the hi%d tab%e on%" if the !a%1e
e4ists in the )arent tab%e.
3f the !a%1e of the ke" hanges in the )arent tab%e 6e.g.( the row 1)dated or
de%eted7( a%% rows with this foreign ke" in the hi%d tab%e6s7 m1st be hand%ed
aording%". =o1 o1%d either 6a7 disa%%ow the hanges< 6b7 asade the hange
6or de%ete the reords7 in the hi%d tab%es aording%"< 67 set the ke" !a%1e in
the hi%d tab%es to .>LL.
-ost RD9-S an be set1) to )erform the hek and ens1re the referentia%
integrit"( in the s)ei@ed manner.
!hysical "rchitecture

0 The transation %og is 1sed to g1arantee the data integrit" of the database and
for data reo!er".
0 The transation %og in a database ma)s o!er one or more )h"sia% @%es.
;one)t1a%%"( the %og @%e is a string of %og reords. ,h"sia%%"( the se81ene of %og
reords is stored eAient%" in the set of )h"sia% @%es that im)%ement the
transation %og.
0 The SQL Ser!er Database ?ngine di!ides eah )h"sia% %og @%e interna%%" into a
n1mber of !irt1a% %og @%es. Birt1a% %og @%es ha!e no @4ed si/e( and there is no @4ed
n1mber of !irt1a% %og @%es for a )h"sia% %og @%e. The Database ?ngine hooses the
si/e of the !irt1a% %og @%es d"namia%%" whi%e it is reating or e4tending %og @%es. The
si/e or n1mber of !irt1a% %og @%es annot be on@g1red or set b" administrators.
0 The on%" time !irt1a% %og @%es aCet s"stem )erformane is if the %og @%es are
de@ned b" sma%% size and growth_increment !a%1es. 3f these %og @%es grow to a %arge
si/e bea1se of man" sma%% inrements( the" wi%% ha!e %ots of !irt1a% %og @%es. This
an s%ow down database start1) and a%so %og bak1) and restore o)erations. De
reommend that "o1 assign %og @%es a size !a%1e %ose to the @na% si/e re81ired( and
a%so ha!e a re%ati!e%" %arge growth_increment !a%1e.
0 The transation %og is a wra)0aro1nd @%e. 2or e4am)%e( onsider a database with
one )h"sia% %og @%e di!ided into fo1r !irt1a% %og @%es. Dhen the database is reated(
the %ogia% %og @%e begins at the start of the )h"sia% %og @%e. .ew %og reords are
added at the end of the %ogia% %og and e4)and toward the end of the )h"sia% %og.
Log tr1nation frees an" !irt1a% %ogs whose reords a%% a))ear in front of the
minim1m reo!er" %og se81ene n1mber 6-inLS.7. The MinLSN is the %og se81ene
n1mber of the o%dest %og reord that is re81ired for a s1essf1% database0wide
ro%%bak.
#hec$%oints and the "cti&e !ortion of the 'og

0 ;hek)oints E1sh dirt" data )ages from the b1Cer ahe of the 1rrent database
to disk. This minimi/es the ati!e )ortion of the %og that m1st be )roessed d1ring a
f1%% reo!er" of a database. D1ring a f1%% reo!er"( the fo%%owing t")es of ations are
)erformed:
The %og reords of modi@ations not E1shed to disk before the s"stem
sto))ed are ro%%ed forward.
A%% modi@ations assoiated with inom)%ete transations( s1h as
transations for whih there is no ;5--3T or R5LL9A;F %og reord( are ro%%ed
bak.
#hec$%oint (%eration
A hek)oint )erforms the fo%%owing )roesses in the database:
Drites a reord to the %og @%e( marking the start of the hek)oint.
Stores information reorded for the hek)oint in a hain of hek)oint %og
reords.
5ne )iee of information reorded in the hek)oint is the %og se81ene
n1mber 6LS.7 of the @rst %og reord that m1st be )resent for a s1essf1%
database0wide ro%%bak. This LS. is a%%ed the -inim1m Reo!er" LS.
6-inLS.7.
3f the database 1ses the sim)%e reo!er" mode%( marks for re1se the s)ae
that )reedes the -inLS..
Drites a%% dirt" %og and data )ages to disk.
Drites a reord marking the end of the hek)oint to the %og @%e.
Drites the LS. of the start of this hain to the database boot )age.
The hek)oint reords a%so ontain a %ist of a%% the ati!e transations that ha!e
modi@ed the database.
"cti&ities That #ause a #hec$%oint
;hek)oints o1r in the fo%%owing sit1ations:
A ;G?;F,53.T statement is e4)%iit%" e4e1ted. A hek)oint o1rs in the
1rrent database for the onnetion.
A minima%%" %ogged o)eration is )erformed in the database< for e4am)%e( a
b1%k0o)" o)eration is )erformed on a database that is 1sing the 91%k0Logged
reo!er" mode%.
Database @%es ha!e been added or remo!ed b" 1sing ALT?R DATA9AS?.
An instane of SQL Ser!er is sto))ed b" a SG>TD5D. statement or b"
sto))ing the SQL Ser!er 6-SSQLS?RB?R7 ser!ie. ?ither ation a1ses a
hek)oint in eah database in the instane of SQL Ser!er.
An instane of SQL Ser!er )eriodia%%" generates a1tomati hek)oints in
eah database to red1e the time that the instane wo1%d take to reo!er the
database.
A database bak1) is taken.
An ati!it" re81iring a database sh1tdown is )erformed. 2or e4am)%e(
A>T5H;L5S? is 5. and the %ast 1ser onnetion to the database is %osed( or
a database o)tion hange is made that re81ires a restart of the database.
)rite"head Transaction 'og

0 SQL Ser!er 1ses a write0ahead %og 6DAL7( whih g1arantees that no data
modi@ations are written to disk before the assoiated %og reord is written to disk.
This maintains the A;3D )ro)erties for a transation.
0 SQL Ser!er maintains a b1Cer ahe into whih it reads data )ages when data
m1st be retrie!ed. Data modi@ations are not made diret%" to disk( b1t are made to
the o)" of the )age in the b1Cer ahe. The modi@ation is not written to disk 1nti%
a hek)oint o1rs in the database( or the modi@ation m1st be written to disk so
the b1Cer an be 1sed to ho%d a new )age. Driting a modi@ed data )age from the
b1Cer ahe to disk is a%%ed E1shing the )age. A )age modi@ed in the ahe( b1t
not "et written to disk( is a%%ed a dirty page.
0 At the time a modi@ation is made to a )age in the b1Cer( a %og reord is b1i%t in
the %og ahe that reords the modi@ation. This %og reord m1st be written to disk
before the assoiated dirt" )age is E1shed from the b1Cer ahe to disk. 3f the dirt"
)age is E1shed before the %og reord is written( the dirt" )age reates a
modi@ation on the disk that annot be ro%%ed bak if the ser!er fai%s before the %og
reord is written to disk. SQL Ser!er has %ogi that )re!ents a dirt" )age from being
E1shed before the assoiated %og reord is written. Log reords are written to disk
when the transations are ommitted.
'og File "rchitecture in S*' Ser&er
Whenever any query is processed, the data will be passed to Data file. Below is the process how
a query is processed in SQL Server/ Iportance of Lo! "ile #rchitecture$
Database has Data @%e and Log @%e.
The 81er" statement is )assed to the b1Cer ahe and %og ahe.
The data in the b1Cer ahe is a%%ed as Dirt" Data or Dirt" 9%oks.
91Cer ahe ontains the dirt" data 6the 1)dated data orres)onding to the
81er" gi!en in the a))%iation7.
The )roess of writing the data from b1Cer ahe to the data @%es of the
database in the form of h1nks is
a%%ed as ;hek)oint ,roess.
?ah h1nk ontains '1# F9.
The 81er" is written into the %og @%e from the %og ahe.
3f an" t")e of fai%1re o1rs whi%e writing data to the data @%e( then the 81er"
in the %og @%e is e4e1ted at the %ast ommit transation )roessed 6refer
ommit )roess down7 and the remaining data is written to the database
whene!er we start the ser!er.
This )roess of writing data to the database after a fai%1re from the %og @%e is
a%%ed as Reo!er".
,roed1re ;ahe ontains the e4e1tion )%an.
;onte4t ;ahe ontains the data of the stored )roed1re.
Ser!er Le!e% Data Str1t1re ontains the Ser!er %e!e% information.
Commit Process:
o As soon as ommit statement is written to the %og @%e it throws a token
to the 1ser that ommit is om)%eted s1essf1%%" 6?4. 1 row aCeted7(
this )roess is a%%ed as ;ommit ,roess.
Ty%es of isolation le&els in S*' Ser&er
Read ;ommitted
SQL Ser!er a81ires a share %ok whi%e reading a row into a 1rsor b1t frees
the %ok immediate%" after reading the row. 9ea1se shared %ok re81ests are
b%oked b" an e4%1si!e %ok( a 1rsor is )re!ented from reading a row that
another task has 1)dated b1t not "et ommitted. Read ommitted is the
defa1%t iso%ation %e!e% setting for both SQL Ser!er and 5D9;.
Read >nommitted
SQL Ser!er re81ests no %oks whi%e reading a row into a 1rsor and honors no
e4%1si!e %oks. ;1rsors an be )o)1%ated with !a%1es that ha!e a%read" been
1)dated b1t not "et ommitted. The 1ser is b")assing a%% of the %oking
transation ontro% mehanisms in SQL Ser!er.
Re)eatab%e Read
SQL Ser!er re81ests a shared %ok on eah row as it is read into the 1rsor as
in R?AD ;5--3TT?D( b1t if the 1rsor is o)ened within a transation( the
shared %oks are he%d 1nti% the end of the transation instead of being freed
after the row is read. So )hantom rows are This has the same eCet as
s)eif"ing G5LDL5;F on a S?L?;T statement.
(!hantom read+,hantom reads o1rs when an insert or de%ete ation is
)erformed against a row that is being read b" a transation.The seond
transation read shows a row that did not e4ist in the origina% read as the
res1%t of an insertion b" a diCerent transation or d1e to de%etion o)eration
some rows doesnIt a))ear7
Seria%i/ab%e
3n seria%i/ab%e read )hantom reads are not a%%owed bea1se whi%e the @rst
transation is in )rogress other transation wont e4e1te.
Sna)shot
SQL Ser!er re81ests no %oks whi%e reading a row into a 1rsor and honors no
e4%1si!e %oks. ;1rsor is )o)1%ated with the !a%1es as of the time when the
transation @rst started. Sro%% %oks are sti%% re81ested regard%ess of 1se of
sna)shot iso%ation.
Read uncommitted e,am%le+
>nommitted Read a%%ows "o1r transation to read an" data that is 1rrent%"
on a data )age( whether that has been ommitted or not. 2or
e4am)%e(another 1ser might ha!e a transation in )rogress that has 1)dated
data( and e!en tho1gh itIs ho%ding e4%1si!e %oks on the data( "o1r
transation an read it an"wa".

3f we e4e1te the se%et 81er" before the 1)date transation gets


ommitted(it wi%% not wait for the 1)date transation to ommits.Q1er"
wi%% be e4e1ted immediate%" witho1t an" time %a)se.

Read ;ommitted e4am)%e


Read ommitted a%%ows "o1r transation to read on%" if the data is
ommitted.Read ;ommitted o)eration ne!er reads data that another
a))%iation has hanged b1t not "et ommitted.

3f we e4e1te the se%et 81er" before the 1)date transation gets


ommitted(it wi%% wait ti%% the 1)date transation gets ommitted.

Repeatable Read example :


3n Re)eatab%e Read iss1ing the same 81er" twie within a transation wi%% not
make an" hanges to data !a%1es made b" another 1serIs
transation.Re)eatab%e Read a%%ows )hantom reads6Data getting hanged in
1rrent transation b" other transations is a%%ed ,hantom Reads7.So
)hantom rows wi%% a))ear.

Dhi%e the transation6@rst 81er"7 is in )rogress(re)eatab%e read a%%ows


another transation6seond 81er"7 to e4e1te.3t means it a%%ow
)hantom reads.So seond transation6seond 81er"7(need not wait ti%%
@rst transation6@rst 81er"7 om)%etes.Gere !a%1es wi%% be added
before @rst 81er" om)%etes.

Seriali-a.le e,am%le + The Seria%i/ab%e iso%ation %e!e% adds to the


)ro)erties of Re)eatab%e Read b" ens1ring that if a 81er" is reiss1ed(
rows wi%% not ha!e been added in the tab%e. 3n other words( )hantoms
wi%% not a))ear if the same 81er" is iss1ed twie within a transation.

Dhi%e the transation6@rst 81er"7 is in )rogress(seria%i/ab%e read does


not a%%ow another transation6seond 81er"7(3t means it donIt a%%ow
)hantom reads.So seond transation6seond 81er"7( m1st wait ti%% @rst
transation6@rst 81er"7 om)%etes.Gere !a%1es wi%% be added on%" after
@rst 81er" om)%etes.

sna%shot e,am%le + To 1se the sna)shot iso%ation %e!e% "o1 need to


enab%e it on the database b" r1nning the fo%%owing ommand
ALT?R DATA9AS? D9name
S?T ALL5DHS.A,SG5TH3S5LAT35. 5.
)hat is loc$ escalation/
Lok esa%ation is the )roess of on!erting a %ot of %ow %e!e% %oks 6%ike row
%oks( )age %oks7 into higher %e!e% %oks 6%ike tab%e %oks7. ?!er" %ok is a
memor" str1t1re too man" %oks wo1%d mean( more memor" being
o1)ied b" %oks. To )re!ent this from ha))ening( SQL Ser!er esa%ates the
man" @ne0grain %oks to fewer oarse0grain %oks. Lok esa%ation thresho%d
was de@nab%e in SQL Ser!er J.'( b1t from SQL Ser!er +.* onwards itIs
d"namia%%" managed b" SQL Ser!er.
-S SQL Ser!er arhitet1re.
The maKor om)onents of SQL Ser!er are:
1. Re%ationa% ?ngine
#. Storage ?ngine
$. SQL 5S
.ow we wi%% dis1ss and 1nderstand eah one of them.
17 Re%ationa% ?ngine: A%so a%%ed as the 81er" )roessor( Re%ationa% ?ngine
in%1des the om)onents of SQL Ser!er that determine what "o1r 81er"
e4at%" needs to do and the best wa" to do it. 3t manages the e4e1tion of
81eries as it re81ests data from the storage engine and )roesses the res1%ts
ret1rned.
DiCerent Tasks of Re%ationa% ?ngine:
1. Q1er" ,roessing
#. -emor" -anagement
$. Thread and Task -anagement
&. 91Cer -anagement
'. Distrib1ted Q1er" ,roessing
#7 Storage ?ngine: Storage ?ngine is res)onsib%e for storage and retrie!a% of
the data on to the storage s"stem 6Disk( SA. et.7. to 1nderstand more( %etIs
fo1s on the one)ts.
Dhen we ta%k abo1t an" database in SQL ser!er( there are # t")es of @%es
that are reated at the disk %e!e% L Data fle and Log fle. Data @%e )h"sia%%"
stores the data in data )ages. Log @%es that are a%so known as write ahead
%ogs( are 1sed for storing transations )erformed on the database.
LetIs 1nderstand data @%e and %og @%e in more detai%s:
0ata File: Data File stores data in the form of Data Page 6MF97 and these
data )ages are %ogia%%" organi/ed in e4tents.
E,tents: ?4tents are %ogia% 1nits in the database. The" are a ombination of
M data )ages i.e. J& F9 forms an e4tent. ?4tents an be of two t")es( -i4ed
and >niform. -i4ed e4tents ho%d diCerent t")es of )ages %ike inde4( s"stem(
data et 6m1%ti)%e obKets7. 5n the other hand( >niform e4tents are dediated
to on%" one t")e 6obKet7.
!ages: As we sho1%d know what t")e of data )ages an be stored in SQL
Ser!er( be%ow mentioned are some of them:
Data ,age: 3t ho%ds the data entered b" the 1ser b1t not the data whih
is of t")e te4t( nte4t( n!arhar6ma47( !arhar6ma47( !arbinar"6ma47(
image and 4m% data.
3nde4: 3t stores the inde4 entries.
Te4t:3mage: 3t stores L59 6 Large 5bKet data7 %ike
te4t( nte4t( !arhar6ma47( n!arhar6ma47( !arbinar"6ma47(
image and 4m% data.
GA- N SGA- 6G%oba% A%%oation -a) N Shared G%oba% A%%oation -a)7:
The" are 1sed for sa!ing information re%ated to the a%%oation of
e4tents.
,2S 6,age 2ree S)ae7: 3nformation re%ated to )age a%%oation and
1n1sed s)ae a!ai%ab%e on )ages.
3A- 63nde4 A%%oation -a)7: 3nformation )ertaining to e4tents that are
1sed b" a tab%e or inde4 )er a%%oation 1nit.
9;- 691%k ;hanged -a)7: Fee)s information abo1t the e4tents
hanged in a 91%k 5)eration.
D;- 6DiCerentia% ;hange -a)7: This is the information of e4tents that
ha!e modi@ed sine the %ast 9A;F>, DATA9AS? statement as )er
a%%oation 1nit.
'og File: 3t a%so known as write ahead %og. 3t stores modi@ation to the
database 6D-L and DDL7.
S1Aient information is %ogged to be ab%e to:
o Ro%% bak transations if re81ested
o Reo!er the database in ase of fai%1re
o Drite Ahead Logging is 1sed to reate %og entries
Transation %ogs are written in hrono%ogia% order in a
ir1%ar wa"
Tr1nation )o%i" for %ogs is based on the reo!er" mode%
S*' (S: This %ies between the host mahine 6Dindows 5S7 and SQL Ser!er.
A%% the ati!ities )erformed on database engine are taken are of b" SQL 5S.
3t is a high%" on@g1rab%e o)erating s"stem with )owerf1% A,3 6a))%iation
)rogramming interfae7( enab%ing a1tomati %oa%it" and ad!aned
)ara%%e%ism. SQL 5S )ro!ides !ario1s o)erating s"stem ser!ies( s1h as
memor" management dea%s with b1Cer )oo%( %og b1Cer and dead%ok
detetion 1sing the b%oking and %oking str1t1re. 5ther ser!ies in%1de
e4e)tion hand%ing( hosting for e4terna% om)onents %ike ;ommon Lang1age
R1ntime( ;LR et.
,ages and ?4tents
*1 )hat is a %age in data.ase systems and tell me something a.out
%ages/
"ns+
The f1ndamenta% 1nit of data storage in SQL Ser!er is the )age.
,age si/e is Mkb means 1#M )ages = 1 -9.
,age starts with the header of OJ b"tes that is 1sed to store )age n1mber(
)age t")e( the amo1nt of free s)ae on the )age( and the obKet id that
owns the )age. The ma4im1m si/e of a sing%e row on a )age is M*J* b"tes.
91t this restrition is re%a4ed for tab%es whih are ha!ing !arhar( n!arhar(
Barbinar"( Ta4t or 3mage o%1mns.
*1 "ny idea 2hat is 3R()4(5ERF'()40"T" allocation unit6/
"ns+
Dhen the tota% row si/e of a%% @4ed and !ariab%e o%1mns in a tab%e e4eeds
the M(*J* b"te %imitation( SQL Ser!er d"namia%%" mo!es one or more
!ariab%e %ength o%1mns to )ages in the R5DH5B?R2L5DHDATA a%%oation
1nit.
This is done whene!er an insert or 1)date o)eration inreases the tota% si/e
of the row be"ond the M*J* b"te %imit. Dhen a o%1mn is mo!ed to a )age in
the R5DH5B?R2L5DHDATA a%%oation 1nit( a #&0b"te )ointer on the origina%
)age in the 3.HR5DHDATA a%%oation 1nit is maintained. 3f a s1bse81ent
o)eration red1es the row si/e( SQL Ser!er d"namia%%" mo!es the o%1mns
bak to the origina% data )age.
*1 #an you .rie7y descri.e a.out E8TENT/
"ns+
An e4tent is eight )h"sia%%" ontig1o1s )ages( or J& F9 means 1J ?4tents=
1 -9.
There are two t")es of ?4tents. >niform ?4tents and -i4ed ?4tents.
>niform e4tents are owned b" a sing%e obKet<
-i4ed e4tents are shared b" 1) to eight obKets
*1 )hat are the di9erent Ty%es of !ages a&aila.le/
"ns+
:"; and S:"; (:lo.al "llocation ;a% < Shared :";)+
:";+ ?4tents ha!e been a%%oated: 1 L 2ree s)ae * L .o s)ae
S:";+ -i4ed ?4tents ha!e been a%%oated: 1 L 2ree S)ae P -i4ed ?4tent
and * L .o s)ae
?ah GA- : SGA- o!ers J&*** e4tents L & G9
!FS (!age Free S%ace)+ ,erentage of free s)ae a!ai%ab%e in eah )age in
an e4tent.
0#; (0i9erential #hanged ;a%)+ This traks the e4tents that ha!e
hanged sine the %ast 9A;F>, DATA9AS? statement. 1 L -odi@ed( * L .ot
modi@ed
=#; (=ul$ #hanged ;a%)+ This traks the e4tents that ha!e been
modi@ed b" b1%k %ogged o)erations sine the %ast 9A;F>, L5G statement. 1
L -odi@ed( * L .ot modi@ed 6>sed on%" in b1%k %ogged Reo!er" mode%7
3n eah data @%e )ages are arranged %ike be%ow
A%ong with that we ha!e three diCerent data )ages
0ata
Inde,
Te,t> Image ('(=? R()4(5ERF'(E? 8;')

2i%es and 2i%e Gro1)s
*1 Ha&e you e&er heard the 2ord 3Files6 or 3File :rou%s6 in S*'
Ser&er/
"ns+
Files+ There are three t")es( ,rimar"0 .mdf( Seondar" L .ndf and Log @%es
L .%df
File :rou%s+ There are two( ,rimar" 2i%e Gro1) L A%% s"stem tab%es and >ser
De@ned L De)ends
A%% seondar" @%es and 1ser de@ned @%e gro1)s are reated to o)timi/e the
data aess and for )artitioning the tab%es.
*1 )hat are the ad&antages and disad&antages o&er using
@legrou%s/
"ns+
"d&antages+
>sing @%egro1)s( "o1 an e4)%iit%" )%ae database obKets into a
)arti1%ar set of database @%es. 2or e4am)%e( "o1 an se)arate tab%es
and their non%1stered inde4es into se)arate @%egro1)s. This an
im)ro!e )erformane( bea1se modi@ations to the tab%e an be
written to both the tab%e and the inde4 at the same time. This an be
es)eia%%" 1sef1% if "o1 are not 1sing stri)ing with )arit" 6RA3D0'7.
Another ad!antage of @%egro1)s is the abi%it" to bak 1) on%" a sing%e
@%egro1) at a time. This an be e4treme%" 1sef1% for a BLD9( bea1se
the sheer si/e of the database o1%d make baking 1) an e4treme%"
time0ons1ming )roess.
=et another ad!antage is the abi%it" to mark the @%egro1) and a%% data
in the @%es that are )art of it as either read0on%" or read0write.
0isad&antages
The @rst is the administration that is in!o%!ed in kee)ing trak of the
@%es in the @%egro1) and the database obKets that are )%aed in them.
The other is that if "o1 are working with a sma%%er database and ha!e
RA3D0' im)%emented( "o1 ma" not be im)ro!ing )erformane.
Transation %og Arhitet1re
*1 #an you tal$ a.out 3Transactionlog6 'ogical and !hysical
architecture/
"ns+
3Transactionlog6 'ogical "rchitecture+
?ah %og reord is identi@ed b" a 1ni81e n1mber a%%ed LS. 6Log Se81ene
.1mber7. A %og reord ontains the LS.( Transation3D to whih it be%ongs
and data modi@ation reord.
Data modi@ation reord: 3tIs either o)eration )erformed or before and after
data image
Dhen reorded Q5)eration ,erformedR
Transation ommitted L Logia% 5)eration is )ermanent%" a))%ied to
the data
Transation ro%%bak L Re!erse Logia% 5)eration is a))%ied to the data.
Dhen Reorded Q9efore and After Data 3mageR
Transation ommitted L A))%ied the after transation image
Transation ro%%bak L A))%ied the before transation image
3Transactionlog6 !hysical "rchitecture+
The transation %og is 1sed to g1arantee the data integrit" of the database
and for data reo!er".
The SQL Ser!er Database ?ngine di!ides eah )h"sia% %og @%e interna%%" into
a n1mber of !irt1a% %og @%es. Birt1a% %og @%es ha!e no @4ed si/e( and there is
no @4ed n1mber of !irt1a% %og @%es for a )h"sia% %og @%e.
The on%" time !irt1a% %og @%es aCet s"stem )erformane is if the %og @%es are
de@ned b" sma%% si/e and growthHinrement !a%1es. 3f these %og @%es grow to
a %arge si/e bea1se of man" sma%% inrements( the" wi%% ha!e %ots of !irt1a%
%og @%es. This an s%ow down database start1) and a%so %og bak1) and
restore o)erations.
*1 )hat are the #hec$!oints in S*' Ser&er data.ase/
"ns+
;hek)oints E1sh dirt" data )ages from the b1Cer ahe of the 1rrent
database to disk. This minimi/es the ati!e )ortion of the %og that m1st be
)roessed d1ring a f1%% reo!er" of a database. D1ring a f1%% reo!er"( the
fo%%owing t")es of ations are )erformed:
The %og reords of modi@ations not E1shed to disk before the s"stem
sto))ed are ro%%ed forward.
A%% modi@ations assoiated with inom)%ete transations( s1h as
transations for whih there is no ;5--3T or R5LL9A;F %og reord( are
ro%%ed bak.
9efore a database bak1)( the Database ?ngine a1tomatia%%" )erforms a
hek)oint so that a%% hanges to the database )ages are ontained in the
bak1). A%so( sto))ing a ser!er iss1es a hek)oint in eah database on the
ser!er.
*1 )hat is an "cti&e 'og/
"ns+
The setion of the %og @%e from the -inLS. to the %ast0written %og reord is
a%%ed the ati!e )ortion of the %og( or the ati!e %og. This is the setion of the
%og re81ired to do a f1%% reo!er" of the database. .o )art of the ati!e %og
an e!er be tr1nated. A%% %og reords m1st be tr1nated from the )arts of
the %og before the -inLS..
*1 )hat is 3)rite "head Transaction 'og6/
"ns+
SQL Ser!er 1ses a write0ahead %og 6DAL7( whih g1arantees that no data
modi@ations are written to disk before the assoiated %og reord is written to
disk.
Data modi@ations are not made diret%" to disk( b1t are made to the o)" of
the )age in the b1Cer ahe. The modi@ation is not written to disk 1nti% a
hek)oint o1rs in the database. A )age modi@ed in the ahe( b1t not "et
written to disk( is a%%ed a dirt" )age. The interna% )roess that at1a%%" goes
on:
1. ;o)" of the data )ages are )1%%ed and )%aed in b1Cer ahe
#. A))%ied the o)eration on the )ages that are on b1Cer ahe
$. Drite the %og reord detai%s 6,ages modi@ed7 to Disk
&. Drite : E1sh :a))%" the )age to the disk
3f ste) & ha))ens before the ste) $ then ro%%bak is im)ossib%e. SQL Ser!er
takes the res)onsibi%it" of writing the %og detai%s to disk before E1shing the
dirt" )ages.
-emor" -anagement Arhitet1re
*1 #an you descri.e S*' Ser&er ;emory "rchitecture/
"ns+
SQL Ser!er d"namia%%" a81ires and frees memor" as re81ired. T")ia%%"( an
administrator need not ha!e to s)eif" how m1h memor" sho1%d be
a%%oated to SQL Ser!er( a%tho1gh the o)tion sti%% e4ists and is re81ired in
some en!ironments.
SQL Ser!er s1))orts Address Dindowing ?4tensions 6AD?7 a%%owing 1se of
)h"sia% memor" o!er & gigab"tes 6G97 on $#0bit !ersions of -irosoft
Dindows o)erating s"stems. This feat1re is de)reated from Dina%i #*1#.
SQL Ser!er tries to reah a ba%ane between two goa%s:
Fee) the b1Cer )oo% from beoming so big that the entire s"stem is
%ow on memor".
-inimi/e )h"sia% 3:5 to the database @%es b" ma4imi/ing the si/e of
the b1Cer )oo%.
*1 0o you ha&e any idea a.out =u9er ;anagement/
"ns+
A b1Cer is a Mkb si/e in memor". To red1e the 3:5 o)erations from database
to disk b1Cer manager 1se the b1Cer ahe. 9- gets the data from database
to b1Cer ahe and modi@es the data and the modi@ed )age is sent bak to
the disk
The b1Cer manager on%" )erforms reads and writes to the database. 5ther
@%e and database o)erations s1h as o)en( %ose( e4tend( and shrink are
)erformed b" the database manager and @%e manager om)onents.
*1 E,%lain e9ects of ;in and ;a, memory con@guration o%tions
"ns+
The min ser!er memor" and ma4 ser!er memor" on@g1ration o)tions
estab%ish 1))er and %ower %imits to the amo1nt of memor" 1sed b" the b1Cer
)oo% of the -irosoft SQL Ser!er Database ?ngine. The b1Cer )oo% starts with
on%" the memor" re81ired to initia%i/e. As the Database ?ngine work%oad
inreases( it kee)s a81iring the memor" re81ired to s1))ort the work%oad.
The b1Cer )oo% does not free an" of the a81ired memor" 1nti% it reahes the
amo1nt s)ei@ed in min ser!er memor". 5ne min ser!er memor" is
reahed( the b1Cer )oo% then 1ses the standard a%gorithm to a81ire and free
memor" as needed. The on%" diCerene is that the b1Cer )oo% ne!er dro)s its
memor" a%%oation be%ow the %e!e% s)ei@ed in min ser!er memor"( and
ne!er a81ires more memor" than the %e!e% s)ei@ed in ma4 ser!er memor".
Thread and Task Arhitet1re
*1 #an you descri.e ho2 S*' Ser&er handles =atch or Tas$
Scheduling/
"ns+
?ah instane m1st hand%e )otentia%%" tho1sands of on1rrent re81ests from
1sers. 3nstanes of SQL Ser!er 1se -irosoft Dindows threads( or if
on@g1red( the" 1se @bers( to manage these on1rrent tasks eAient%". This
in%1des one or more threads for eah ser!er .et0Librar"( a network thread
to hand%e network 3:5( and a signa% thread for omm1niating with the
Ser!ie ;ontro% -anager.
Anderstanding Scheduling+ ?ah instane of SQL Ser!er has an interna%
%a"er 6SQL 5S:Ferne%7 that im)%ements an en!ironment simi%ar to an
o)erating s"stem. This interna% %a"er is 1sed for shed1%ing and
s"nhroni/ing on1rrent tasks witho1t ha!ing to a%% the Dindows kerne%.
#onnection+ A onnetion is estab%ished when the 1ser is s1essf1%%"
%ogged in. The 1ser an then s1bmit one or more Transat0SQL statements
for e4e1tion. A onnetion is %osed when the 1ser e4)%iit%" %ogs o1t( or the
onnetion is terminated.
=atch+ An SQL bath is a set of one or more Transat0SQL statements sent
from a %ient to an instane of SQL Ser!er for e4e1tion.
Tas$+ A task re)resents a 1nit of work that is shed1%ed b" SQL Ser!er. A
bath an ma) to one or more tasks.
)indo2s thread+ ?ah Dindows thread re)resents an inde)endent
e4e1tion mehanism.
Fi.er+ A @ber is a %ightweight thread that re81ires fewer reso1res than a
Dindows thread. 5ne Dindows thread an be ma))ed to man" @bers.
)or$er thread+ The worker thread re)resents a %ogia% thread 6Task7 in SQL
Ser!er that is interna%%" ma))ed 61:17 to either a Dindows thread or( if
%ightweight )oo%ing is t1rned 5.( to a @ber. The ma))ing an be done ti%% the
free worker threads a!ai%ab%e. 6,arameter: -a4 worker Threads7
Thread and Fi.er E,ecution+ -irosoft Dindows 1ses a n1meri )riorit"
s"stem that ranges from 1 thro1gh $1 to shed1%e threads for e4e1tion.
Sero is reser!ed for o)erating s"stem 1se. Dhen se!era% threads are waiting
to e4e1te( Dindows dis)athes the thread with the highest )riorit".
9" defa1%t( eah instane of SQL Ser!er is a )riorit" of +( whih is referred to
as the norma% )riorit". The )riorit" boost on@g1ration o)tion an be 1sed to
inrease the )riorit" of the threads from an instane of SQL Ser!er to 1$. This
is referred to as high )riorit".
The )erformane of an" instanes r1nning at norma% )riorit" an be
ad!erse%" aCeted. A%so( the )erformane of other a))%iations and
om)onents on the ser!er an de%ine if )riorit" boost is t1rned on.
Q1er" ,roessing Arhitet1re
*1 I ha&e su.mitted a *uery to S*' Ser&er from an "%%lication and I
got the re%ly as 3data inserted successfully61 #an you demonstrate
2hat the %rocessing done inside/
"ns+
Dhen "o1 s1bmit a 81er" to a SQL Ser!er database( a n1mber of )roesses
on the ser!er go to work on that 81er". The )1r)ose of a%% these )roesses is
to manage the s"stem s1h that it wi%% )ro!ide "o1r data bak to "o1( or
store it( in as time%" a manner as )ossib%e( whi%st maintaining the integrit" of
the data.
A%% these )roesses go thro1gh two stages:
1. Re%ationa% ?ngine
#. Storage ?ngine
"t #lient+
1. >ser enter data and %ik on s1bmit
#. The %ient database %ibrar" transforms the origina% re81est into a se81ene
of one or more Transat0SQL statements to be sent to SQL Ser!er. These
statements are ena)s1%ated in one or more Tab1%ar Data Stream 6TDS7
)akets and )assed to the database network %ibrar"
$. The database network %ibrar" 1ses the network %ibrar" a!ai%ab%e in the
%ient om)1ter to re)akage the TDS )akets as network )rotoo% )akets.
&. The network )rotoo% )akets are sent to the ser!er om)1ter network
%ibrar" aross the network
At Ser!er:
'. The e4trated TDS )akets are sent to 5)en Data Ser!ies 65DS7( where
the origina% 81er" is e4trated.
J. 5DS sends the 81er" to the re%ationa% engine
+. A onnetion estab%ished to the re%ationa% engine and assign a S3D to the
onnetion
"t Relational Engine+
M. ;hek )ermissions and determines if the 81er" an be e4e1ted b" the
1ser assoiated with the re81est
O. Q1er" sends to Q1er" ,arser
3t heks that the T0SQL is written orret%"
91i%d a ,arse Tree \ Se81ene Tree
1*. ,arse Tree sends to A%gebri/er
Beri@es a%% the o%1mns( obKets and data t")es
Aggregate 9inding 6determines the %oation of aggregates s1h as
GR5>, 9=( and -AT7
91i%ds a Q1er" ,roessor Tree in 9inar" 2ormat
11. Q1er" ,roessor Tree sends to 5)timi/er
9ased on the 81er" )roessor tree and Gistogram 6Statistis7 b1i%ds an
o)timi/ed e4e1tion )%an
Stores the e4e1tion )%an into ahe and send it to the database
engine
"t 0ata.ase Engine+
1#. Database engine ma) a bath into diCerent tasks
1$. ?ah task assoiated with a )roess
1&. ?ah )roess assigned with a Dindows Thread or a Dindows 2iber. The
worker thread takes are of this.
1'. The Thread:2iber send to the e4e1tion 81e1e and wait for the ;,> time.
1J. The Thread:2iber identi@es the tab%e %oation where the data need to be
stored
1+. Go to the @%e header( heks the ,2S( GA- and GSA- and go to the
orret )age
1M. Beri@es the )age is not orr1)ted 1sing Torn )age Detetion : ;hek S>-
and writes the data
1O. 3f re81ire a%%oates new )ages and stores data on it. 5ne the data is
stored:1)dated:added in a )age( it 1)dates the be%ow %oations
,2S
,age Geader L ;heks1m : Torn ,age Detetion 6Setor info7
9;-
D;-
#*. 3n this )roess the
-emor" manager take are of a%%oating b1Cers( new )ages et(
Lok manager take are of a%%oating a))ro)riate %oks on the
obKets:)ages and re%easing them when task om)%eted
Thread Shed1%er: shed1%es the threads for ;,> time
3:5 manager: ?stab%ish memor" b1s for read:write o)erations from
memor" to disk and !ie !ersa
Dead%ok\Reso1re\Shed1%er -onitor: -onitors the )roesses
#1. 5ne the )roess is om)%eted the res1%t set is s1bmitted to the
re%ationa% engine and fo%%ow the same )roess for sending bak the res1%t set
to %ient a))%iation.
##. The onnetion wi%% be %osed and the S3D is remo!ed
Majorly used DMVs are :
sys.dm_exec_connections - active user connections and internal tasks,
sys.dm_exec_requests - give details on !at eac! connection is actually
"er#orming in $%& $erver.
sys.dm_exec_cac!ed_"lans - query "lan cac!ed.
sys.dm_exec_query_stats - DMV !ic! returns aggregate "er#ormance statistics
di9erence .et2een SBl Ser&er 2CCD and 2CCE %atching
SQL Server 2005: install $ervice "ack on active node #irst. t!e setu" ill launc! t!e
installation on "assive nodes "arallely.
SQL Server 2008: install $' on "assive nodes, #ailover and make t!is active. no install t!e $' on ot!er
side.
Sus%ect !ages in data.ase
i# d(cc command takes long time to run t!en use msd(..sus"ect_"ages.
If database is hu!e in si%e Instead of restorin! whole database we can &ust restore corrupt pa!es
fro latest bac'up
RESTORE DATABASE yourDBNameHere
PAGE = 'fileid:pageid,fileid:pageid,et' !! e"g" #:$%%&,#:$%%$,et
'RO( D)S* = 'D:+S,-Ba.up/+yourD0NameHere1la/t'ull"BA*'
2)TH
NORE3O4ER5
GO
Note : Ru6 re/tore 7it8 6o reo9ery a6 t8e6 7e a6 re/tore all t!log 0a.up/
a6d of our/e la/t o6e 7it8 reo9ery
restore system data(ases:
!tt":))msdn.microso#t.com)en-us)li(rary)dd*+,++-.as"x .$%& *+/*0
#or $%& *+/*
ste"s:
/. insert t!e $%& server installation media or #rom command "rom"t go to t!e "at! t!at
!as setu" #iles.
(y de#ault t!e setu" #iles ill (e in t!e 1:2"rogram #iles2Microso#t $%& $erver2//+2setu"
(ootstra"2release
*. execute t!e (elo command in cmd "rom"t
setup /quiet /action = rebuilddatabase /instancename=<instance name>
/sqlsystemadminaccounts=<roup name> !/sap"d = <sa ps"d># ! /sqlcollation =
<collation name>#
"ost re(uild t task :
restore master, msd(, model d(
restore master d(:
ste"s -
/. start t!e instance in single user mode. !tt":))3are3.net)4"5//,
i,e - sqlservr.exe -c -m
*. t!en restore "ro"er master d( (acku".
di##t status o# "rocess:
runnin - "rocess running already in sc!eduler
runnable - "rocess in t!e runna(le queue t!at is aiting #or sc!eduler to allocate
suspended - "rocess aiting on resource
Status $eanin
6ackground 7!e $'8D is running a (ackground task, suc! as deadlock detection.
$lee"ing
7!e $'8D is not currently executing. 7!is usually indicates t!at t!e $'8D is aaiting
a command #rom t!e a""lication.
9unning 7!e $'8D is currently running on a sc!eduler.
9unna(le 7!e $'8D is in t!e runna(le queue o# a sc!eduler and aiting to get sc!eduler time.
$os_sc!eduler_yield
7!e $'8D as running, (ut it !as voluntarily yielded its time slice on t!e sc!eduler to
allo anot!er $'8D to acquire sc!eduler time.
$us"ended 7!e $'8D is aiting #or an event, suc! as a lock or a latc!.
9oll(ack 7!e $'8D is in roll(ack o# a transaction.
De#akeu"
8ndicates t!at t!e $'8D is aiting #or a resource t!at is in t!e "rocess o# (eing #reed.
7!e aitresource #ield s!ould indicate t!e resource in question.
1ommon DMV used -
sys.dm_exec_query_stat
sys.dm_exec_sql_text

You might also like