Database Storage Layer

The storage layer is the bottom layer of a DBMS. Search engines are simpler, using OS files for storage, with no concurrency / recovery.

Why do we not leave query execution to the OS? The OS may get in the way of the DBMS. The DBMS does things its own way - specialized prefetching, control over buffer replacement policy, process scheduling and flushing to disk.

1. Disks & Files

DBMS store information on disks. R/W between disk & ram is expensive, so must be planned carefuly by the DBMS. RAM is too expensive & volatile for bulks storage. Flash storage is also expensive, usually used for caching.

Disks store data in pages or blocks. The time to read a page is relative to its location. The access time is given by :

Blocks in a file should be arranged sequentially to minimize and - which then enables prefetching optimisation.

Defragmentation rearranges blocks in a file to be sequential to improve performance.

The lowest layer of the DBMS is the disk space manager. It provides an API to allocate, deallocate, read and write pages.

RAID can also be used for higher throughput with striping and longer MTTF with redundancy.

2. Main Memory Indexing

Indexing is a technique to speed up queries by creating a data structure that allows for faster lookups.

Many databases can fit in main memory, but memory access time becomes the new bottleneck. Cache performance is now crucial - but the DBMS can't control this. Cache behaviour is improved by:

2.1 Cache Conscious Indexing

Index trees a similar to a B+ tree, where each node is a page.

ASIDE: B+ Trees

invert center Small screen

A B+ tree is balanced, keeping all leaves sorted. It also has pointers between the leaves allowing range query. Insertion is , where is the degree of the tree.

A cache sensitive search tree is similar to a B+ tree, used to improve locality. Each node is fit into a L2 cache line (as L1 is too small). Fan-out is increased to reduce tree height. Child pointers are eliminated, and instead the children are stored in a contiguous array. This allows for binary search on the children, which is faster than following pointers.

invert center Small screen

This is an optimised structure for memory, reducing the number of cache misses. CSS balances search performance with memory usage. However, it is not dynamic - as fan-out and array-size are fixed.

A CSB+ tree has children of the same node stored in their own array, with each parent having a single pointer to the child array. This allows for dynamic fan-out, keeping the CSS search performance. Good update performance if there is no split.

A CSB+S is a CSB+ tree with segments: dividing child array into segments with one pointer per segment. Better split performance, but worse search performance.

A Full CSB+ tree has pre-allocated child arrays.

2.2 Cache Conscious Joining

There are many types of joins:

A Radix Join splits the data recursively until it fits into a cache line, based on the last few bits of the hash value. Then, matching partitions are joined using a nested-loop for small partitions or a hash join for larger partitions.

This avoids cache and TLB thrashing, beating conventional join methods.

2.3 Designing Good Algorithms

Cache performance is very important. Imrpove locality by clustering data into a cache line, leaving out irrelevant data (e.g. pointer elimation) or partitioning to avoid thrashing.

3. Main Memory DBs

OLAP - massive amounts of data, complex queries, large number of tables, long running, read heavy. OLTP - only transactions, few tables touched, generated queries.

Nowadays, even a big DB can fit into main memory. 5% of the CPU cycles are spent on useful work in DBMS, the rest is recovery, locking, buffer pool (cache management), latching (synchronization), etc.


So, to go faster, focus on the overhead. Main memory deployment gets rid of the buffer pool.

3.1 VoltDB

Main memory, single threaded database for high availabilty & durability. K-safety for fault tolerence, autonomous partitions, no latching, locking or logging. Scales horizontally.

3.1.1 Partitioning

Each physical CPU core has one partition of the data. Tables can be either partitioned (by a partition column, for transactional data), or replicated (for static data). Code can be single partition (all ops within a single partition, most transactional workload) or multi-partition (CRUD across multiple partitions & modificateions on replicated tables). Each partition has the execution engine & the data. Requests are executed sequentially on a single thread.

3.1.2 Compiling Transactions

The schema (DDL), workload (Java stored procedures) and project (users, groups, partitioning) are compiled into an application catalog. All access is via Java stored procedures. Every invocation is a transaction. High performance clients communicate asynchronously with the DB.

3.1.3 Durability

VoltDB uses K-Safety for redundancy, with snapshots and spooling (to data warehouse).

4. SSDs

Random reads are equally as fast as sequential reads, but random writes are slow. Data organized into pages into flash blocks. Time to retrieve unrelated to location. Capacity is more expensive than HDDs.

Internally, its an array of flash chips, with internal parallelism in read/write. However, we can only read or write a page at a time, and can only erase a block at a time. SSDs have a limited number of writes per block, so they use wear leveling to avoid wearing out blocks.

4.1 Flash OLTP

OLTP is dominated by random access - faster on flash. However, random writes are slow, unpredictable and wear out the flash. So, we need to batch writes and write sequentially. We then mark the old pages as invalid, and later reclaim them.

4.2 Flash OLAP

To combine scans with scattered updates, we can use:

We coan use SSDs as a write cache for HDDs, to get the best of both worlds. Materialized Sort Merge (MaSM) is a technique to combine the two approaches. It uses a merge tree to store updates, and then merges them with the main data in the background. This allows for fast queries, while still allowing for updates. The index can be coarse-grained to reduce the number of merges, and can be fine-grained to reduce the number of reads.

4.3 Flash Logging

Transactional logging is a major bottleneck for OLTP. We can use flash logging to speed this up. This is a technique to write logs to flash, and then later merge them with the main data. This allows for fast writes, while still allowing for recovery.

Back to Home