cmu scs 15-721 :: indexing (locking & latching) · cmu 15-721 (spring 2016) skip lists . a...

123
Andy Pavlo // Carnegie Mellon University // Spring 2016 Lecture #06 – Indexing (Locking & Latching) DATABASE SYSTEMS 15-721

Upload: others

Post on 19-Aug-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

Andy Pavlo // Carnegie Mellon University // Spring 2016

Lecture #06 – Indexing (Locking & Latching)

DATABASE SYSTEMS

15-721

Page 2: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

TODAY ’S AGENDA

Order Preserving Indexes Index Locking & Latching Prison Gang Tattoos

2

Page 3: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

DATABASE INDEX

A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. Indexes are used to quickly locate data without having to search every row in a table every time a table is accessed.

3

Page 4: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

DATA STRUCTURES

Order Preserving Indexes → A tree-like structure that maintains keys in some

sorted order. → Supports all possible predicates with O(log n) searches.

Hashing Indexes → An associative array that maps a hash of the key to a

particular record. → Only supports equality predicates with O(1) searches.

4

Page 5: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

DATA STRUCTURES

Order Preserving Indexes → A tree-like structure that maintains keys in some

sorted order. → Supports all possible predicates with O(log n) searches.

Hashing Indexes → An associative array that maps a hash of the key to a

particular record. → Only supports equality predicates with O(1) searches.

4

Page 6: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

B-TREE VS. B+TREE

The original B-tree from 1972 stored keys + values in all nodes in the tree. → More memory efficient since each key only appears

once in the tree.

A B+tree only stores values in leaf nodes. Inner nodes only guide the search process. → Easier to manage concurrent index access when the

values are only in the leaf nodes.

5

Page 7: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

B+TREE

6

Page 8: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

B+Tree Leaf Node

B+TREE

6

K1 V1 • • • Kn Vn ¤ ¤ Prev Next

Page 9: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

B+Tree Leaf Node

B+TREE

6

K1 V1 • • • Kn Vn ¤ ¤ Prev Next

Page 10: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

B+Tree Leaf Node

B+TREE

6

Key + Value

K1 V1 • • • Kn Vn ¤ ¤ Prev Next

Page 11: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

B+Tree Leaf Node

B+TREE

6

Key + Value

K1 V1 • • • Kn Vn ¤ ¤ Prev Next

¤ ¤

Page 12: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

B+Tree Leaf Node

B+TREE

6

Sorted Keys K1 K2 K3 K4 K5 • • • Kn

Values ¤ ¤ ¤ ¤ ¤ • • • ¤

¤ Prev

¤ Next

# Level

# Slots

Page 13: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

B+Tree Leaf Node

B+TREE

6

Sorted Keys K1 K2 K3 K4 K5 • • • Kn

Values ¤ ¤ ¤ ¤ ¤ • • • ¤

¤ Prev

¤ Next

# Level

# Slots

Page 14: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

B+TREE DESIGN CHOICES

Non-Unique Indexes: One key maps to multiple values. Variable Length Keys: The size of each key is not the same.

7

Page 15: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

B+TREE: NON-UNIQUE INDEXES

Approach #1: Duplicate Keys → Use the same leaf node layout but store duplicate

keys multiple times.

Approach #2: Value Lists → Store each key only once and maintain a linked list of

unique values.

8

Page 16: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

B+Tree Leaf Node

B+TREE: DUPLICATE KEYS

9

Sorted Keys K1 K1 K1 K2 K2 • • • Kn

¤ Prev

¤ Next

# Level

# Slots

Values ¤ ¤ ¤ ¤ ¤ • • • ¤

Page 17: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

B+Tree Leaf Node

B+TREE: DUPLICATE KEYS

9

Sorted Keys K1 K1 K1 K2 K2 • • • Kn

¤ Prev

¤ Next

# Level

# Slots

Values ¤ ¤ ¤ ¤ ¤ • • • ¤

Page 18: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

B+Tree Leaf Node

B+TREE: DUPLICATE KEYS

9

Sorted Keys K1 K1 K1 K2 K2 • • • Kn

¤ Prev

¤ Next

# Level

# Slots

Values ¤ ¤ ¤ ¤ ¤ • • • ¤

Page 19: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

B+Tree Leaf Node

