04
play

04 Part II Intro to Database Systems Andy Pavlo AP AP - PowerPoint PPT Presentation

Database Storage 04 Part II Intro to Database Systems Andy Pavlo AP AP 15-445/15-645 Computer Science Fall 2020 Carnegie Mellon University 2 ADM INISTRIVIA Project #1 will be released on September 14 th 15-445/645 (Fall 2020) 3 DISK-


  1. Database Storage 04 Part II Intro to Database Systems Andy Pavlo AP AP 15-445/15-645 Computer Science Fall 2020 Carnegie Mellon University

  2. 2 ADM INISTRIVIA Project #1 will be released on September 14 th 15-445/645 (Fall 2020)

  3. 3 DISK- O RIEN TED ARCHITECTURE The DBMS assumes that the primary storage location of the database is on non-volatile disk. The DBMS's components manage the movement of data between non-volatile and volatile storage. 15-445/645 (Fall 2020)

  4. 4 SLOTTED PAGES Slot Array The most common layout scheme is called slotted pages. Header The slot array maps "slots" to the tuples' starting position offsets. Tuple #4 Tuple #3 The header keeps track of: Tuple #2 Tuple #1 → The # of used slots → The offset of the starting location of the last slot used. Fixed/Var-length Tuple Data 15-445/645 (Fall 2020)

  5. 4 SLOTTED PAGES Slot Array The most common layout scheme is called slotted pages. Header The slot array maps "slots" to the tuples' starting position offsets. Tuple #4 Tuple #3 The header keeps track of: Tuple #2 Tuple #1 → The # of used slots → The offset of the starting location of the last slot used. Fixed/Var-length Tuple Data 15-445/645 (Fall 2020)

  6. 5 LOG- STRUCTURED FILE ORGANIZATIO N Instead of storing tuples in pages, the DBMS only stores log records. The system appends log records to the file of how the database was modified: → Inserts store the entire tuple. → Deletes mark the tuple as deleted. → Updates contain the delta of just the attributes that were modified. 15-445/645 (Fall 2020)

  7. 5 LOG- STRUCTURED FILE ORGANIZATIO N Page Instead of storing tuples in pages, the DBMS only stores log records. INSERT id=1,val=a New Entries INSERT id=2,val=b The system appends log records to the DELETE id=4 file of how the database was modified: INSERT id=3,val=c → Inserts store the entire tuple. UPDATE val=X (id=3) → Deletes mark the tuple as deleted. UPDATE val=Y (id=4) → Updates contain the delta of just the … attributes that were modified. 15-445/645 (Fall 2020)

  8. 6 LOG- STRUCTURED FILE ORGANIZATIO N Page To read a record, the DBMS scans the log backwards and "recreates" the INSERT id=1,val=a tuple to find what it needs. INSERT id=2,val=b Reads DELETE id=4 INSERT id=3,val=c UPDATE val=X (id=3) UPDATE val=Y (id=4) … 15-445/645 (Fall 2020)

  9. 6 LOG- STRUCTURED FILE ORGANIZATIO N Page To read a record, the DBMS scans the log backwards and "recreates" the INSERT id=1,val=a tuple to find what it needs. INSERT id=2,val=b id=1 DELETE id=4 id=2 Build indexes to allow it to jump to INSERT id=3,val=c id=3 locations in the log. UPDATE val=X (id=3) id=4 UPDATE val=Y (id=4) … 15-445/645 (Fall 2020)

  10. 6 LOG- STRUCTURED FILE ORGANIZATIO N Page To read a record, the DBMS scans the log backwards and "recreates" the id=1,val=a id=2,val=b tuple to find what it needs. id=3,val=X id=4,val=Y Build indexes to allow it to jump to locations in the log. Periodically compact the log. 15-445/645 (Fall 2020)

  11. 6 LOG- STRUCTURED FILE ORGANIZATIO N Page To read a record, the DBMS scans the log backwards and "recreates" the id=1,val=a id=2,val=b tuple to find what it needs. id=3,val=X id=4,val=Y Build indexes to allow it to jump to locations in the log. Periodically compact the log. 15-445/645 (Fall 2020)

  12. 7 LOG- STRUCTURED COM PACTIO N Compaction coalesces larger log files into smaller files by removing unnecessary records. Level Compaction Sorted Sorted Compaction Level 0 Log File Log File 15-445/645 (Fall 2020)

  13. 7 LOG- STRUCTURED COM PACTIO N Compaction coalesces larger log files into smaller files by removing unnecessary records. Level Compaction Compaction Level 0 Level 1 Sorted Log File 15-445/645 (Fall 2020)

  14. 7 LOG- STRUCTURED COM PACTIO N Compaction coalesces larger log files into smaller files by removing unnecessary records. Level Compaction Sorted Sorted Compaction Level 0 Log File Log File Level 1 Sorted Log File Sorted Log File Level 2 Sorted Log File 15-445/645 (Fall 2020)

  15. 7 LOG- STRUCTURED COM PACTIO N Compaction coalesces larger log files into smaller files by removing unnecessary records. Level Compaction Universal Compaction Sorted Sorted Sorted Sorted Sorted Sorted Compaction Level 0 Log File Log File Log File Log File Log File Log File Level 1 Sorted Log File Sorted Log File Sorted Sorted Sorted Log File Log File Log File Level 2 Sorted Log File 15-445/645 (Fall 2020)

  16. 8 TODAY'S AGENDA Data Representation System Catalogs Storage Models 15-445/645 (Fall 2020)

  17. 9 TUPLE STORAGE A tuple is essentially a sequence of bytes. It's the job of the DBMS to interpret those bytes into attribute types and values. The DBMS's catalogs contain the schema information about tables that the system uses to figure out the tuple's layout. 15-445/645 (Fall 2020)

  18. 10 DATA REPRESENTATIO N INTEGER / BIGINT / SMALLINT / TINYINT → C/C++ Representation FLOAT / REAL vs. NUMERIC / DECIMAL → IEEE-754 Standard / Fixed-point Decimals VARCHAR / VARBINARY / TEXT / BLOB → Header with length, followed by data bytes. TIME / DATE / TIMESTAMP → 32/64-bit integer of (micro)seconds since Unix epoch 15-445/645 (Fall 2020)

  19. 11 VARIABLE PRECISIO N NUM BERS Inexact, variable-precision numeric type that uses the "native" C/C++ types. → Examples: FLOAT , REAL / DOUBLE Store directly as specified by IEEE-754 . Typically faster than arbitrary precision numbers but can have rounding errors… 15-445/645 (Fall 2020)

  20. 12 VARIABLE PRECISIO N NUM BERS Rounding Example Output #include <stdio.h> x+y = 0.300000 0.3 = 0.300000 int main( int argc, char * argv[]) { float x = 0.1; float y = 0.2; printf("x+y = %f\n", x+y); printf("0.3 = %f\n", 0.3); } 15-445/645 (Fall 2020)

  21. 12 VARIABLE PRECISIO N NUM BERS Rounding Example Output #include <stdio.h> x+y = 0.300000 0.3 = 0.300000 #include <stdio.h> int main( int argc, char * argv[]) { float x = 0.1; x+y = 0.30000001192092895508 int main( int argc, char * argv[]) { float y = 0.2; 0.3 = 0.29999999999999998890 float x = 0.1; printf("x+y = %f\n", x+y); float y = 0.2; printf("0.3 = %f\n", 0.3); printf("x+y = %.20f \n", x+y); } printf("0.3 = %.20f \n", 0.3); } 15-445/645 (Fall 2020)

  22. 13 FIXED PRECISIO N NUM BERS Numeric data types with (potentially) arbitrary precision and scale. Used when rounding errors are unacceptable. → Example: NUMERIC , DECIMAL Many different implementations. → Example: Store in an exact, variable-length binary representation with additional meta-data. → Can be less expensive if you give up arbitrary precision. Demo: Postgres, MySQL, SQL Server, Oracle 15-445/645 (Fall 2020)

  23. 14 POSTGRES: NUM ERIC # of Digits typedef unsigned char NumericDigit; Weight of 1 st Digit typedef struct { int ndigits; Scale Factor int weight; int scale; Positive/Negative/NaN int sign; NumericDigit *digits; Digit Storage } numeric; 15-445/645 (Fall 2020)

  24. 14 POSTGRES: NUM ERIC # of Digits typedef unsigned char NumericDigit; Weight of 1 st Digit typedef struct { int ndigits; Scale Factor int weight; int scale; Positive/Negative/NaN int sign; NumericDigit *digits; Digit Storage } numeric; 15-445/645 (Fall 2020)

  25. 15 MYSQ L: NUM ERIC # of Digits Before Point typedef int32 decimal_digit_t; # of Digits After Point struct decimal_t { int intg, frac, len; Length (Bytes) bool sign; decimal_digit_t *buf; Positive/Negative }; Digit Storage 15-445/645 (Fall 2020)

  26. 15 MYSQ L: NUM ERIC # of Digits Before Point typedef int32 decimal_digit_t; # of Digits After Point struct decimal_t { int intg, frac, len; Length (Bytes) bool sign; decimal_digit_t *buf; Positive/Negative }; Digit Storage 15-445/645 (Fall 2020)

  27. 16 LARGE VALUES Tuple Most DBMSs don't allow a tuple to Header a b c d e exceed the size of a single page. To store values that are larger than a page, the DBMS uses separate Overflow Page overflow storage pages. VARCHAR DATA → Postgres: TOAST (>2KB) → MySQL: Overflow (>½ size of page) → SQL Server: Overflow (>size of page) 15-445/645 (Fall 2020)

  28. 17 EXTERNAL VALUE STORAGE Tuple Some systems allow you to store a Header a b c d e really large value in an external file. Treated as a BLOB type. → Oracle: BFILE data type → Microsoft: FILESTREAM data type External File The DBMS cannot manipulate the contents of an external file. Data → No durability protections. → No transaction protections. 15-445/645 (Fall 2020)

  29. 17 EXTERNAL VALUE STORAGE Tuple Some systems allow you to store a Header a b c d e really large value in an external file. Treated as a BLOB type. → Oracle: BFILE data type → Microsoft: FILESTREAM data type External File The DBMS cannot manipulate the contents of an external file. Data → No durability protections. → No transaction protections. 15-445/645 (Fall 2020)

  30. 18 SYSTEM CATALOGS A DBMS stores meta-data about databases in its internal catalogs. → Tables, columns, indexes, views → Users, permissions → Internal statistics Almost every DBMS stores databases' catalogs in another database. → Wrap object abstraction around tuples. → Specialized code for "bootstrapping" catalog tables. 15-445/645 (Fall 2020)

Recommend


More recommend