Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Database Storage and Indices

Database Storage and Indices

Pooria Azimi

April 12, 2012
Tweet

More Decks by Pooria Azimi

Other Decks in Programming

Transcript

  1. Abstraction Layers DBMS Architecture Levels: • External (View) Level •

    Logical (Conceptual) Level • Physical (Internal) Level 3
  2. Outline How to store a table on disk? • Sequential

    file, Hash map, B tree, ... Indexing, for faster retrieval 4
  3. 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
  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. 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
  6. Sequential Files 6 • CSV (comma separated values) • Fixed-length

    fields • Variable-length fields • XML
  7. Sequential Files 6 • CSV (comma separated values) • Fixed-length

    fields • Variable-length fields • XML • ...
  8. 8 CSV (Variable-length Fields) • Search: Slow • Update: Slow

    • Delete: Slow • Space: Very efficient
  9. 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. 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. 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. 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. 13 CSV (Fixed-length Fields) Suitable for • Logs • Tables

    that can be read sequentially • Tables with short, fixed-length fields
  14. 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
  15. 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
  16. 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
  17. 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
  18. 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
  19. Hash Map Good: • Fast (for lookup field) • Easy

    to implement • Relatively space-efficient (no pointers) 15
  20. Hash Map Good: • Fast (for lookup field) • Easy

    to implement • Relatively space-efficient (no pointers) 15
  21. Hash Map Good: • Fast (for lookup field) • Easy

    to implement • Relatively space-efficient (no pointers) Bad: 15
  22. 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
  23. 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
  24. 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
  25. 16 ... 11 | Smi- 10 | Bob ... 14

    | Cla- 13 | Bla- ... 17 | Ste- 16 | Jan- ... ... ... 9 | Joh- ... ... B tree ... 15 | Ada- 12 | Jon-
  26. • They were specifically built for database storage • They

    are I/O-friendly • B trees are fast and scalable 17 B tree
  27. • 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
  28. • 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
  29. 19 B tree Good: • Fast (for indexed fields) •

    Scalable • Suitable for range queries (on indexed fields)
  30. 19 B tree Good: • Fast (for indexed fields) •

    Scalable • Suitable for range queries (on indexed fields) Bad:
  31. 19 B tree Good: • Fast (for indexed fields) •

    Scalable • Suitable for range queries (on indexed fields) Bad: • Space overhead
  32. 19 B tree Good: • Fast (for indexed fields) •

    Scalable • Suitable for range queries (on indexed fields) Bad: • Space overhead • Negative impact on performance
  33. Index •Why use indices? • Faster retrieval • Most efficient

    in range queries •Tradeoffs • Slower writes/updates vs. faster retrieval • Disk space overhead 20
  34. 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
  35. Why Use Indices? • SELECT name, city FROM users WHERE

    city LIKE ‘S%’ 23 Sample queries:
  36. 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:
  37. 24

  38. 24 UID 10 11 12 13 14 15 Index (in

    memory) 8 Bytes (4 bytes for keys, 4 bytes for pointers)
  39. 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)
  40. 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
  41. 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
  42. 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
  43. 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
  44. 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
  45. 25

  46. 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)
  47. 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)
  48. 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)
  49. 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)
  50. 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)
  51. 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-
  52. 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-
  53. 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
  54. 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
  55. 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
  56. 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
  57. 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
  58. 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
  59. 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
  60. 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
  61. Index trees are small. Therefore, we can fit all index

    trees in main memory. 34 B tree Multiple Indexes
  62. 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
  63. 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
  64. 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
  65. 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
  66. 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
  67. Bitmap Index Suitable for: • Categorical fields, with a small

    number of possible values (age group, account type) • Boolean fields (gender) 40
  68. 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
  69. Tradeoffs In Using Indices Good: • Much faster search (on

    indexed fields) • Range queries 42
  70. Tradeoffs In Using Indices Good: • Much faster search (on

    indexed fields) • Range queries Bad: 42
  71. Tradeoffs In Using Indices Good: • Much faster search (on

    indexed fields) • Range queries Bad: • Slower insert/update/delete 42
  72. Tradeoffs In Using Indices Good: • Much faster search (on

    indexed fields) • Range queries Bad: • Slower insert/update/delete • Space overhead 42
  73. Tradeoffs In Using Indices 43 No indices 1 Index 2

    indices 3 indices 4 indices Number of transactions per second:
  74. Tradeoffs In Using Indices 43 No indices 1 Index 2

    indices 3 indices 4 indices 100 Number of transactions per second:
  75. Tradeoffs In Using Indices 43 No indices 1 Index 2

    indices 3 indices 4 indices 100 65 Number of transactions per second:
  76. Tradeoffs In Using Indices 43 No indices 1 Index 2

    indices 3 indices 4 indices 100 65 22 Number of transactions per second:
  77. Tradeoffs In Using Indices 43 No indices 1 Index 2

    indices 3 indices 4 indices 100 65 22 15 Number of transactions per second:
  78. 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:
  79. 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
  80. 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
  81. Questions • Storage, B+ tree, ... • Indices • Project

    1: MySQL • Project 2: MongoDB • Optional Project 46