B+TREE: VALUE L ISTS

10

Values ¤ ¤ ¤ ¤ ¤

• • •

¤ Prev

¤ Next

# Level

# Slots

Sorted Keys K1 K2 K3 K4 K5 • • • Kn

Page 20: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

B+Tree Leaf Node

B+TREE: VALUE L ISTS

10

Values ¤ ¤ ¤ ¤ ¤

• • •

¤ Prev

¤ Next

# Level

# Slots

Sorted Keys K1 K2 K3 K4 K5 • • • Kn

Page 21: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

B+TREE: VARIABLE LENGTH KEYS

Approach #1: Pointers → Store the keys as pointers to the tuple’s attribute.

Approach #2: Variable Length Nodes → The size of each node in the b+tree can vary. → Requires careful memory management.

Approach #3: Key Map → Embed an array of pointers that map to the key +

value list within the node.

11

Page 22: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

B+Tree Leaf Node

B+TREE: KEY MAP

12

Key+Values

• • •

¤ Prev

¤ Next

# Level

# Slots

Key Map

K1 V1 V2

¤ ¤ ¤

K2 V1 V2 V3

Page 23: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

B+Tree Leaf Node

B+TREE: KEY MAP

12

Key+Values

• • •

¤ Prev

¤ Next

# Level

# Slots

Key Map

K1 V1 V2

¤ ¤ ¤

K2 V1 V2 V3

Page 24: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

B+TREE ALTERNATIVES

T-Trees Skip Lists Radix Trees (aka Patricia Trees) MassTree Fractal Trees

13

Page 25: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

B+TREE ALTERNATIVES

T-Trees Skip Lists Radix Trees (aka Patricia Trees) MassTree Fractal Trees

13

Page 26: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

T-TREES

Based on AVL Trees. Instead of storing keys in nodes, store pointers to their original values.

Proposed in 1986 from Univ. of Wisconsin Used in TimesTen and other early in-memory DBMSs during the 1990s.

14

A STUDY OF INDEX STRUCTURES FOR MAIN MEMORY DATABASE MANAGEMENT SYSTEMS VLDB 1986

Page 27: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

T-TREES

15

Page 28: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

T-Tree Node

T-TREES

15

Min-K ¤ Max-K ¤

Parent Pointer

Right Child Pointer

Left Child Pointer

¤ ¤ ¤ ¤

Page 29: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

T-Tree Node

T-TREES

15

Min-K ¤ Max-K ¤

Parent Pointer

Right Child Pointer

Left Child Pointer

¤ ¤ ¤ ¤

Data Pointers

Page 30: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

T-Tree Node

T-TREES

15

Min-K ¤ Max-K ¤

Parent Pointer

Right Child Pointer

Left Child Pointer

¤ ¤ ¤ ¤

Node Boundaries

Page 31: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

Key Space (Low→High)

T-Tree Node

T-TREES

15

Min-K ¤ Max-K ¤

Parent Pointer

Right Child Pointer

Left Child Pointer

¤ ¤ ¤ ¤

1 2 3 4 5 6 7

Page 32: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

Key Space (Low→High)

T-Tree Node

T-TREES

15

Min-K ¤ Max-K ¤

Parent Pointer

Right Child Pointer

Left Child Pointer

¤ ¤ ¤ ¤ 1

2

3

4

5

6

7

1 2 3 4 5 6 7

Page 33: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

Key Space (Low→High)

T-Tree Node

T-TREES

15

Min-K ¤ Max-K ¤

Parent Pointer

Right Child Pointer

Left Child Pointer

¤ ¤ ¤ ¤ 1

2

3

4

5

6

7

1 2 3 4 5 6 7

Page 34: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

T-TREES

Advantages → Uses less memory because it does not store keys

inside of each node.

Disadvantages → Have to chase pointers when scanning range or

performing binary search inside of a node. → Difficult to rebalance. → Difficult to implement safe concurrent access.

16

Page 35: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

SKIP L ISTS

A collection of lists at different levels → Lowest level is a sorted, singly linked list of all keys → 2nd level links every other key → 3rd level links every fourth key → In general, a level has half the keys of one below it

To insert a new key, flip a coin to decide how many levels to add the new key into. Provides approximate O(log n) search times.

17

