SQL Server Indexes Under-the-Hood · 2013-05-26 · PAGE HEADER (96 bytes) Row Offset 400 bytes/row...

30
Index Internals

Transcript of SQL Server Indexes Under-the-Hood · 2013-05-26 · PAGE HEADER (96 bytes) Row Offset 400 bytes/row...

Page 1: SQL Server Indexes Under-the-Hood · 2013-05-26 · PAGE HEADER (96 bytes) Row Offset 400 bytes/row 80,000 rows (1024 x 8) –96 bytes * *

Index Internals

Page 2: SQL Server Indexes Under-the-Hood · 2013-05-26 · PAGE HEADER (96 bytes) Row Offset 400 bytes/row 80,000 rows (1024 x 8) –96 bytes * *

http://[email protected]@EdwinMSarmientohttp://ca.linkedin.com/in/EdwinMSarmiento

Microsoft MVP - SQL Server

Page 3: SQL Server Indexes Under-the-Hood · 2013-05-26 · PAGE HEADER (96 bytes) Row Offset 400 bytes/row 80,000 rows (1024 x 8) –96 bytes * *

indexesHow do

work?

Page 4: SQL Server Indexes Under-the-Hood · 2013-05-26 · PAGE HEADER (96 bytes) Row Offset 400 bytes/row 80,000 rows (1024 x 8) –96 bytes * *

the better question to ask is

Page 5: SQL Server Indexes Under-the-Hood · 2013-05-26 · PAGE HEADER (96 bytes) Row Offset 400 bytes/row 80,000 rows (1024 x 8) –96 bytes * *

How is

?storeddata

Page 6: SQL Server Indexes Under-the-Hood · 2013-05-26 · PAGE HEADER (96 bytes) Row Offset 400 bytes/row 80,000 rows (1024 x 8) –96 bytes * *

PAGE HEADER (96 bytes)

Row Offset

Stored inpages

Page 7: SQL Server Indexes Under-the-Hood · 2013-05-26 · PAGE HEADER (96 bytes) Row Offset 400 bytes/row 80,000 rows (1024 x 8) –96 bytes * *

Row Offset

PAGE HEADER (96 bytes)

8 Pages = (64K)

Page 8: SQL Server Indexes Under-the-Hood · 2013-05-26 · PAGE HEADER (96 bytes) Row Offset 400 bytes/row 80,000 rows (1024 x 8) –96 bytes * *

Stored in either

or

(hobt)

Page 9: SQL Server Indexes Under-the-Hood · 2013-05-26 · PAGE HEADER (96 bytes) Row Offset 400 bytes/row 80,000 rows (1024 x 8) –96 bytes * *

unordered data

Page 10: SQL Server Indexes Under-the-Hood · 2013-05-26 · PAGE HEADER (96 bytes) Row Offset 400 bytes/row 80,000 rows (1024 x 8) –96 bytes * *
Page 11: SQL Server Indexes Under-the-Hood · 2013-05-26 · PAGE HEADER (96 bytes) Row Offset 400 bytes/row 80,000 rows (1024 x 8) –96 bytes * *

PAGE HEADER (96 bytes)

Row Offset

400 bytes/row80,000 rows

(1024 x 8) – 96 bytes

**

Page 12: SQL Server Indexes Under-the-Hood · 2013-05-26 · PAGE HEADER (96 bytes) Row Offset 400 bytes/row 80,000 rows (1024 x 8) –96 bytes * *

8096 bytes/page400 bytes/row

80,000 rows20 rows/page

=

=

Page 13: SQL Server Indexes Under-the-Hood · 2013-05-26 · PAGE HEADER (96 bytes) Row Offset 400 bytes/row 80,000 rows (1024 x 8) –96 bytes * *

finding rows in

Page 14: SQL Server Indexes Under-the-Hood · 2013-05-26 · PAGE HEADER (96 bytes) Row Offset 400 bytes/row 80,000 rows (1024 x 8) –96 bytes * *

IAM

HeapExtent 127 Extent 128 Extent 129

010203……

SmithOwenJones…...

………......

01020304…

AkersFunkSmithMartin...

………......

010203……

RussoWoodsBarr......

………......

010203……

ConFunkWoods......

………......

010203……

SmithOwenJones…...

………......

01020304…

AkersFunkSmithMartin...

