You are on page 1of 42

Simon Cho

[StepbyStep]
SQL server Index operation for
Beginner to Expert
Orange County
4/14/2018
Simon Cho
Chapter leader of SQLAngeles.com
SQL Community Speaker in SQLPass.org
Consultant for SQL Server and Virtual environment
Staff Database Engineer in Visa Inc.

Blog : Simonsql.com
Email : Simon@SimonSQL.com

Like to DIY wood working!


Please Support Our Sponsors
Local User Groups
Orange County User Group
2rd Thursday of each month
bigpass.pass.org

Los Angeles User Group San Diego User Group


3rd Thursday of each odd month 1st & 3rd Thursday of each month
sqlla.pass.org meetup.com/sdsqlug
meetup.com/sdsqlbig

Malibu User Group SQLAngeles - Korean


3rd Wednesday of each month
sqlmalibu.pass.org
Every Other Tuesday
sqlangeles.pass.org

SQLSaturday Los Angeles SQLSaturday San Diego


June 9th September 15th
SQL Summit
Annual International Conference
November 6 -9 | Seattle, WA
2 Days of Pre-Cons
200+ sessions over 3 days
Over 5,000 SQL Professionals
Evening Networking Activities
Discount Code: SSDISODNS
What is the Physical DB objects?
A. Table
B. View
C. Stored Procedure
D. Trigger
E. Synonyms
How much you knows about Index?
No Idea….
Cluster Index vs Non-Cluster Index
Unique Index vs Non-Unique Index
Index Scan vs Index Seek
Covering Index
What type of data is in index Leaf node
Index Compression…
How much you knows about Index?
Today’s Goal!

(Index)
Let’s assume that we are App Developer and want
to save 100 rows to somewhere.
Once data size grows, it can’t fit in Memory.
Create multiple files.
File Seq1 File Seq2 File Seq3

Page 1 Page 2 Page 3


Double Linked List Double Linked List

Note: No data order

Page(Data Blocks) : MSSQL = 8KB, MySQL(InnoDB) = 4KB - 64KB(Default 16KB), Oracle = 2KB – 32 KB(Default 8KB)
Getting bigger and bigger
Page 1
Page 2 New Decision. Let’s order data and create a control page.
Page 3

Page …

Page N Page 0

Note: Data order by Key


Then, what is the NonClustered Index?
I only need a part of data for search, “Name”
Sort By “Name” along with “Seq” key. After that, create new file list like Clustered Index
Page 1
Page ….
Page N

Intermediate
Leaf Nodes Nodes
Root Node
B-Tree Structure

Ref) https://www.ibm.com/support/knowledgecenter/en/SSEPGG_9.5.0/com.ibm.db2.luw.admin.perf.doc/doc/c0005300.html
What data is in Leaf Node?

Index data + Reference Data


CL : All column
NL : CL Key data or RID(Heap unique ID)
What’s Covering Index?

All necessary columns(or more) are

in one Index.
Search By Ordered value(=Index Key)
SELECT * FROM tbl
WHERE Seq = 12001 or Seq = 12100
4 Pages
Cost = (Logical
Page 2 IO)
4 Pages * 8KB = 32 KB

@Seq1 = 12001
Page 2

Page 0
@Seq2 = 12100

Index Seek(=Bookmark Look up = Key Look up)


Note: Possibly read same page multiple times.
Search By Non-Ordered value
SELECT * FROM tbl
WHERE Tel = ‘123-123-1234’ or Tel = ‘456-465-4567’
Cost = N Pages
(Logical IO)

N Pages * 8KB ≈ Index Size(Heap Size)

Index Scan(=Table Scan)


wish to call “heap scan”
Index Overhead(CL + NC) vs Heap
Search : Has benefit.
Insert : 2 inserts CL and NC(IO is not X2)
Delete : 2 deletes CL and NC(IO is not X2)
Update : 2 updates in case of NC data updated.
Space : Extra Space required.
Drop/Change Clustered key

All NonClustered Indexes Rebuild!!!


- All NC leaf data point to Heap key(Rowid) -> Change to new CL key
Heap
Clustered table/index
Nonclustered index on a heap
Nonclustered index on a clustered table
Clustered Index
sysindexes id indid = 1 root
Clustered Index
Akhtar

Martin
Page 140 - Root
Akhtar
SELECT lastname, firstnameMartin
Ganio Smith
FROM
… member …
WHERE
Page 141
lastname = 'Ota' Page 145

Akhtar 2334 ... Ganio 7678 ... Martin 1234 ... Smith 1434 ...
Barr 5678 ... Hall 8078 ... Martin 7778 ... Smith 5778 ...
Con 2534 ... Jones 2434 ... Ota 5878 ... Smith 7978 ...
Funk 1334 ... Jones 5978 ... Phua 7878 ... White 2234 ...
Funk 1534 ... Jones 2634 ... Rudd 6078 ... White 1634 ...
... ... ... ... ... ... ... ... ... ... ... ...
Page 100 Page 110 Page 120 Page 130
Non-clustered Index with Heap
sysindexes id indid = 2 root