CONCURRENT MAINTENANCE OF SKIP LISTS Univ. of Maryland Tech Report 1990

Page 36: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

End Levels

K1 V1

K2

K2 V2

K3 V3

K4 V4

K4

K6 V6

SKIP L ISTS: INSERT

18

P=N

P=N/2

P=N/4

Page 37: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

End Levels

K1 V1

K2

K2 V2

K3 V3

K4 V4

K4

K6 V6

SKIP L ISTS: INSERT

18

P=N

P=N/2

P=N/4

Page 38: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

End Levels

K1 V1

K2

K2 V2

K3 V3

K4 V4

K4

K6 V6

SKIP L ISTS: INSERT

18

P=N

P=N/2

P=N/4

Page 39: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

End Levels

K1 V1

K2

K2 V2

K3 V3

K4 V4

K4

K6 V6

SKIP L ISTS: INSERT

18

P=N

P=N/2

P=N/4

Page 40: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

End Levels

K1 V1

K2

K2 V2

K3 V3

K4 V4

K4

K6 V6

SKIP L ISTS: INSERT

18

P=N

P=N/2

P=N/4

Page 41: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

End Levels

K1 V1

K2

K2 V2

K3 V3

K4 V4

K4

K6 V6

SKIP L ISTS: INSERT

18

P=N

P=N/2

P=N/4

Txn #1: Insert K5

Page 42: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

End Levels

K1 V1

K2

K2 V2

K3 V3

K4 V4

K4

K6 V6

SKIP L ISTS: INSERT

18

P=N

P=N/2

P=N/4

Txn #1: Insert K5

Page 43: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

End Levels

K1 V1

K2

K2 V2

K3 V3

K4 V4

K4

K5 V5

K5

K5

K6 V6

SKIP L ISTS: INSERT

18

P=N

P=N/2

P=N/4

Txn #1: Insert K5

Page 44: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

End Levels

K1 V1

K2

K2 V2

K3 V3

K4 V4

K4

K5 V5

K5

K5

K6 V6

SKIP L ISTS: INSERT

18

P=N

P=N/2

P=N/4

Txn #1: Insert K5

Page 45: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

End Levels

K1 V1

K2

K2 V2

K3 V3

K4 V4

K4

K5 V5

K5

K5

K6 V6

SKIP L ISTS: INSERT

18

P=N

P=N/2

P=N/4

Txn #1: Insert K5

Page 46: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

End

K1 V1

K2

K2 V2

K3 V3

K4 V4

K4

K5 V5

K5

K5

K6 V6

Levels

SKIP L ISTS: SEARCH

19

P=N

P=N/2

P=N/4

Page 47: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

End

K1 V1

K2

K2 V2

K3 V3

K4 V4

K4

K5 V5

K5

K5

K6 V6

Levels

SKIP L ISTS: SEARCH

19

P=N

P=N/2

P=N/4

Txn #1: Find K3

Page 48: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

End

K1 V1

K2

K2 V2

K3 V3

K4 V4

K4

K5 V5

K5

K5

K6 V6

Levels

SKIP L ISTS: SEARCH

19

P=N

P=N/2

P=N/4

Txn #1: Find K3

Page 49: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

End

K1 V1

K2

K2 V2

K3 V3

K4 V4

K4

K5 V5

K5

K5

K6 V6

Levels

SKIP L ISTS: SEARCH

19

P=N

P=N/2

P=N/4

Txn #1: Find K3

K3<K5

Page 50: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

End

K1 V1

K2

K2 V2

K3 V3

K4 V4

K4

K5 V5

K5

K5

K6 V6

Levels

SKIP L ISTS: SEARCH

19

P=N

P=N/2

P=N/4

Txn #1: Find K3

K3<K5

K3>K2

Page 51: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

End

K1 V1

K2

K2 V2

K3 V3

K4 V4

K4

K5 V5

K5

K5

K6 V6

Levels

SKIP L ISTS: SEARCH

19

P=N

P=N/2

P=N/4

Txn #1: Find K3

K3<K5

K3>K2 K3<K4

Page 52: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

End

K1 V1

K2

K2 V2

K3 V3

K4 V4

K4

K5 V5

K5

K5

K6 V6

Levels

SKIP L ISTS: SEARCH