………......

010203……

RussoWoodsBarr......

………......

0102030405

ConFunkWoodsDurrerLang

………......

010203……

SmithOwenJones…...

………......

01020304…

AkersFunkSmithMartin...

………......

010203……

RussoWoodsBarr......

………......

010203……

ConFunkWoods......

………......

010203……

SmithOwenJones…...

………......

01020304…

AkersFunkSmithMartin...

………......

010203……

RussoWoodsBarr......

………......

0102030405

DunnRandallOwenSleppyLaMee

………......

010203……

SmithOwenJones…...

………......

01020304…

AkersFunkSmithMartin...

………......

010203……

RussoWoodsBarr......

………......

010203……

ConFunkWoods......

………......

010203……

SmithOwenJones…...

………......

01020304…

AkersFunkSmithMartin...

………......

010203……

RussoWoodsBarr......

………......

010203……

SmithOwenJones…...

………......

01020304…

AkersFunkSmithMartin...

………......

010203……

RussoWoodsBarr......

………......

010203……

ConFunkWoods......

………......

010203……

SmithOwenJones…...

………......

01020304…

AkersFunkSmithMartin...

………......

010203……

RussoWoodsBarr......

………......

010203……

GraffBaconKoch......

………......

Extent 13001020304…

SeattleParisTokyoAtlanta...

………......

127 1

128 1

129 0

130 1

Extent Bit Map

Page 15: SQL Server Indexes Under-the-Hood · 2013-05-26 · PAGE HEADER (96 bytes) Row Offset 400 bytes/row 80,000 rows (1024 x 8) –96 bytes * *

ordered data

Page 16: SQL Server Indexes Under-the-Hood · 2013-05-26 · PAGE HEADER (96 bytes) Row Offset 400 bytes/row 80,000 rows (1024 x 8) –96 bytes * *

finding rows in

Page 17: SQL Server Indexes Under-the-Hood · 2013-05-26 · PAGE HEADER (96 bytes) Row Offset 400 bytes/row 80,000 rows (1024 x 8) –96 bytes * *

Page 140 - Root

Page 100 Page 120 Page 130

Page 141 Page 145

AkersBarrConFunkFunk...

23345678253413341534...

...

...

...

...

...

...

MartinMartinOwenPicaRusso...

12347778587878786078...

...

...

...

...

...

...

Martin

Page 110

Owen 5878 ...

SmithSmithSmithWoodsWoods...

14345778797822341634...

...

...

...

...

...

...

GanioHallJonesJonesJones...

76788078243459782634...

...

...

...

...

...

...

AkersGanio…

Smith…

Martin

Akers…MartinMartin

SELECT lastname, firstnameFROM memberWHERE lastname = 'Owen'

... ...

...

...

...

...

...

... ... ...

...

...

...

...

...

... ... ...

...

...

...

...

...

...... ...

...

...

...

...

...

...

Page 18: SQL Server Indexes Under-the-Hood · 2013-05-26 · PAGE HEADER (96 bytes) Row Offset 400 bytes/row 80,000 rows (1024 x 8) –96 bytes * *
Page 19: SQL Server Indexes Under-the-Hood · 2013-05-26 · PAGE HEADER (96 bytes) Row Offset 400 bytes/row 80,000 rows (1024 x 8) –96 bytes * *

How do I find rows

?non-clustered

in a heapwith a

index

Page 20: SQL Server Indexes Under-the-Hood · 2013-05-26 · PAGE HEADER (96 bytes) Row Offset 400 bytes/row 80,000 rows (1024 x 8) –96 bytes * *

NonClustered

Index

Non-LeafLevel

Page 12 - Root

Page 37 Page 28

Leaf Level(Key Value)

Page 41 Page 51 Page 61 Page 71

Akers

Barr

Con

Funk

Funk

4:706:01

4:705:03

4:704:01

4:706:02

4:704:02

Martin

Smith

...

Smith

Smith

Smith

Woods

Woods

4:706:03

4:708:04

4:707:01

4:704:03

4:705:02

Ganio

Hall

Jones

Jones

Jones

4:709:01

4:709:03

4:709:02

4:708:03

4:707:03

HeapPage 707 Page 708 Page 709

01

02

03

04

...

...

...

...

...

...

Akers

Funk

Smith

Mather

...

