DB Storage3
DB Storage3
                                                          OLAP
                                                  HTAP
                                      OLTP
                        Simple
                                 Write-Heavy                    Read-Heavy
• Ideal for OLTP workloads where queries are more likely to access individual
entities and execute write-heavy workloads.
                                          Database Page
                                                          header
                                                                     header a0      b0     c0
                       NSM: OLTP EXAMPLE
          SELECT * FROM useracct
           WHERE userName = ?                                                      Lecture #8
             AND userPass = ?
                                                                   Index
header - - - - -
Dis#
                       NSM: OLTP EXAMPLE
          SELECT * FROM useracct
           WHERE userName = ?                                                        Lecture #8
             AND userPass = ?
                                                                    Index
          INSERT INTO useracct
          VALUES (?,?,…?)
                                 N/M Dis# Page
                                   header    userID userName userPass hostname    lastLogin
                                   header
                                    header   user
                                                - ID userName
                                                         -    userPass
                                                                  -    hostname
                                                                           -      lastLogin
                                                                                      -
Dis#
                              NSM: OLAP EXAMPLE
                       SELECT COUNT(U.lastLogin),
                              EXTRACT(month FROM U.lastLogin) AS month
                         FROM useracct AS U
                        WHERE U.hostname LIKE '%.gov'
                        GROUP BY EXTRACT(month FROM U.lastLogin)
Dis#
                              NSM: OLAP EXAMPLE
                       SELECT COUNT(U.lastLogin),
                              EXTRACT(month FROM U.lastLogin) AS month
                         FROM useracct AS U
                        WHERE U.hostname LIKE '%.gov'
                        GROUP BY EXTRACT(month FROM U.lastLogin)
Dis#
                              NSM: OLAP EXAMPLE
                       SELECT COUNT(U.lastLogin),
                              EXTRACT(month FROM U.lastLogin) AS month
                         FROM useracct AS U
                        WHERE U.hostname LIKE '%.gov'
                        GROUP BY EXTRACT(month FROM U.lastLogin)
• Disadvantages
• → Not good for scanning large portions of the table and/or
  • a subset of the attributes.
• → Terrible memory locality in access patterns.
• → Not ideal for compression because of multiple value
 • domains within a single page.
DECOMPOSITION STORAGE MODEL (DSM)
   The DBMS stores a single attribute for all tuples
   contiguously in a block of data.
   → Also known as a "column store"
                                            File #1
Maintain a separate file per attribute                a0 a1 a2     a3 a4 a5
                                            File #2
data about entire column.                             b0 b1 b2     b3 b4 b5
                                            File #3
                                                      c0     c1     c2      c3    c4
                                                      c5
     DSM: DATABASE EXAMPLE
The DBMS stores the values of a single attribute
across multiple tuples contiguously in a page.
→ Also known as a "column store".
Dis#
                              DSM: OLAP EXAMPL E
                       SELECT COUNT(U.lastLogin),
                              EXTRACT(month FROM U.lastLogin) AS month
                         FROM useracct AS U
                        WHERE U.hostname LIKE '%.gov'
                        GROUP BY EXTRACT(month FROM U.lastLogin)
Dis#
                              DSM: OLAP EXAMPL E
                       SELECT COUNT(U.lastLogin),
                              EXTRACT(month FROM U.lastLogin) AS month
                         FROM useracct AS U
                        WHERE U.hostname LIKE '%.gov'
                        GROUP BY EXTRACT(month FROM U.lastLogin)
Dis#
         DSM: TUPLE IDENTIFICATION
     Choice #1: Fixed-length OFFsets
     → Each value is the same length for an attribute.
     Choice #2: Embedded Tuple Ids
     → Each value is stored with its tuple id in a column.
Offsets                                EmbeddedIds
     A    B    C    D                        A       B       C       D
 0                                       0       0       0       0
 1                                       1       1       1       1
 2                                       2       2       2       2
 3                                       3       3       3       3
 DSM: VARIABLE -LENGTH DATA
Padding variable-length fields to ensure they are
fixed-length is wasteful, especially for large
attributes.
• Disadvantages
• → Slow for point queries, inserts, updates, and deletes because of
   tuple splitting/stitching/reorganization.
                         OBSERVATION
• OLAP queries almost never access a single column in a table by itself.
• → At some point during query execution, the DBMS must
  • get other columns and stitch the original tuple back together.