19

P=N

P=N/2

P=N/4

Txn #1: Find K3

K3<K5

K3>K2 K3<K4

Page 53: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

SKIP L ISTS

Advantages → Uses less memory than a typical B+tree (only if you

don’t include reverse pointers). → Insertions and deletions do not require rebalancing. → It is possible to implement a concurrent skip list

using only CAS instructions.

Disadvantages → Not cache friendly because they do not optimize

locality of references. → Reverse search is non-trivial.

20

Page 54: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

End

K1 V1

K2

K2 V2

K3 V3

K4 V4

K4

K5 V5

K5

K5

K6 V6

Levels

SKIP L ISTS: REVERSE SEARCH

21

P=N

P=N/2

P=N/4

Source: MemSQL

Page 55: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

End

K1 V1

K2

K2 V2

K3 V3

K4 V4

K4

K5 V5

K5

K5

K6 V6

Levels

SKIP L ISTS: REVERSE SEARCH

21

P=N

P=N/2

P=N/4

Txn #1: Find K3

Source: MemSQL

Page 56: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

End

K1 V1

K2

K2 V2

K3 V3

K4 V4

K4

K5 V5

K5

K5

K6 V6

Levels

SKIP L ISTS: REVERSE SEARCH

21

P=N

P=N/2

P=N/4

Txn #1: Find K3

Source: MemSQL

Page 57: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

End

K1 V1

K2

K2 V2

K3 V3

K4 V4

K4

K5 V5

K5

K5

K6 V6

Levels

SKIP L ISTS: REVERSE SEARCH

21

P=N

P=N/2

P=N/4

Txn #1: Find K3

Source: MemSQL

LAST K5

LAST K4

LAST K2

Page 58: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

End

K1 V1

K2

K2 V2

K3 V3

K4 V4

K4

K5 V5

K5

K5

K6 V6

Levels

SKIP L ISTS: REVERSE SEARCH

21

P=N

P=N/2

P=N/4

Txn #1: Find K3

Source: MemSQL

LAST K5

LAST K4

LAST K2

Page 59: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

End

K1 V1

K2

K2 V2

K3 V3

K4 V4

K4

K5 V5

K5

K5

K6 V6

Levels

SKIP L ISTS: REVERSE SEARCH

21

P=N

P=N/2

P=N/4

Txn #1: Find K3

Source: MemSQL

LAST K5

LAST K4

LAST K2

Page 60: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

End

K1 V1

K2

K2 V2

K3 V3

K4 V4

K4

K5 V5

K5

K5

K6 V6

Levels

SKIP L ISTS: REVERSE SEARCH

21

P=N

P=N/2

P=N/4

Txn #1: Find K3

K3<K5

Source: MemSQL

LAST K5

LAST K4

LAST K2

Page 61: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

End

K1 V1

K2

K2 V2

K3 V3

K4 V4

K4

K5 V5

K5

K5

K6 V6

Levels

SKIP L ISTS: REVERSE SEARCH

21

P=N

P=N/2

P=N/4

Txn #1: Find K3

K3<K5

Source: MemSQL

K3<K4

LAST K5

LAST K4

LAST K2

Page 62: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

End

K1 V1

K2

K2 V2

K3 V3

K4 V4

K4

K5 V5

K5

K5

K6 V6

Levels

SKIP L ISTS: REVERSE SEARCH

21

P=N

P=N/2

P=N/4

Txn #1: Find K3

K3<K5

Source: MemSQL

K3<K4

LAST K5

LAST K4

LAST K2

K3>K2

Page 63: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

End

K1 V1

K2

K2 V2

K3 V3

K4 V4

K4

K5 V5

K5

K5

K6 V6

Levels

SKIP L ISTS: REVERSE SEARCH

21

P=N

P=N/2

P=N/4

Txn #1: Find K3

K3<K5

Source: MemSQL

K3<K4

LAST K5

LAST K4

LAST K2

K3>K2

Page 64: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

End

K1 V1

K2

K2 V2

K3 V3

K4 V4

K4

K5 V5

K5

K5

K6 V6

Levels

SKIP L ISTS: REVERSE SEARCH

21

P=N

P=N/2

P=N/4

Txn #1: Find K3

K3<K5

Source: MemSQL

K3<K4

