Database Files
Database Files
Chapter 1 – DBMS
DATA STORAGE
DISKS, BUFFERS, FILES
COMP7104-DASC7104 1
Recall: traditional RDBMS
Structured data (relational), centralized, disk-based, OLTP workloads, ACID
COMP7104 - DASC7104 2
Recall: RDBMS anatomy
SQL Client
Completed
Query Parsing
& Optimization
We will unpack a data system
Relational Operators
and explored modular system
design. Files and Index Management
Database
Management
Buffer Management
System
Disk Space Management
Database
COMP7104-DASC7104 4
Example schema from the cow book
• Sailors (sid: integer, sname: string, rating: integer, age: real)
• Boats (bid: integer, bname: string, color:string)
• Reserves (sid: integer, bid: integer, day: dates, rname: string)
COMP7104-DASC7104 5
Architecture of a RDBMS
Parse, check (syntax), and verify the query
SQL Client
Indexed Join
Indexed Join
RDBMS
Heap Scan Indexed Scan
Reserves Sailors
Database
COMP7104-DASC7104 7
Architecture of a DBMS
Organizing tables and records as
groups of pages in a logical file SQL Client
SSN Last First Age Salary
Name Name
Page
Files and Index Management
Header
RDBMS
Page Page
Header Header
Database
COMP7104-DASC7104 8
Architecture of a DBMS
Illusion of operating in main memory
SQL Client
Relational Operators
RAM
Database
Files and Index Management
Management
Buffer Management
RDBMS
Disk Space Management
Database
COMP7104-DASC7104 9
Architecture of a DBMS
Translating page requests into physical
SQL Client
addresses on disk(s)
Query Parsing
& Optimization
Block 1 Block 2 Block 3 Relational Operators
Database
COMP7104-DASC7104 10
Recall: Abstraction at each level
Query Parsing
& Optimization
What à How
COMP7104 - DASC7104 11
Architecture of a DBMS
• Organized in layers SQL Client
Database
COMP7104-DASC7104 12
Architecture of a DBMS
• Two crucial issues related to SQL Client
storage and memory
management in RDBMS
Query Parsing
& Optimization
Relational Operators
Database
COMP7104-DASC7104 13
Architecture of a DBMS
Completed SQL Client
Completed
Query Parsing
& Optimization
Relational Operators
COMP7104-DASC7104 15
Overview: representations
Record SSN Last First Age Salary
Name Name
Header M
3
2 Bob Harmon
Page 1 Page 2
Slotted Page
Page
Header Page 3 Page 4
Page 5 Page 6
COMP7104-DASC7104 16
Files of pages of records
• Tables stored as logical files consisting of pages each containing a
collection of records
• Pages are managed
– on disk by the disk space manager: pages read / written to physical
disk/files
• subject of today’s lecture
– in memory by the buffer manager: higher levels of DBMS only
operate in memory
• coming soon
• Main ideas in this lecture
– Block / page – granularity of reasoning (because of disk!)
– Exploit access patterns in memory management
– Efficient binary representations of data
COMP7104-DASC7104 17
Page Page
Header Header
Page Page
Header Header
Page Page
Header Header
COMP7104-DASC7104 18
DISK SPACE MANAGEMENT
COMP7104-DASC7104 19
Disks and files
• Yes, most DBMS stores information on hard disks and SSDs.
– Recall: hard disk are a mechanical anachronism (slow!)
– Recall: SSDs faster, still slow relative to main memory, costly writes
COMP7104-DASC7104 20
Note on terminology
• Block = Unit of transfer for disk read/write
– 64KB – 128KB is a good number today
– Cow book says 4KB
COMP7104-DASC7104 21
Arranging blocks on disk
• Next block concept (for locality):
– sequential blocks on same track, followed by
– blocks on same cylinder, followed by
– blocks on adjacent cylinder
Arm assembly
COMP7104-DASC7104 22
Disk space management SQL Client
Query Parsing
• Lowest layer of DBMS, manages space on disk & Optimization
Relational Operators
– Mapping pages to locations on disk
– Loading pages from disk to memory Files and Index Management
Database
Management
– Saving pages back to disk & ensuring writes Buffer Management
System
Disk Space Management
• Higher levels call upon this layer to: You are here
Database
– Read/write a page
– Allocate/de-allocate logical pages
A B C D E
24
COMP7104-DASC7104
Disk space management: implementation
COMP7104-DASC7104 25
Using local filesystem
Get Page 4 Get Page 5
COMP7104-DASC7104 26
Disk space management
• Provide API to read and
write pages to device
Page 5 Page 6
COMP7104-DASC7104 27
DATABASE FILES
files(pages(records))
COMP7104-DASC7104 28
Files of pages of records
files(pages(records))
• Higher levels of DBMS operate on pages of records and files of pages
COMP7104-DASC7104 29
Many kinds of database files
• Unordered Heap Files
– Records placed arbitrarily across pages
• Clustered Heap File and Hash Files
– Records and pages are grouped
• Sorted Files
– Pages and records are in sorted order
• Index Files
– B+ Trees, Hash Tables, …
– May contain records or point to records in other files
COMP7104-DASC7104 30
Unordered heap files
• Collection of records with no particular order
– Not to be confused with the “heap” data structure
COMP7104-DASC7104 31
Heap file implemented as list
Data Data Data Full pages
Page Page Page
Header
Page
Data
Page 2
Data
Page N
DIRECTORY
Header
Page
Data Data Data
Page Page Page Pages with
Free Space
A B C D E
COMP7104-DASC7104 34
Indexes: quick preview
• A heap file allows us to retrieve records:
– by specifying the record Id (often as page Id + offset)
– by scanning all records sequentially
COMP7104-DASC7104 35
Summary
File
Page 1 Page 2
SSNz Last First Age Salary
Name Name
Page 3 Page 4
123 Adams Elmo 31 $400
Page 5 Page 6
COMP7104-DASC7104 36
Next
File
Page 1 Page 2
SSNz Last First Age Salary
Name Name
Page 3 Page 4
123 Adams Elmo 31 $400
Page 5 Page 6
COMP7104-DASC7104 37
PAGE LAYOUT
files(pages(records))
COMP7104-DASC7104 38
Page basics: the header
Page Header
COMP7104-DASC7104 41
A note on memory illustrations
• Data is stored in linear order
– 1 byte per position
– Memory addresses are ordered
– Disk addresses are ordered
COMP7104-DASC7104 42
Fixed length records, packed
Record Id: Page Header Record
(Page 2, Record 4)
Record Record
Record Record
Record
Record Record
Record
Record Record
Record Record
Record
Record Record
Record Record
Record
Record Record
A B C D E
COMP7104-DASC7104 48
Variable length records
Page Header
Record Record
Record Record
Record
COMP7104-DASC7104 49
First: Relocate metadata to footer
Record Record
Record Record
Record
Page Footer
COMP7104-DASC7104 50
Slotted page
Record Record
Record Id:
Record Record
(Page 2, Record 4)
Record
COMP7104-DASC7104 52
Slotted page: delete record
Record Record
Record Id:
Record Record
(Page 2, Record 4)
Record
COMP7104-DASC7104 53
Slotted page: insert record
Record Record
Record Id:
Record Record
(Page 2, Record 4)
Record
• Insert:
COMP7104-DASC7104 54
Slotted page: insert record
Record Record
Record Id:
Record Record
(Page 2, Record 4)
Record Reco
rd
• Insert:
– Place record in free space on page
COMP7104-DASC7104 55
Slotted page: insert record
Record Record
Record Id:
Record Record
(Page 2, Record 4)
Record Reco
rd
• Insert:
– Place record in free space on page
– Create pointer/length pair in next open slot in slot directory
COMP7104-DASC7104 56
Slotted page: insert record
Record Record
Record Id:
Record Record
(Page 2, Record 4)
Record Reco
rd
• Insert:
– Place record in free space on page
– Create pointer/length pair in next open slot in slot directory
– Update the free space pointer
COMP7104-DASC7104 57
Record Record
Quick quizz
Record Record
Rec
Record
o
rd
Footer 18 12 42 24 16
A. Slotted pages can work for both fixed or variable length records
B. A deletion operation requires no searching on the page
C. Deleting a record can change the recordId of other records
D. All of the above
E. None of the above
A B C D E
COMP7104-DASC7104 58
Slotted page: insert record
Record Record
Record Id:
Record Record
(Page 2, Record 4)
Record Reco
rd
• Insert:
– Place record in free space on page
– Create pointer/length pair in next open slot in slot directory
– Update the free space pointer
– Fragmentation?
COMP7104-DASC7104 59
Slotted page: insert record
Record Record
Record Id:
Record Record
(Page 2, Record 4)
Record
• Insert:
– Place record in free space on page Is this safe?
– Create pointer/length pair in next open slot in slot directory
– Update the free space pointer When should I reorganize?
– Reorganize data on page. What if we need more slots?
COMP7104-DASC7104 60
Slotted page: growing slots
Record Record
Record Record
Record
COMP7104-DASC7104 61
Slotted page: growing slots
5
Header
Record 16 24 32
Record12 18
Record Record
rd Record Record
Footer
12 18Record
12 42 24 16 5 Slot directory
Record Record
rd Record Record
Footer
12 18Record
12 42 24 16 6 Slot directory
Record Record
rd Record Record
Footer
12 18Record
12 42 24 16 5 Slot directory
COMP7104-DASC7104 65
Log-structured pages
The log records complement the database file: to read a record, the
system « replays » the log backwards and recreates that record
• Use indexes to quickly scan log
• Periodically compact / coalesce records (lots of redundant ones
otherwise)
• Used in HBase, RocksDB, Cassandra
Log entry
INSERT Id=1,val=B
Read
INSERT Id=2,val=A
DELETE id=3
UPDATE val=C (id=1)
66
COMP7104-DASC7104
Quick quizz
• Which of the following is NOT TRUE for log-structured pages
A. Updates are fast
B. Reads may be slow
C. Well adapted for append-only storage
D. All of the above
E. None of the above
A B C D E
COMP7104-DASC7104 67
Record
Formats
RECORD LAYOUT
files(pages(records))
COMP7104-DASC7104 68
Record formats
• Relational model à each record in table has some fixed type
• Assume system catalog stores the schema
– No need to store type information with records (save space!)
– Catalog is just another table …
• Goals:
– Records should be compact in memory & disk format
– Fast access to fields (why?)
COMP7104-DASC7104 69
Record formats: fixed length
24
4 8 1 4 7
3 3.142 T 3 HELLO_W
BOOLEAN
• Field types same for all records in a file.
– Type info stored separately in system catalog
• Disk byte representation same as in memory
• Finding i’th field?
– done via arithmetic (fast)
COMP7104-DASC7104 70
• Compact? (Nulls?)
Record formats: variable length
What happens if fields are variable length?
Record
Bob Big, St. M 32 94703
VARCHAR VARCHAR CHAR INT INT
• Issues?
COMP7104-DASC7104 72
Record formats: variable length
What happens if fields are variable length?
Record
Bob Big, St. M 32 94703
VARCHAR VARCHAR CHAR INT INT
Header M
3
2 Bob Harmon
Page 1 Page 2
Slotted Page
Page
Header Page 3 Page 4
Page 5 Page 6
COMP7104-DASC7104 78
System catalogs
• For each relation:
– name, file location, file structure (e.g., heap file)
– attribute name and type, for each attribute
– index name, for each index
– integrity constraints
• For each index:
– structure (e.g., B+ tree) and search key fields
• For each view:
– view name and definition
• Plus statistics, authorization, buffer pool size, etc
• Page layouts
– Fixed-length packed and unpacked
– Variable length records in slotted pages, with intra-page reorg
FILE ORGANIZATIONS
COMP7104-DASC7104 82
Architecture of a DBMS
Completed SQL Client
Completed
Query Parsing
& Optimization
Relational Operators
• Find a record?
– Given a recordId: (pageId, slot)?
– Matching username = “sarahmanning”?
COMP7104-DASC7104 84
Multiple file organizations
• Clustered files & indexes: Group data into blocks to enable fast
lookup and efficient modifications. (more on this soon …)
COMP7104-DASC7104 85
Bigger questions
COMP7104-DASC7104 86
Goals for what follows
• Big picture overheads for data access
– We’ll simplify performance models to provide insight, not to get
perfect performance
– Still, a bit of discipline:
• Clearly identify assumptions up front
• Then estimate cost in a principled way
COMP7104-DASC7104 87
COST MODEL AND ANALYSIS
COMP7104-DASC7104 88
Cost model for analysis
• B: The number of data blocks in the file
• R: Number of records per block
• D: (Average) time to read/write disk block
COMP7104-DASC7104 90
Heap files & sorted files
Heap File
2, 5 1, 6 4, 7 3, 10 8, 9
Sorted File
1, 2 3, 4 5, 6 7, 8 9, 10
Equality Search
Range Search
Insert
Delete
Equality Search
Range Search
Insert
Delete
A B C
Heap File Sorted File Tie
COMP7104-DASC7104 94
Scan all records
Heap File
2, 5 1, 6 4, 7 3, 10 8, 9
Sorted File
1, 2 3, 4 5, 6 7, 8 9, 10
Equality Search
Range Search
Insert
Delete
Equality Search
Range Search
Insert
Delete
A B C
Heap File Sorted File Tie
COMP7104-DASC7104 98
Find key 8
Heap File
2, 5 1, 6 4, 7 3, 10 8, 9
2, 5 1, 6 4, 7 3, 10 8, 9
• Breaking an assumption
– What if there could be more than one copy of each value?
A B C D
B/2 log2 B sqrt(B) B
COMP7104-DASC7104 100
Find key 8
Sorted File
1, 2 3, 4 5, 6 7, 8 9, 10
COMP7104-DASC7104 101
Average case binary search
1 IO
2 IOs
3 IOs
4 IOs
Range Search
Insert
Delete
Range Search
Insert
Delete
A B C
Heap File Sorted File Tie
COMP7104-DASC7104 105
Find keys between 7 and 9
Heap File
2, 5 1, 6 4, 7 3, 10 8, 9
COMP7104-DASC7104 106
Find keys between 7 and 9
Heap File
2, 5 1, 6 4, 7 3, 10 8, 9
Sorted File
1, 2 3, 4 5, 6 7, 8 9, 10
Insert
Delete
Insert
Delete
A B C
Heap File Sorted File Tie
COMP7104-DASC7104 110
Insert 4.5
Heap File
2, 5 1, 6 4, 7 3, 10 8, 9 4.5,
COMP7104-DASC7104 111
Insert 4.5
Heap File
2, 5 1, 6 4, 7 3, 10 8, 9 4.5,
1, 2 3, 4 5, 6 7, 8 9, 10
COMP7104-DASC7104 112
Insert 4.5
Heap File
2, 5 1, 6 4, 7 3, 10 8, 9 4.5,
1, 2 3, 4 5, 6
4.5,5 7, 7
6, 8 8,10
9, 9 10, _
Delete
Delete
A B C
Heap File Sorted File Tie
COMP7104-DASC7104 116
Delete 4.5
Heap File
2, 5 1, 6 4, 7 3, 10 8, 9 4.5,
COMP7104-DASC7104 117
Delete 4.5
Heap File
2, 5 1, 6 4, 7 3, 10 8, 9 4.5,
Sorted File
1, 2 3, 4 4.5,5
__,5
5, 6 6, 8
7, 7 8,10
9, 9 10,
2, 5 1, 6 4, 7 3, 10 8, 9 4.5,
Sorted File
1, 2 3, 4 5, ,65
__ 7,
6, 8
7 9,
8,10
9 10,