Professional Documents
Culture Documents
[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
(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(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
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?
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
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