LAST K5

LAST K4

LAST K2

K3>K2

Page 65: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

WHY ARE INDEXES DIFFERENT ?

The DBMS has to treat locking in indexes differently than how its concurrency control scheme manages database objects.

The physical structure can change as long as the logical contents are consistent.

22

Page 66: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

PROBLEM SCENARIO #1

23

A

B

D G

20

10 35

6 12 23 38 44

C

E F

Page 67: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

PROBLEM SCENARIO #1

23

A

B

D G

20

10 35

6 12 23 38 44

C

E F

Txn #1: Check if 25 exists

Page 68: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

PROBLEM SCENARIO #1

23

A

B

D G

20

10 35

6 12 23 38 44

C

E F

Txn #1: Check if 25 exists

Page 69: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

PROBLEM SCENARIO #1

23

A

B

D G

20

10 35

6 12 23 38 44

C

E F

Txn #1: Check if 25 exists

Page 70: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

PROBLEM SCENARIO #1

23

A

B

D G

20

10 35

6 12 23 38 44

C

E F

Txn #1: Check if 25 exists S

Page 71: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

PROBLEM SCENARIO #1

23

A

B

D G

20

10 35

6 12 23 38 44

C

E F

Txn #1: Check if 25 exists S

Page 72: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

PROBLEM SCENARIO #1

23

A

B

D G

20

10 35

6 12 23 38 44

C

E F

Txn #1: Check if 25 exists S

S

Page 73: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

PROBLEM SCENARIO #1

23

A

B

D G

20

10 35

6 12 23 38 44

C

E F

Txn #1: Check if 25 exists

S

Page 74: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

PROBLEM SCENARIO #1

23

A

B

D G

20

10 35

6 12 23 38 44

C

E F

Txn #1: Check if 25 exists

S

Page 75: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

PROBLEM SCENARIO #1

23

A

B

D G

20

10 35

6 12 23 38 44

C

E F

Txn #1: Check if 25 exists

S

!

Page 76: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

PROBLEM SCENARIO #1

23

A

B

D G

20

10 35

6 12 23 38 44

C

E F

Txn #1: Check if 25 exists Txn #2: Insert 25

Page 77: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

PROBLEM SCENARIO #1

23

A

B

D G

20

10 35

6 12 23 38 44

C

E F

Txn #1: Check if 25 exists Txn #2: Insert 25

X

X

Page 78: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

PROBLEM SCENARIO #1

23

A

B

D G

20

10 35

6 12 23 38 44

C

E F

Txn #1: Check if 25 exists Txn #2: Insert 25

25

X

X

Page 79: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

PROBLEM SCENARIO #1

23

A

B

D G

20

10 35

6 12 23 38 44

C

E F

Txn #1: Check if 25 exists Txn #2: Insert 25

25

Page 80: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

PROBLEM SCENARIO #1

23

A

B

D G

20

10 35

6 12 23 38 44

C

E F

Txn #1: Check if 25 exists Txn #2: Insert 25 Txn #1: Insert 25

25

Page 81: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

PROBLEM SCENARIO #1

23

A

B

D G

20

10 35

6 12 23 38 44

C

E F

Txn #1: Check if 25 exists Txn #2: Insert 25 Txn #1: Insert 25

25

X

X

Page 82: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

PROBLEM SCENARIO #2

24

A

B

D G

20

10 35

6 12 23 38 44

C

E F

Page 83: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

PROBLEM SCENARIO #2

24

A

B

D G

20

10 35

6 12 23 38 44

C

E F

Txn #1: Scan [12, 23]

Page 84: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

PROBLEM SCENARIO #2

24

A

B

D G

20

10 35

6 12 23 38 44

C

E F

Txn #1: Scan [12, 23]

S

Page 85: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

PROBLEM SCENARIO #2

24

A

B

D G

20

10 35

6 12 23 38 44

C

E F

Txn #1: Scan [12, 23]

S S

Page 86: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

PROBLEM SCENARIO #2

24

A

B

D G

20

10 35

6 12 23 38 44

C

E F

Txn #1: Scan [12, 23] Txn #2: Insert 21

Page 87: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

PROBLEM SCENARIO #2

24

A

B

D G

20

10 35

6 12 23 38 44

C

E F

