Database System Internals Data Storage and (more) Buffer ......Data Storage and (more) Buffer...
Transcript of Database System Internals Data Storage and (more) Buffer ......Data Storage and (more) Buffer...
1January 13, 2020
Database System Internals
CSE 444 - Winter 2020
Data Storage and (more) Buffer Management
Announcements
§ Room temperature being looked into
§ Lab 1 part 1 is due on tonight at 11pm• Don’t worry about passing exact tests and
implementing everything as completely as possible for intermediate stage
• We are not grading according to tests-passed for part 1, just that the functions asked for are complete.
§Homework 1:• New option to submit by Gradescope pdf, or paper
copy• We will scan your paper copies into gradescope.
CSE 444 - Winter 2020 2January 13, 2020
Important Note
§ Lectures show principles§ Homeworks + Quizzes test the principles
§ You need to think through what you will actually implement in SimpleDB!
• Try to implement the simplest solutions
§ If you are confused, tell us!• Thursday section this week will be extra lab help, Q/A office
hours style
§ SimpleDB not designed to be bullet-proof software
CSE 444 - Winter 2020 3January 13, 2020
DBMS Architecture
Process Manager
Admission Control
Connection Mgr
Query Processor
Parser
Query Rewrite
Optimizer
Executor
Storage Manager
Access Methods
Lock Manager
Buffer Manager
Log Manager
Shared Utilities
Memory Mgr
Disk Space Mgr
Replication Services
Admin Utilities
[Anatomy of a Db System. J. Hellerstein & M. Stonebraker. Red Book. 4ed.]
4January 13, 2020 CSE 444 - Winter 2020
DBMS Architecture
Query Processor
Storage Manager
Access Methods: HeapFile, etc.Buffer Manager
5
Operators: Sequential Scan, etc.
Data on disk
Disk Space Mgr
January 13, 2020 CSE 444 - Winter 2020
Today: Starting at the Bottom
Consider a relation storing tweets:Tweets(tid, user, time, content)
How should we store it on disk?
CSE 444 - Winter 2020 6January 13, 2020
Design Exercise
§ One design choice: One OS file for each relation• This does not always have to be the case! (e.g., SQLite uses one file
for whole database)• DBMSs can also use disk drives directly
§ An OS file provides an API of the form• Seek to some position (or “skip” over B bytes)• Read/Write B bytes
7
Disk
FileTuple
SeekRead… but how much?
January 13, 2020 CSE 444 - Winter 2020
First Principle: Work with Pages
§ Reading/writing to/from disk• Seeking takes a long time!• Reading sequentially is fast
§ Solution: Read/write pages of data• Traditionally, a page corresponds to a disk block
§ To simplify buffer manager, want to cache a collection of same-sized objects
8
Page 0 Page 1 Page 2 Page 3
January 13, 2020 CSE 444 - Winter 2020
Empty space
Continuing our Design
Key questions:§ How do we organize pages into a file?§ How do we organize data within a page?
9
First, how could we store some tuples on a page?Let’s first assume all tuples are of the same size:
Tweets(tid int, user char(10), time int, content char(140))
January 13, 2020 CSE 444 - Winter 2020
Page Formats
Issues to consider§ 1 page = 1 disk block = fixed size (e.g. 8KB)§ Records:
• Fixed length• Variable length
§ Record id = RID• Like a pointer to a tuple• Typically RID = (PageID, SlotNumber)
Why do we need RID’s in a relational DBMS ?See future discussion on indexes and transactions
10January 13, 2020 CSE 444 - Winter 2020
Design Exercise§ Think how you would store tuples on a page• Fixed length tuples• Variable length tuples
§ Compare your solution with your neighbor’s
11January 13, 2020 CSE 444 - Winter 2020
Page Formats
Page Format Approach 1
Fixed-length records: packed representationDivide page into slots. Each slot can hold one tupleRecord ID (RID) for each tuple is (PageID,SlotNb)
Slot1 Slot2 SlotNFree space N
Number of records
12
How do we insert a new record?
CSE 444 - Winter 2020January 13, 2020
Page Format Approach 1
Fixed-length records: packed representationDivide page into slots. Each slot can hold one tupleRecord ID (RID) for each tuple is (PageID,SlotNb)
Slot1 Slot2 SlotN SlotN+1
Free Sp. N
Number of records
13
How do we insert a new record?
CSE 444 - Winter 2020January 13, 2020
Page Format Approach 1
Fixed-length records: packed representationDivide page into slots. Each slot can hold one tupleRecord ID (RID) for each tuple is (PageID,SlotNb)
Slot1 Slot2 SlotN SlotN+1
Free Sp. N
Number of records
14
How do we insert a new record?
How do we delete a record?
CSE 444 - Winter 2020January 13, 2020
Page Format Approach 1
Fixed-length records: packed representationDivide page into slots. Each slot can hold one tupleRecord ID (RID) for each tuple is (PageID,SlotNb)
Slot1 Slot2 SlotN SlotN+1
Free Sp. N
Number of records
15
How do we insert a new record?
How do we delete a record? What is the problem?
CSE 444 - Winter 2020January 13, 2020
Page Format Approach 1
Fixed-length records: packed representationDivide page into slots. Each slot can hold one tupleRecord ID (RID) for each tuple is (PageID,SlotNb)
Slot1 Slot2 SlotN SlotN+1
Free Sp. N
Number of records
16
How do we insert a new record?
How do we delete a record? Cannot move records! (Why?)
CSE 444 - Winter 2020
How do we handle variable-length records?
January 13, 2020
Page Format Approach 2
Can handle variable-length recordsCan move tuples inside a page without changing RIDsRID is (PageID, SlotID) combination
Slot directoryEach slot contains
<record offset, record length>
17
Header contains slot directory+ Need to keep track of # of slots+ Also need to keep track of free space pointer (F)
Free space 4 F
CSE 444 - Winter 2020January 13, 2020
CSE 444 - Winter 2020
Record Formats
Fixed-length records => Each field has a fixed length(i.e., it has the same length in all the records)
Field 1 Field 2 . . . . . . Field K
Information about field lengths and types is in the catalog
18January 13, 2020
CSE 444 - Winter 2020
Record Formats
Variable length records
Remark: NULLS require no space at all (why ?)
19
Field 1 Field 2 . . . . . . Field K
Record header
January 13, 2020
CSE 444 - Winter 2020
LOB
§ Large objects• Binary large object: BLOB• Character large object: CLOB
§ Supported by modern database systems§ E.g. images, sounds, texts, etc.
§ Storage: attempt to cluster blocks together
21January 13, 2020
Continuing our Design
Our key questions:§ How do we organize pages into a file?§ How do we organize data within a page?
Now, how should we group pages into files?
CSE 444 - Winter 2020 22January 13, 2020
Heap File Implementation 1
CSE 444 - Winter 2020 23
Data page
Data page
Data page
Data page
Data page
Data page
Data page
Data page
A sequence of pages (implementation in SimpleDB)
Some pages have space and other pages are fullAdd pages at the end when need more space
Works well for small filesBut finding free space requires scanning the file…
January 13, 2020
Heap File Implementation 2
Headerpage
Data page
Data page
Data page
Data page
Data page
Data page
Linked list of pages:Data page
Data page
Full pages
Pages with some free space
24January 13, 2020 CSE 444 - Winter 2020
Heap File Implementation 3
Data page
Data page
Data page
Better: directory of pages
Directory
Header page
Directory contains free-space count for each page.Faster inserts for variable-length records
25January 13, 2020 CSE 444 - Winter 2020
CSE 444 - Winter 2020
Modifications: Insert Tuple
§ File is unsorted (= heap file)• add it wherever there is space (easy J)• add more pages if out of space
§ File is sorted• Is there space on the right page ?
• Yes: we are lucky, store it there• Is there space in a neighboring page ?
• Look 1-2 pages to the left/right, shift records• If anything else fails, create overflow page
26January 13, 2020
CSE 444 - Winter 2020
Overflow Pages
§After a while the file starts being dominated by overflow pages: time to reorganize
Pagen-1 Pagen Pagen+1
Overflow
27January 13, 2020
CSE 444 - Winter 2020
Modifications: Deletions
§ Free space by shifting records within page• Be careful with slots• RIDs for remaining tuples must NOT change
§May be able to eliminate an overflow page
28January 13, 2020
CSE 444 - Winter 2020
Modifications: Updates
§ If new record is shorter than previous, easy J§ If it is longer, need to shift records
• May have to create overflow pages
29January 13, 2020
Continuing our Design
We know how to store tuples on disk in a heap file
How do these files interact with rest of engine?§ Let’s look back at lecture 3
CSE 444 - Winter 2020 30January 13, 2020
How Components Fit Together
Query Processor
Storage Manager
Access Methods: HeapFile, etc.Buffer Manager
31
Operators: Sequential Scan, etc.
Data on disk
Disk Space Mgr
Operators view relations as collections of records
The access methods worry about how to organize these collections
January 13, 2020 CSE 444 - Winter 2020
Heap File Access Method API
§Create or destroy a file§ Insert a record§Delete a record with a given rid (rid)
• rid: unique tuple identifier (more later)§Get a record with a given rid
• Not necessary for sequential scan operator• But used with indexes (more next lecture)
§Scan all records in the file
CSE 444 - Winter 2020 32January 13, 2020
Suppliers Supplies
sno = sno
𝜎 sscity=‘Seattle’ ⋀ sstate=‘WA’ ⋀ pno=2
𝛑sname
(File scan) (File scan)
(Nested loop)
(On the fly)
(On the fly)
33
next()
next()
next()
next() next()
Query ExecutionHow it all Fits Together
next()
January 13, 2020 CSE 444 - Winter 2020
34
Query Execution In SimpleDB
SeqScan Operator at bottom of plan
Heap File Access Method
In SimpleDB, SeqScan can find HeapFile in Catalog
open()
open()
Offers iterator interfaceopen()next()close()Knows how to read/write pages from disk
next()
next()
But if Heap File reads data directly from disk, it will not stay cached in Buffer Pool!
CSE 444 - Winter 2020January 13, 2020
HeapFile
BufferPool
Manager
35
Data on disk: OS Files
Iterator interfaceopen()next()close()Read/write pages from disk
Query Execution In SimpleDBEveryone sharesa single cache
HeapFile2
HeapFile3
HeapFileN
Heap files for other relations
getPage()
readPage()
January 13, 2020 CSE 444 - Winter 2020
Buffer Manager
§ Brings pages in from memory and caches them§ Eviction policies
• Random page (ok for SimpleDB)• Least-recently used• The “clock” algorithm
§Keeps track of which pages are dirty• A dirty page has changes not reflected on disk• Implementation: Each page includes a dirty bit
36CSE 444 - Winter 2020January 13, 2020
Buffer Manager
CSE 444 - Winter 2020 37
Disk
Mainmemory
Page requests from higher-level code
Buffer pool
Disk page
Free frame
1 page correspondsto 1 disk block
Disk is a collectionof blocks
Buffer pool managerAccess methods
January 13, 2020
Pushing Updates to Disk
§ When inserting a tuple, HeapFile inserts it on a page but does not write the page to disk
§ When deleting a tuple, HeapFile deletes tuple from a page but does not write the page to disk
§ The buffer manager worries when to write pages to disk (and when to read them from disk)
§ When need to add new page to file, HeapFile adds page to file on disk and then reads it through buffer manager
CSE 444 - Winter 202038January 13, 2020
Alternate Design: Column Store
39
Rows stored contiguously on disk
(+ tuple headers)
Columns stored contiguously on disk(no headers needed)
January 13, 2020 CSE 444 - Winter 2020
Column Store Illustration
40
Row-based(4 pages)
A 1A 2A 2A 2
Page
C 4C 4
B 2B 4
Column-based(4 pages)
AAA
1
A
2
PageC
2
C
444
B
2
B
2
C-Store alsoavoids largetuple headers
CSE 444 - Winter 2020January 13, 2020
Column Store Example
CSE 444 - Winter 2020 41
The Design and Implementation of Modern Column-Oriented Database Systems Daniel Abadi, Peter Boncz, Stavros Harizopoulos, Stratos Idreos, Samuel Madden. Foundations and Trends® in Databases (Vol 5, Issue 3, 2012, pp 197-280)
January 13, 2020
Conclusion
§ Row-store storage managers are most commonly used today for OLTP systems
§ They offer high-performance for transactions§ But column-stores win for analytical workloads§ They are widely used in OLAP
§ [Optional] Final discussion: OS vs DBMS• OS files vs DBMS files• OS buffer manager vs DBMS buffer manager
CSE 444 - Winter 2020 42January 13, 2020