Page 704 Page 705 Page 706

01

02

03

...

...

...

...

...

...

...

Con

Funk

Woods

...

...

01

02

03

...

...

...

...

...

...

...

Russo

Woods

Barr

...

...

01

02

03

...

...

...

...

...

...

...

Smith

Owen

Jones

...

...

01

02

03

04

...

...

...

...

...

...

Martin

Pica

Jones

Smith

...

01

02

03

...

...

...

...

...

...

...

Ganio

Jones

Hall

...

...

Martin

Mather

Owen

Pica

Russo

4:708:01

4:706:04

4:707:02

4:708:02

4:705:01

Martin

02 ... Pica

Pica 4:708:02

01 ... Russo

02 ... Owen

Akers

Ganio

...

Akers

MartinMartin

Owen 4:707:02

04 ... Mather

Russo 4:705:01

Mather 4:706:04

SELECT lastname, firstname

FROM member

WHERE lastname

BETWEEN 'Masters' AND 'Russo'

NonClustered

Index

Non-LeafLevel

Page 12 - Root

Page 37 Page 28

Leaf Level(Key Value)

Page 41 Page 51 Page 61 Page 71

Akers

Barr

Con

Funk

Funk

4:706:01

4:705:03

4:704:01

4:706:02

4:704:02

Martin

Smith

...

Smith

Smith

Smith

Woods

Woods

4:706:03

4:708:04

4:707:01

4:704:03

4:705:02

Ganio

Hall

Jones

Jones

Jones

4:709:01

4:709:03

4:709:02

4:708:03

HeapPage 707 Page 708 Page 709

01

02

03

04

...

...

...

...

...

...

Akers

Funk

Smith

Mather

...

Page 704 Page 705 Page 706

01

02

03

...

...

...

...

...

...

...

Con

Funk

Woods

...

...

01

02

03

...

...

...

...

...

...

...

Russo

Woods

Barr

...

...

01

02

03

...

...

...

...

...

...

...

Smith

Owen

Jones

...

...

01

02

03

04

...

...

...

...

...

...

Martin

Pica

Jones

Smith

...

01

02

03

...

...

...

...

...

...

...

Ganio

Jones

Hall

...

...

Martin

Mather

Owen

Pica

Russo

4:708:01

4:706:04

4:707:02

4:708:02

4:705:01

Martin

02 ... Pica

Pica 4:708:02

01 ... Russo

02 ... Owen

Akers

Ganio

...

Akers

MartinMartin

Owen 4:707:02

04 ... Mather

Russo 4:705:01

Mather 4:706:04

Page 21: SQL Server Indexes Under-the-Hood · 2013-05-26 · PAGE HEADER (96 bytes) Row Offset 400 bytes/row 80,000 rows (1024 x 8) –96 bytes * *

Page 904 Page 4890 Page 4901

41

42

43

50

...

...

...

...

...

Akers

Funk

Smith

Maters

Page 901 Page 902 Page 903

01

02

03

...

20

...

...

...

...

...

Con

Funk

Woods

...

Crest

21

22

23

...

40

...

...

...

...

...

Russo

Woods

Barr

...

...

51

52

53

...

60

...

...

...

...

...

Smith

Owen

Jones

...

Ale

7991

799

799

799

7999

...

...

...

...

...

Martin

Pica

Jones

Smith

Marks

799

799

799

...

800

...

...

...

...

...

Ganio

Jones

Hall

...

Yu. . . . .

clusterWhat happens when you

a table?

Page 22: SQL Server Indexes Under-the-Hood · 2013-05-26 · PAGE HEADER (96 bytes) Row Offset 400 bytes/row 80,000 rows (1024 x 8) –96 bytes * *

Clustered Index

Page 100 Page 120 Page 130

Akers

Barr

Con

Funk

Funk

...

2334

5678

2534

1334

1534

...

...

...

...

...

...

...

Martin

Martin

Owen

Pica

Russo

...

1234

7778

5878

7878

6078

...

...

...

...

...

...

...

Page 110

Smith

Smith

Smith

Woods

Woods

...

1434

5778

7978

2234

1634

...

...

...

...

...

...

...

Ganio

Hall

Jones

Jones

Jones

...

7678

8078

2434

5978

2634

...

...

...

...

...

...

...