Txn #1: Scan [12, 23] Txn #2: Insert 21

X

X

Page 88: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

PROBLEM SCENARIO #2

24

A

B

D G

20

10 35

6 12 23 38 44

C

E F

Txn #1: Scan [12, 23] Txn #2: Insert 21

23 21

X

X

Page 89: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

PROBLEM SCENARIO #2

24

A

B

D G

20

10 35

6 12 23 38 44

C

E F

Txn #1: Scan [12, 23] Txn #2: Insert 21

23 21

Page 90: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

PROBLEM SCENARIO #2

24

A

B

D G

20

10 35

6 12 23 38 44

C

E F

Txn #1: Scan [12, 23] Txn #2: Insert 21

23 21

Txn #1: Scan [12, 23]

Page 91: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

PROBLEM SCENARIO #2

24

A

B

D G

20

10 35

6 12 23 38 44

C

E F

Txn #1: Scan [12, 23] Txn #2: Insert 21

23 21

Txn #1: Scan [12, 23]

S S

Page 92: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

LOCKS VS. LATCHES

Locks → Protects the index’s logical contents from other txns. → Held for txn duration. → Need to be able to rollback changes.

Latches → Protects the critical sections of the index’s internal

data structure from other threads. → Held for operation duration. → Do not need to be able to rollback changes.

25

A SURVEY OF B-TREE LOCKING TECHNIQUES TODS 2010

Page 93: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

LOCKS VS. LATCHES

26

Locks Latches

Separate… User transactions Threads

Protect… Database Contents In-Memory Data Structures

During… Entire Transactions Critical Sections

Modes… Shared, Exclusive, Update, Intention

Read, Write

Deadlock Detection & Resolution Avoidance

…by… Waits-for, Timeout, Aborts Coding Discipline

Kept in… Lock Manager Protected Data Structure

Source: Goetz Graefe

Page 94: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

INDEX LOCKS

27

Lock Table

txn1 X

txn2 S

txn3 S • • •

txn3 S

txn2 S

txn4 S • • •

txn4 IX

txn6 X

txn5 S • • •

Page 95: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

INDEX LOCKS

27

Lock Table

txn1 X

txn2 S

txn3 S • • •

txn3 S

txn2 S

txn4 S • • •

txn4 IX

txn6 X

txn5 S • • •

Page 96: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

INDEX LOCKS

27

Lock Table

txn1 X

txn2 S

txn3 S • • •

txn3 S

txn2 S

txn4 S • • •

txn4 IX

txn6 X

txn5 S • • •

Page 97: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

LOCK-FREE INDEXES

Possibility #1: No Locks → Txns don’t acquire locks to access/modify database. → Still have to use latches to install updates.

Possibility #2: No Latches → Use multi-versioning inside of the index. Swap

pointers using atomic updates to install updates. → Still have to use locks to validate txns.

28

Page 98: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

INDEX LOCKING

Predicate Locks Key-Value Locks Gap Locks Key-Range Locks Hierarchical Locking

29

Page 99: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

PREDICATE LOCKS

Proposed locking scheme from System R. → Shared lock on the predicate in a WHERE clause of a

SELECT query. → Exclusive lock on the predicate in a WHERE clause of

any UPDATE, INSERT or DELETE query.

Never implemented in any system.

30

THE NOTIONS OF CONSISTENCY AND PREDICATE LOCKS IN A DATABASE SYSTEM CACM 1976

Page 100: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

PREDICATE LOCKS

31

SELECT SUM(balance) FROM account WHERE name = ‘Tupac’

INSERT INTO account (name, balance) VALUES (‘Tupac’, 100);

Records in Table ‘account’

Page 101: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

PREDICATE LOCKS

31

SELECT SUM(balance) FROM account WHERE name = ‘Tupac’

INSERT INTO account (name, balance) VALUES (‘Tupac’, 100);

Records in Table ‘account’

Page 102: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

PREDICATE LOCKS

31

SELECT SUM(balance) FROM account WHERE name = ‘Tupac’

INSERT INTO account (name, balance) VALUES (‘Tupac’, 100);

name=‘Tupac’

Records in Table ‘account’

Page 103: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

PREDICATE LOCKS

31

SELECT SUM(balance) FROM account WHERE name = ‘Tupac’

