Share
Embed
Annual Pro sale · Save $30
Have presentations of your own?
Share with private links, password protection, and more control over your work.
Transcript
118 slides
-
Abstraction Layers DBMS Architecture Levels: • External (View) Level •
Logical (Conceptual) Level • Physical (Internal) Level 3 -
Outline How to store a table on disk? • Sequential
file, Hash map, B tree, ... 4 -
Outline How to store a table on disk? • Sequential
file, Hash map, B tree, ... Indexing, for faster retrieval 4 -
Outline How to store a table on disk? • Sequential
file, Hash map, B tree, ... Indexing, for faster retrieval • Single and multiple index, Bitmap index, Clustered vs. non-clustered, ... 4 -
Outline How to store a table on disk? • Sequential
file, Hash map, B tree, ... Indexing, for faster retrieval • Single and multiple index, Bitmap index, Clustered vs. non-clustered, ... • Tradeoffs in using (multiple) indices 4 -
5 Users table: UID Name Email Gender Age Group City
Salary 1 Alice [email protected] Female 30-40 London 1260 2 Bob [email protected] Male 20-30 Paris 907 3 Carol [email protected] Female 20-30 Shanghai 1400 4 Dave [email protected] Male 10-20 Sydney 2577 5 Eve [email protected] Female 50-60 Toronto 3790 6 Mallory [email protected] Female 30-40 Shanghai 1968 7 Peggy [email protected] Female 40-50 Cairo 3731 438532 Steve [email protected] Male 50-60 Cupertino 7110 -
Sequential Files 6 • CSV (comma separated values) • Fixed-length
fields • Variable-length fields -
Sequential Files 6 • CSV (comma separated values) • Fixed-length
fields • Variable-length fields • XML -
Sequential Files 6 • CSV (comma separated values) • Fixed-length
fields • Variable-length fields • XML • ... -
7 CSV (Variable-length Fields) 1,Alice,[email protected],Female,30-40,London,1260\n 2,Bob,[email protected],Male,20-30,Paris,907\n 3,Carol,[email protected],Female,20-30,Shanghai,1400\n 4,Dave,[email protected],Male,10-20,Sydney,2577\n 5,Eve,[email protected],Female,50-60,Toronto,3790\n 6,Mallory,[email protected],Female,30-40,Shanghai,1968\n
7,Peggy,[email protected],Female,40-50,Cairo,3731\n ...... 438532,Steve,[email protected],Male,50-60,Cupertino,7110\n /home/pooria/db/users.table -
8 CSV (Variable-length Fields) • Search: Slow • Update: Slow
• Delete: Slow • Space: Very efficient -
9 CSV (Variable-length Fields) Suitable for • Small tables and
short Logs • Tables that don’t change very often • Tables that must be read sequentially (moving data between two application) • Tables with variable-length fields -
10 CSV (Fixed-length Fields) 1, Alice,[email protected],Female,30-40, London,1260\n 2, Bob, [email protected],
Male,20-30, Paris, 907\n 3, Carol,[email protected],Female,20-30, Shanghai,1400\n 4, Dave, [email protected], Male,10-20, Sydney,2577\n 5, Eve,[email protected],Female,50-60, Toronto,3790\n 6,Mallory, [email protected],Female,30-40, Shanghai,1968\n 7, Peggy,[email protected],Female,40-50, Cairo,3731\n ...... 438532, Steve, [email protected], Male,50-60,Cupertino,7110\n /home/pooria/db/users.table -
11 CSV (Fixed-length Fields) /home/pooria/db/users.table 1 [email protected] London1260 2 Bob
[email protected] Male20-30 Paris 907 3 [email protected] Shanghai1400 4 Dave [email protected] Male10-20 Sydney2577 5 [email protected] Toronto3790 6Mallory [email protected] Shanghai1968 7 [email protected] Cairo3731 ...... 438532 Steve [email protected] Male50-60Cupertino7110 -
12 CSV (Fixed-length Fields) • Search: A bit faster (than
variable-length) • Update: Very fast (however, we must search the tuple first) • Delete: Slow • Space: Not efficient -
13 CSV (Fixed-length Fields) Suitable for • Logs • Tables
that can be read sequentially • Tables with short, fixed-length fields -
2 | Bob | 26 | [email protected] 14 | Jane
| 20 | [email protected] … 5 | Smith | 14 | [email protected] 13 | Adams | 35 | [email protected] … 6 | Jones | 19 | [email protected] 7 | Jane | 48 | [email protected] … 1 | Blake | 23 | [email protected] … … Hash Map 14 UID Hash function -
2 | Bob | 26 | [email protected] 14 | Jane
| 20 | [email protected] … 5 | Smith | 14 | [email protected] 13 | Adams | 35 | [email protected] … 6 | Jones | 19 | [email protected] 7 | Jane | 48 | [email protected] … 1 | Blake | 23 | [email protected] … … UID=7 Hash Map 14 UID Hash function -
2 | Bob | 26 | [email protected] 14 | Jane
| 20 | [email protected] … 5 | Smith | 14 | [email protected] 13 | Adams | 35 | [email protected] … 6 | Jones | 19 | [email protected] 7 | Jane | 48 | [email protected] … 1 | Blake | 23 | [email protected] … … UID=7 Hash Map 14 UID Hash function -
2 | Bob | 26 | [email protected] 14 | Jane
| 20 | [email protected] … 5 | Smith | 14 | [email protected] 13 | Adams | 35 | [email protected] … 6 | Jones | 19 | [email protected] 7 | Jane | 48 | [email protected] … 1 | Blake | 23 | [email protected] … … UID=7 Hash Map 14 City=Cupertino UID Hash function -
2 | Bob | 26 | [email protected] 14 | Jane
| 20 | [email protected] … 5 | Smith | 14 | [email protected] 13 | Adams | 35 | [email protected] … 6 | Jones | 19 | [email protected] 7 | Jane | 48 | [email protected] … 1 | Blake | 23 | [email protected] … … UID=7 Hash Map 14 City=Cupertino UID Hash function -
Hash Map Good: • Fast (for lookup field) • Easy
to implement 15 -
Hash Map Good: • Fast (for lookup field) • Easy
to implement • Relatively space-efficient (no pointers) 15 -
Hash Map Good: • Fast (for lookup field) • Easy
to implement • Relatively space-efficient (no pointers) 15 -
Hash Map Good: • Fast (for lookup field) • Easy
to implement • Relatively space-efficient (no pointers) Bad: 15 -
Hash Map Good: • Fast (for lookup field) • Easy
to implement • Relatively space-efficient (no pointers) Bad: • Naïve implementations only allow one index (lookup key) per table 15 -
Hash Map Good: • Fast (for lookup field) • Easy
to implement • Relatively space-efficient (no pointers) Bad: • Naïve implementations only allow one index (lookup key) per table • Not scalable (for non-memory resident data) 15 -
Hash Map Good: • Fast (for lookup field) • Easy
to implement • Relatively space-efficient (no pointers) Bad: • Naïve implementations only allow one index (lookup key) per table • Not scalable (for non-memory resident data) • Not suitable for range queries 15 -
16 ... 11 | Smi- 10 | Bob ... 14
| Cla- 13 | Bla- ... 17 | Ste- 16 | Jan- ... ... ... 9 | Joh- ... ... B tree ... 15 | Ada- 12 | Jon- -
• They were specifically built for database storage • They
are I/O-friendly 17 B tree -
• They were specifically built for database storage • They
are I/O-friendly • B trees are fast and scalable 17 B tree -
• They were specifically built for database storage • They
are I/O-friendly • B trees are fast and scalable • They can be modified to allow more than one index per table 17 B tree -
• They were specifically built for database storage • They
are I/O-friendly • B trees are fast and scalable • They can be modified to allow more than one index per table • They work well with range queries 17 B tree -
19 B tree Good: • Fast (for indexed fields) •
Scalable • Suitable for range queries (on indexed fields) -
19 B tree Good: • Fast (for indexed fields) •
Scalable • Suitable for range queries (on indexed fields) Bad: -
19 B tree Good: • Fast (for indexed fields) •
Scalable • Suitable for range queries (on indexed fields) Bad: • Space overhead -
19 B tree Good: • Fast (for indexed fields) •
Scalable • Suitable for range queries (on indexed fields) Bad: • Space overhead • Negative impact on performance -
Index •Why use indices? • Faster retrieval • Most efficient
in range queries •Tradeoffs • Slower writes/updates vs. faster retrieval • Disk space overhead 20 -
21 Users table: UID Name Email Gender Age Group City
Salary 1 Alice [email protected] Female 30-40 London 1260 2 Bob [email protected] Male 20-30 Paris 907 3 Carol [email protected] Female 20-30 Shanghai 1400 4 Dave [email protected] Male 10-20 Sydney 2577 5 Eve [email protected] Female 50-60 Toronto 3790 6 Mallory [email protected] Female 30-40 Shanghai 1968 7 Peggy [email protected] Female 40-50 Cairo 3731 438532 Steve [email protected] Male 50-60 Cupertino 7110 -
Why Use Indices? • SELECT name, city FROM users WHERE
city LIKE ‘S%’ 23 Sample queries: -
Why Use Indices? • SELECT name, city FROM users WHERE
city LIKE ‘S%’ • SELECT name, city FROM users WHERE uid BETWEEN 10 AND 20 23 Sample queries: -
24 UID 10 11 12 13 14 15 Index (in
memory) 8 Bytes (4 bytes for keys, 4 bytes for pointers) -
24 UID 10 11 12 13 14 15 UID 7
… … … … … 13 … … … … … 11 … … … … … 9 … … … … … 12 … … … … … 10 … … … … … Index (in memory) Data (on disk) 8 Bytes (4 bytes for keys, 4 bytes for pointers) -
24 UID 10 11 12 13 14 15 UID 7
… … … … … 13 … … … … … 11 … … … … … 9 … … … … … 12 … … … … … 10 … … … … … Index (in memory) Data (on disk) 8 Bytes (4 bytes for keys, 4 bytes for pointers) 480 Bytes -
24 UID 10 11 12 13 14 15 UID 7
… … … … … 13 … … … … … 11 … … … … … 9 … … … … … 12 … … … … … 10 … … … … … Index (in memory) Data (on disk) 8 Bytes (4 bytes for keys, 4 bytes for pointers) 480 Bytes -
24 UID 10 11 12 13 14 15 UID 7
… … … … … 13 … … … … … 11 … … … … … 9 … … … … … 12 … … … … … 10 … … … … … Index (in memory) Data (on disk) 8 Bytes (4 bytes for keys, 4 bytes for pointers) 480 Bytes -
24 UID 10 11 12 13 14 15 UID 7
… … … … … 13 … … … … … 11 … … … … … 9 … … … … … 12 … … … … … 10 … … … … … Index (in memory) Data (on disk) 8 Bytes (4 bytes for keys, 4 bytes for pointers) 480 Bytes -
24 UID 10 11 12 13 14 15 UID 7
… … … … … 13 … … … … … 11 … … … … … 9 … … … … … 12 … … … … … 10 … … … … … Index (in memory) Data (on disk) 8 Bytes (4 bytes for keys, 4 bytes for pointers) 480 Bytes -
25 Name Alice Bob Bernard Carole Cindy Dave UID Name
8 Peggy … … … … 3 Carole … … … … 2 Bob … … … … 26 Trudy … … … … 32 Bernard … … … … 1 Alice … … … … Index (in memory) Data (on disk) -
25 Name Alice Bob Bernard Carole Cindy Dave UID Name
8 Peggy … … … … 3 Carole … … … … 2 Bob … … … … 26 Trudy … … … … 32 Bernard … … … … 1 Alice … … … … Index (in memory) Data (on disk) -
25 Name Alice Bob Bernard Carole Cindy Dave UID Name
8 Peggy … … … … 3 Carole … … … … 2 Bob … … … … 26 Trudy … … … … 32 Bernard … … … … 1 Alice … … … … Index (in memory) Data (on disk) -
25 Name Alice Bob Bernard Carole Cindy Dave UID Name
8 Peggy … … … … 3 Carole … … … … 2 Bob … … … … 26 Trudy … … … … 32 Bernard … … … … 1 Alice … … … … Index (in memory) Data (on disk) -
25 Name Alice Bob Bernard Carole Cindy Dave UID Name
8 Peggy … … … … 3 Carole … … … … 2 Bob … … … … 26 Trudy … … … … 32 Bernard … … … … 1 Alice … … … … Index (in memory) Data (on disk) -
26 B tree Separate trees for index & data Data
(UID): ... 11 | Smi- 10 | Bob ... 14 | Cla- 13 | Bla- ... 17 | Ste- 16 | Jan- ... ... ... ... 15 | Ada- 12 | Jon- -
26 B tree Separate trees for index & data Index
(UID): Data (UID): ... 11 10 ... 14 13 ... 17 16 ... ... ... ... 15 12 ... 11 | Smi- 10 | Bob ... 14 | Cla- 13 | Bla- ... 17 | Ste- 16 | Jan- ... ... ... ... 15 | Ada- 12 | Jon- -
26 B tree Separate trees for index & data Index
(UID): Data (UID): ... 11 10 ... 14 13 ... 17 16 ... ... ... ... 15 12 ... 11 | Smi- 10 | Bob ... 14 | Cla- 13 | Bla- ... 17 | Ste- 16 | Jan- ... ... ... ... 15 | Ada- 12 | Jon- Memory Disk -
27 B tree Separate trees for index & data ...
11 | Smi- 10 | Bob ... 14 | Cla- 13 | Bla- ... 17 | Ste- 16 | Jan- ... ... ... ... 11 10 ... 14 13 ... 17 16 ... ... ... ... 15 12 ... 15 | Ada- 12 | Jon- Index (UID): Data (UID): Memory Disk -
28 B tree Multiple Indexes ... 11 | Smi- 10
| Bob ... 14 | Cla- 13 | Bla- ... 17 | Ste- 16 | Jan- ... ... ... ... 15 | Ada- 12 | Jon- ... 11 10 ... 14 13 ... 17 16 ... ... ... ... 15 12 UID: Memory Disk -
29 B tree Multiple Indexes ... 11 | Smi- 10
| Bob ... 14 | Cla- 13 | Bla- ... 17 | Ste- 16 | Jan- ... ... ... ... 11 10 ... 14 13 ... 17 16 ... ... ... ... 15 12 ... 15 | Ada- 12 | Jon- UID: Memory Disk -
30 B tree Multiple Indexes ... 11 | Smi- 10
| Bob ... 14 | Cla- 13 | Bla- ... 17 | Ste- 16 | Jan- ... ... ... ... 11 10 ... 14 13 ... 17 16 ... ... ... ... 15 12 ... 15 | Ada- 12 | Jon- UID: ... Bl Ad ... Jan Cl ... Ste Sm ... ... ... ... Jon Bo Username: Memory Disk -
31 B tree Multiple Indexes ... 11 | Smi- 10
| Bob ... 14 | Cla- 13 | Bla- ... 17 | Ste- 16 | Jan- ... ... ... ... 11 10 ... 14 13 ... 17 16 ... ... ... ... 15 12 ... 15 | Ada- 12 | Jon- UID: ... Bl Ad ... Jan Cl ... Ste Sm ... ... ... ... Jon Bo Username: Memory Disk -
32 B tree Multiple Indexes ... 11 | Smi- 10
| Bob ... 14 | Cla- 13 | Bla- ... 17 | Ste- 16 | Jan- ... ... ... ... 11 10 ... 14 13 ... 17 16 ... ... ... ... 15 12 ... 15 | Ada- 12 | Jon- UID: ... Bl Ad ... Jan Cl ... Ste Sm ... ... ... ... Jon Bo Username: Age: ... 19 14 ... 26 23 ... 56 48 ... ... ... ... 35 20 Memory Disk -
33 B tree Multiple Indexes ... 11 | Smi- 10
| Bob ... 14 | Cla- 13 | Bla- ... 17 | Ste- 16 | Jan- ... ... ... ... 11 10 ... 14 13 ... 17 16 ... ... ... ... 15 12 ... 15 | Ada- 12 | Jon- UID: ... Bl Ad ... Jan Cl ... Ste Sm ... ... ... ... Jon Bo Username: ... 19 14 ... 26 23 ... 56 48 ... ... ... ... 35 20 Age: Memory Disk -
Index trees are small. Therefore, we can fit all index
trees in main memory. 34 B tree Multiple Indexes -
Clustered vs. Non-clustered Indexes Non-clustered Index: The data rows may
be randomly spread throughout the table. A non-clustered index tree contains the index keys in sorted order, with the leaf level of the index containing the pointer to the page and the row number in the data page. Clustered Index: The ordering of the physical data rows is in accordance with the index blocks that point to them. Therefore, only one clustered index can be created on a given table. 35 -
36 ... 11 | Smi- 10 | Bob ... 14
| Cla- 13 | Bla- ... 17 | Ste- 16 | Jan- ... ... ... ... 11 10 ... 14 13 ... 17 16 ... ... ... ... 15 12 ... 15 | Ada- 12 | Jon- UID: ... Bl Ad ... Jan Cl ... Ste Sm ... ... ... ... Jon Bo Username: Age: ... 19 14 ... 26 23 ... 48 35 ... ... ... ... 26 20 Clustered Non-clustered Memory Disk -
Clustered vs. Non-clustered Indexes • Each table must have one
(and only one) clustered index (typically primary key) • Each table can have any number of non-clustered indexes 37 -
Reverse Index CREATE INDEX ON email SELECT name FROM users
WHERE email LIKE ‘%@aut.ac.ir’ 38 Sample Query: Find all users with ‘aut.ac.ir’ email account -
Reverse Index 39 CREATE INDEX ON reverse(email) SELECT name FROM
users WHERE reverse(email) LIKE reverse(‘%@aut.ac.ir’) Sample Query: Find all users with ‘aut.ac.ir’ email account -
Bitmap Index Suitable for: • Categorical fields, with a small
number of possible values (age group, account type) • Boolean fields (gender) 40 -
Bitmap Index 41 Gender (Female): 011101100010101010…1 Age Group (20-30): 011001000010000010…1
Age Group (30-40): 000100011100000001…0 … Female, Age between 20 and 40: 011101000010000010…1 -
Tradeoffs In Using Indices Good: • Much faster search (on
indexed fields) 42 -
Tradeoffs In Using Indices Good: • Much faster search (on
indexed fields) • Range queries 42 -
Tradeoffs In Using Indices Good: • Much faster search (on
indexed fields) • Range queries Bad: 42 -
Tradeoffs In Using Indices Good: • Much faster search (on
indexed fields) • Range queries Bad: • Slower insert/update/delete 42 -
Tradeoffs In Using Indices Good: • Much faster search (on
indexed fields) • Range queries Bad: • Slower insert/update/delete • Space overhead 42 -
Tradeoffs In Using Indices 43 No indices 1 Index 2
indices 3 indices 4 indices Number of transactions per second: -
Tradeoffs In Using Indices 43 No indices 1 Index 2
indices 3 indices 4 indices 100 Number of transactions per second: -
Tradeoffs In Using Indices 43 No indices 1 Index 2
indices 3 indices 4 indices 100 65 Number of transactions per second: -
Tradeoffs In Using Indices 43 No indices 1 Index 2
indices 3 indices 4 indices 100 65 22 Number of transactions per second: -
Tradeoffs In Using Indices 43 No indices 1 Index 2
indices 3 indices 4 indices 100 65 22 15 Number of transactions per second: -
Tradeoffs In Using Indices 43 No indices 1 Index 2
indices 3 indices 4 indices 100 65 22 15 5 Number of transactions per second: -
Indices in MySQL CREATE INDEX IX_users_username ON users (username); SELECT
* FROM users WITH(INDEX(IX_users_username)) WHERE username = ‘Jane’ DROP INDEX users.IX_users_username; 44 -
Indices in Microsoft SQL Server CREATE [NONCLUSTERED] INDEX IX_users_username ON
users (username); GO SELECT * FROM users WITH(INDEX(IX_users_username)) WHERE username = ‘Jane’ DROP INDEX users.IX_users_username GO 45 -
Questions • Storage, B+ tree, ... • Indices • Project
1: MySQL • Project 2: MongoDB 46 -
Questions • Storage, B+ tree, ... • Indices • Project
1: MySQL • Project 2: MongoDB • Optional Project 46