Non
Akhtar Non-Leaf
... Clustered
clustered
Martin
Level Index
SELECT lastname,
Page 37 Pagefirstname
28
Page 12 - Root
AkhtarFROM member Martin
Ganio
...
WHERE lastname Smith ...
BETWEEN 'Masters' AND 'Rudd'
Page 41 Page 51 Page 61 Page 71 Leaf Level
Akhtar 4:706:01 Ganio 4:709:01 Martin 4:708:01 Smith 4:706:03 (Key Value)
Barr 4:705:03 Hall 4:709:04 Matey 4:706:04 Smith 4:708:04
Con 4:704:01 Jones 4:709:02 Ota 4:707:02 Smith 4:707:01
Funk 4:706:02 Jones 4:708:03 Phua 4:708:02 White 4:704:03
Funk 4:704:02 Jones 4:707:03 Rudd 4:705:01 White 4:705:02

Heap
Page 704 Page 705 Page 706 Page 707 Page 808 Page 709
01 ... Conn 01 ... Rudd 01 ... Akhtar 01 ... Smith 01 ... Martin 01 ... Ganio
02 ... Funk 02 ... White 02 ... Funk 02 ... Ota
02 02
02 ... Phua 02 ... Jones
03 ... White 03 ... Barr 03 ... Smith 03 ... Jones 03 ... Jones 03 ... Hall
... ... ... ... ... ... 04 ... Matey ... ... ... 04 ... Smith ... ... ...
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
File ID #4
Non-clustered Index with Clustered Index
sysindexes id indid = 2 root
Nonclustered
Index on Aaron Non-Leaf
First Name ... Level
Jose
Aaron Jose
SELECT lastname, firstname,
Deanna Nina phone
FROM …
member …
WHERE firstname = 'Mike'
Aaron Con Deanna Daum Jose Lugo Leaf Level
Adam Barr Don Hall Judy Kaethler (Clustered
Amie Baldwin Doug Hampton Mike Nash Key Value)
… … … … … …

Barr
Clustered Index Kim
On Last Name Nagata
O’Melia
Barr Adam … Kim Shane … Nagata Susanne …
Cox Arlette … Kobara Linda … Nash Mike …
Daum Deanna … LaBrie Ryan … Nixon Toby …
… … … … … … … … …
Inserting Row
'ENG','SCO'
'BEL'
'SPA'
Inserting 'DEN'
'UML'
'AUS' and 'USA'

null
SCO

null
SCO
DEN
UML

AUS
BEL
ENG DEN UML
SCO SPA
USA
ENG USA
Q&A
RDBMS Consulting
Database Design
Performance Tuning(CPU, Memory, IO, Query)
Root Cause Analysis
Virtual Environment Tuning

Simon Cho
Email : Simon@simonsql.com
Index Access Methods
Scan
<Index scan>
Table Scan(Unordered Clustered Index Scan)
Unordered covering nonclustered index scan
Ordered clustered index scan
Ordered Covering Nonclustered Index Scan
<Index scan + Look up>
Unordered Nonclustered Index Scan + Lookups
Seek
<Index Seek>
Clustered Index Seek + Ordered Partial Scan
Covering Nonclustered Index Seek + Ordered Partial Scan
<Index Seek + Look up>
Nonclustered Index Seek + Ordered Partial Scan + Lookups
Table Scan(Unordered Clustered Index
Scan)
SELECT orderid, custid, empid, shipperid, orderdate
FROM dbo.Orders;
Unordered covering nonclustered index
scan
SELECT orderid FROM dbo.Orders;
Ordered clustered index scan
SELECT orderid, custid, empid, shipperid, orderdate
FROM dbo.Orders
ORDER BY orderdate;
Ordered Covering Nonclustered Index
Scan
SELECT orderid, orderdate FROM dbo.Orders ORDER BY orderid;
Nonclustered Index Seek + Ordered
Partial Scan + Lookups
SELECT orderid, custid, empid, shipperid, orderdate
FROM dbo.Orders
WHERE orderid BETWEEN 101 AND 120;
<Nonclustered index seek + ordered partial scan + lookups against a heap>

<Nonclustered index seek + ordered partial scan + lookups against a clustered tale>
Clustered Index Seek + Ordered Partial
Scan
SELECT orderid, custid, empid, shipperid, orderdate FROM dbo.Orders WHERE orderdate = '20060212';
Covering Nonclustered Index Seek +
Ordered Partial Scan
SELECT shipperid, orderdate, custid FROM dbo.Orders WHERE shipperid = 'C' AND orderdate >=
'20060101' AND orderdate < '20070101';

Simon Cho
Q&A
RDBMS Consulting
Database Design
Performance Tuning(CPU, Memory, IO, Query)
Root Cause Analysis
Virtual Environment Tuning

Simon Cho
Email : Simon@simonsql.com

You might also like