INSERT INTO account (name, balance) VALUES (‘Tupac’, 100);

name=‘Tupac’

name=‘Tupac’∧ balance=100

Records in Table ‘account’

Page 104: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

KEY-VALUE LOCKS

Locks that cover a single key value. Need “virtual keys” for non-existent values.

32

10 12 14 16

B+Tree Leaf Node

Page 105: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

KEY-VALUE LOCKS

Locks that cover a single key value. Need “virtual keys” for non-existent values.

32

10 12 14 16

B+Tree Leaf Node Key

[14, 14]

Page 106: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

GAP LOCKS

Each txn acquires a key-value lock on the single key that it wants to access. Then get a gap lock on the next key gap.

33

10 12 14 16

B+Tree Leaf Node

Page 107: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

GAP LOCKS

Each txn acquires a key-value lock on the single key that it wants to access. Then get a gap lock on the next key gap.

33

10 12 14 16 {Gap} {Gap} {Gap}

B+Tree Leaf Node

Page 108: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

GAP LOCKS

Each txn acquires a key-value lock on the single key that it wants to access. Then get a gap lock on the next key gap.

33

10 12 14 16 {Gap} {Gap} {Gap}

B+Tree Leaf Node

Gap (14, 16)

Page 109: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

KEY-RANGE LOCKS

A txn takes locks on ranges in the key space. → Each range is from one key that appears in the

relation, to the next that appears. → Define lock modes so conflict table will capture

commutativity of the operations available.

34

Page 110: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

KEY-RANGE LOCKS

Locks that cover a key value and the gap to the next key value in a single index. → Need “virtual keys” for artificial values (infinity)

35

10 12 14 16 {Gap} {Gap} {Gap}

B+Tree Leaf Node

Page 111: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

KEY-RANGE LOCKS

Locks that cover a key value and the gap to the next key value in a single index. → Need “virtual keys” for artificial values (infinity)

35

10 12 14 16 {Gap} {Gap} {Gap}

B+Tree Leaf Node

Next Key [14, 16)

Page 112: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

KEY-RANGE LOCKS

Locks that cover a key value and the gap to the next key value in a single index. → Need “virtual keys” for artificial values (infinity)

35

10 12 14 16 {Gap} {Gap} {Gap}

B+Tree Leaf Node

Prior Key (12, 14]

Page 113: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

HIERARCHICAL LOCKING

Allow for a txn to hold wider key-range locks with different locking modes. → Reduces the number of visits to lock manager.

36

10 12 14 16 {Gap} {Gap} {Gap}

B+Tree Leaf Node

Page 114: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

HIERARCHICAL LOCKING

Allow for a txn to hold wider key-range locks with different locking modes. → Reduces the number of visits to lock manager.

36

10 12 14 16 {Gap} {Gap} {Gap}

B+Tree Leaf Node IX

[10, 16)

Page 115: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

HIERARCHICAL LOCKING

Allow for a txn to hold wider key-range locks with different locking modes. → Reduces the number of visits to lock manager.

36

10 12 14 16 {Gap} {Gap} {Gap}

B+Tree Leaf Node IX

[10, 16)

[14, 16) X

Page 116: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

PARTING THOUGHTS

Hierarchical locking essentially provides predicate locking without complications. → Index locking occurs only in the leaf nodes. → Latching is to ensure consistent data structure.

Just like concurrency control schemes, research on fast indexes is hot again.

37

Page 117: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

PRISON TAT TOOS

Some of you are going to end up in prison. → This is just the nature of the database game.

Part of surviving prison is being able to navigate and avoid the various factions.

38

Page 118: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

TEAR DROP

39

Page 119: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

THREE DOTS

40

Page 120: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

FIVE DOTS

41

Page 121: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

MARA SALVATRUCHA GANG (MS13)

42

Page 122: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

ARYAN BROTHERHOOD

43

Page 123: CMU SCS 15-721 :: Indexing (Locking & Latching) · CMU 15-721 (Spring 2016) SKIP LISTS . A collection of lists at different levels →Lowest level is a sorted, singly linked list

CMU 15-721 (Spring 2016)

NEXT CLASS

Bw-Tree (Hekaton) Concurrent Skip Lists (MemSQL) ART Index (HyPer)

44