Page 23: SQL Server Indexes Under-the-Hood · 2013-05-26 · PAGE HEADER (96 bytes) Row Offset 400 bytes/row 80,000 rows (1024 x 8) –96 bytes * *

How do I find rows

?clustered

in a tablewith a

index

Page 24: SQL Server Indexes Under-the-Hood · 2013-05-26 · PAGE HEADER (96 bytes) Row Offset 400 bytes/row 80,000 rows (1024 x 8) –96 bytes * *

Clustered Index

Page 140 - Root

Page 100 Page 120 Page 130

Page 141 Page 145

Akers

Barr

Con

Funk

Funk

...

2334

5678

2534

1334

1534

...

...

...

...

...

...

...

Martin

Martin

Owen

Pica

Russo

...

1234

7778

5878

7878

6078

...

...

...

...

...

...

...

Akers

Martin

Martin

Page 110

Owen 5878 ...

Smith

Smith

Smith

Woods

Woods

...

1434

5778

7978

2234

1634

...

...

...

...

...

...

...

Ganio

Hall

Jones

Jones

Jones

...

7678

8078

2434

5978

2634

...

...

...

...

...

...

...

Akers

Ganio

Smith

MartinMartin

Page 25: SQL Server Indexes Under-the-Hood · 2013-05-26 · PAGE HEADER (96 bytes) Row Offset 400 bytes/row 80,000 rows (1024 x 8) –96 bytes * *

How do I find rows

?non-clustered

in aclustered indexwith a

index

Page 26: SQL Server Indexes Under-the-Hood · 2013-05-26 · PAGE HEADER (96 bytes) Row Offset 400 bytes/row 80,000 rows (1024 x 8) –96 bytes * *

Clustered IndexOn Last Name

NonclusteredIndex on

First Name

Non-LeafLevel

Leaf Level(ClusteredKey Value)

Diane

Don

Douglas

Glimp

Hall

Groncki

… …

Jose

Judy

Mike

Lugo

Lew

Nash

… …Mike Nash

Barr Adam

Cox

Dawson

Brian

Matt

… …

Kim

Kocak

LaMee

Shane

Haluk

Brian

… …

Aaron

Diane

Narp

Nash

Nicholls

Sylvie

Mike

Aaron

… …

Barr

Kim

Narp

O’DonnellNarp

Nash Mike …

Jose

Nina

Aaron

...

Jose

Aaron

Adam

Amy

Nicholls

Barr

Strande

… …

SELECT lastname, firstname, phoneFROM memberWHERE firstname = 'Mike'

Clustered IndexOn Last Name

NonclusteredIndex on

First Name

Non-LeafLevel

Leaf Level(ClusteredKey Value)

Diane

Don

Douglas

Glimp

Hall

Groncki

… …

Jose

Judy

Mike

Lugo

Lew

Nash

… …Mike Nash

Barr Adam

Cox

Dawson

Brian

Matt

… …

Kim

Kocak

LaMee

Shane

Haluk

Brian

… …

Aaron

Diane

Narp

Nash

Nicholls

Sylvie

Mike

Aaron

… …

Barr

Kim

Narp

O’DonnellNarp

Nash Mike …

Jose

Nina

Aaron

...

Jose

Aaron

Adam

Amy

Nicholls

Barr

Strande

… …

Page 27: SQL Server Indexes Under-the-Hood · 2013-05-26 · PAGE HEADER (96 bytes) Row Offset 400 bytes/row 80,000 rows (1024 x 8) –96 bytes * *

clustered index

Page 28: SQL Server Indexes Under-the-Hood · 2013-05-26 · PAGE HEADER (96 bytes) Row Offset 400 bytes/row 80,000 rows (1024 x 8) –96 bytes * *

primary keys

clustered index

Page 29: SQL Server Indexes Under-the-Hood · 2013-05-26 · PAGE HEADER (96 bytes) Row Offset 400 bytes/row 80,000 rows (1024 x 8) –96 bytes * *
Page 30: SQL Server Indexes Under-the-Hood · 2013-05-26 · PAGE HEADER (96 bytes) Row Offset 400 bytes/row 80,000 rows (1024 x 8) –96 bytes * *

http://[email protected]@EdwinMSarmientohttp://ca.linkedin.com/in/EdwinMSarmiento

Microsoft MVP - SQL Server