T08 Databases and Optimizing Storage 1
T08 Databases and Optimizing Storage 1
Mainack Mondal
Sandip Chakraborty
CS60203
Autumn 2024
Today’s class
- Why care about a database?
- Components of a database
- Compute (query) and Storage
- Performance Pitfalls
- Doing unnecessary work- ORMs, Absent or bad indexes, Layout
- Read/Write amplification
Why care about a database?
Why use a database? And not the file system?
“I want to store data to the disk”
- use a file system
If so, go right ahead and use plain old files! Else use
a database that does
Note, this is not a rhetoric, many data systems (eg, Kafka,
Hadoop), use files extensively for most/all work
The case for the database: Using the data
Data has meaning: how you use data can help you optimize
- Imagine that you are building a payments app, you store every
transaction in some storage system
- Need: average of amounts of all the transfers from India to Canada
What do you think is the best way to store this data on the disk?
Do you think it’ll be good to store all the amounts together or maybe all
the data of a transaction together?
The operations you perform on stored data
should determine how it is stored!
Today’s class
- Why care about a database?
- Components of a database
- Compute (query) and Storage
- Performance Pitfalls
- Doing unnecessary work- ORMs, Absent or bad indexes, Layout
- Read/Write amplification
Components of a database
What is a database?
Client Interface
Compute Optimizer
Query Execution
Storage
Interface
- Components of a database
- Compute (query) and Storage
- Performance Pitfalls
- Doing unnecessary work- ORMs, Absent or bad indexes, Layout
- Read/Write amplification
Database Internals
The OLAP/OLTP Classification
- Database implementation depends the application it is geared
towards (i.e. the kind of guarantees, and queries to be supported)
- storage and compute are implemented (and optimized) for the same
template0 is used for cases like restoring data from a logical backup or
creating a database with a different encoding; it must never be modified
template1 serves as a template for all the other databases that a user can
create in the cluster
postgres is a regular database that you can use at your discretion
PostgreSQL: System Catalogs
- Metadata of all cluster objects (such as tables, indexes, data types, or
functions) is stored in tables that belong to the system catalog.
- Each database has its own set of tables (and views) that describe the
objects of this database.
- Names of all system catalog tables begin with pg_, like in pg_database
public is the default schema for user objects unless other settings are
specified.
- A tablespace is implemented as a
directory in a file system.
- It means that logical structure and physical data layout do not depend on
each other.
- Each database has the so-called default tablespace. All database objects
are created in this tablespace unless another location is specified. System
catalog objects related to this database are also stored there.
- Each table and index has a free space map, which stores information
about free space available in the relation. The free space map is stored in a
file named with the filenode number plus the suffix _fsm
- Tables also have a visibility map, stored in a fork with the suffix _vm, to
track which pages are known to have no dead tuples. The visibility map
- TOAST Strategies:
- Move long attribute values into a separate service table, having sliced
them into smaller “toasts.”
- compress a long value in such a way that the row fits the page.
- Or you can do both: first compress the value, and then slice and move
TOAST and Potential Pitfalls
- If the main table contains potentially long attributes, a separate table is
created for it right away, one for all the attributes.
- For example, if a table has a column of the numeric or text type, a table
will be created even if this column will never store any long values.
- For indexes, the mechanism can offer only compression; moving long
attributes into a separate table is not supported. It limits the size of the
keys that can be indexed
- Simplest way to review the used strategies is to run the \d+ command
in psql
Page level layout
The first table
details the page
layout
- Not good for scanning large portions of the table and/or a subset of
the attributes (eg, aggregating a column).
- File sizes are larger (100s of MBs), but it may still organize tuples
within the file into smaller groups.
Decomposition Storage Model: Physical Layout
Advantages
- Reduces the amount wasted I/O per query because the DBMS
only reads the data that it needs.
- Faster query processing because of increased locality and cached
data reuse.
- Better data compression (more on this later)
Disadvantages
- Slow for point queries, inserts, updates, and deletes because of
tuple splitting/stitching/reorganization.
Just for reference: PAX Storage Model
- Transparent Huge-pages
- Numeric representation
- NULL representation
- OLAP Indexes
Huge-pages: Motivation
- Transparent Huge-pages
- Numeric representation
- NULL representation
- OLAP Indexes
Numeric representation: Variable Precision
- These types are typically faster than fixed precision numbers because
CPU ISA's (Xeon, Arm) have instructions / registers to support them.
But they do not guarantee exact values
Numeric representation:
Fixed Precision
Arbitrary precision is inefficient!
Definition of Postgres numeric is below
To the side you see (part of) code to add 2
Postgres numerics!
Numeric representation: Fixed Precision
- Transparent Huge-pages
- Numeric representation
- NULL representation
- OLAP Indexes
Representing NULLs!