• But we still need to store data in a columnar
• format to get the storage + execution benefits.
                                                                                             Row GYoup
  immutable (Parquet, Orc).                                a0 a1 a2       b0    b1    b2
                                                PAX File
                                                            c0     c1     c2
                                                                                             Row Group
  immutable (Parquet, Orc).                                a0 a1 a2       b0    b1    b2
                                                PAX File
                                                            c0     c1     c2
                                                                                             Row Group
meta-data header about its contents.                       a3 a4 a5       b3    b4    b5
                                                            c3     c4     c5
                         OBSERVATION
• I/O is the main bottleneck if the DBMS fetches data from disk during
query execution.
• The DBMS can compress pages to increase the utility of the data
moved per I/O operation.
• Considerations
• → Computational overhead
• → Compress vs. decompress speed.
        MYSQL INNODB COMPRESSION
          Buffer Pool               Database File
Write        mod log                   mod log
          Compressed Page0          Compressed Page0      [1,2,4,8] KB
 Read
                                       mod log
Read      Uncompressed
                             16KB   Compressed Page1
             Page0
                                       mod log
                                    Compressed Page2
                           Database Magic!
SELECT * FROM users                          SELECT * FROM users
 WHERE name = 'Andy'                          WHERE name = XX
value. 56 00111000
92 01011100
                                                 120        01111000
multiple values in a single word.                231        11100111
172 10101100
                                                        Compressed:
                                                        8 × 8-bits =
                                                        64 bits
                                        MOST LY ENCODING
                        A variation of bit packing for when an attribute's
                        values are "mostly" less than the largest size, store
                        them with smaller data type.
                        → The remaining values that cannot be compressed are
                          stored in their raw form.
                                 Original Data       Compressed Data
                                       int32           mostly8   offset    value
          Original:                       13              13       3      99999999   Compressed:
          8 × 32-bits =                  191             181                         (8 × 8-bits) +
                                      99999999           XXX                         16-bits + 32-bits
          256 bits                        92              92
                                          81              81                         = 112 bits
                                         120             120
                                         231             231
                                         172             172
Source: Redshift Documentation
            BITMAP ENCODING
Store a separate bitmap for each unique value for
an attribute where an offset in the vector
corresponds to a tuple.
→ The ith position in the Bitmap corresponds to the ith tuple
  in the table.
→ Typically segmented into chunks to avoid allocating large
  blocks of contiguous memory.
3 Y 2 1 0
      4      N
                    Original:             3    1   0
                    9 × 8-bits =          4    0   1    9 × 2-bits =
      6      Y
                    72 bits               6    1   0    18 bits
      7      N
      8      Y                            7    0   1
      9      Y                            8    1   0
                                          9    1   0
          BITMAP ENCODING: EXAMPLE
Assume we have 10 million tuples.
43,000 zip codes in the US.
→ 10000000 × 32-bits = 40 MB           CREATE TABLE customer (
→ 10000000 × 43000 = 53.75 GB             id INT PRIMARY KEY,
                                          name VARCHAR(32),
                                          email VARCHAR(64),
Every time the application inserts a      address VARCHAR(64),
new tuple, the DBMS must extend           zip_code INT
43,000 different bitmaps.              );
                  DELTA ENCODING
    Recording the difference between values that
    follow each other in the same column.
    → Store base value in-line or in a separate look-up table.
                                                           Dictionary
                 name         name        value     code
                Andrea         10        Andrea      10
               Prashanth       20       Prashanth    20
                  Andy         30          Andy      30
                  Matt         40          Matt      40
               Prashanth       20
DICTIONARY: ENCODING / DECODING
  A dictionary needs to support two operations:
  → Encode/Locate: For a given uncompressed value,
    convert it into its compressed form.
  → Decode/Extract: For a given compressed value, convert
    it back into its original form.
      DICTIONARY: ORDER -PRESERVING
        The encoded values need to support the same
        collation as the original values.
                                                                  Dictionary
                Andrea                10        Andrea      10
                                                                    sorted
               Prashanth              40          Andy      20
                  Andy                20          Matt      30
                  Matt                30       Prashanth    40
               Prashanth              40
          ORDER- PRESERVING ENCODING
SELECT name FROM users       Still must perform scan on column
 WHERE name LIKE 'And%'
                                                                Dictionary
                  Andrea            10        Andrea      10
                                                                  sorted
                 Prashanth          40          Andy      20
                    Andy            20          Matt      30
                    Matt            30       Prashanth    40
                 Prashanth          40
DICTIONARY: DATA STRUCTURES
Choice #1:Array
→ One array of variable length strings and another array
  with pointers that maps to string offsets.
→ Expensive to update so only usable in immutable files.