Docsity

External Sorting in Database Management Systems: Minimizing Disk Access Costs - Prof. Kris, Study notes of Database Management Systems (DBMS)

Toggle sidebar
bg1
1
3/8/09 EECS 484: Database Management Systems, Kristen LeFevre 1
External Sorting
Chapter 13
3/8/09 EECS 484: Database Management Systems, Kristen LeFevre 2
Why Sort?
•User wants query answers in some order
•E.g., ORDER BY Age DESC
•First step to bulk-loading B+ Tree
•Eliminate duplicate records
•SELECT DISTINCT
•Evaluate GROUP BY aggregate queries
•Sort-merge join algorithm (later)
3/8/09 EECS 484: Database Management Systems, Kristen LeFevre 3
Why bother?
•But we already know how to sort…
•New Problem: How to sort 100 GB of data
with 1 GB RAM?
•External Sort – Minimize disk access cost
• Why not use virtual memory?
pf3
pf4
pf5

Partial preview of the text

Download External Sorting in Database Management Systems: Minimizing Disk Access Costs - Prof. Kris and more Study notes Database Management Systems (DBMS) in PDF only on Docsity!

3/8/09 EECS 484: Database Management Systems, Kristen LeFevre 1

External Sorting

Chapter 13

3/8/09 EECS 484: Database Management Systems, Kristen LeFevre 2

Why Sort?

• User wants query answers in some order

  • E.g., ORDER BY Age DESC

• First step to bulk-loading B+ Tree

• Eliminate duplicate records

  • SELECT DISTINCT

• Evaluate GROUP BY aggregate queries

• Sort-merge join algorithm (later)

3/8/09 EECS 484: Database Management Systems, Kristen LeFevre 3

Why bother?

• But we already know how to sort…

• New Problem: How to sort 100 GB of data

with 1 GB RAM?

  • External Sort – Minimize disk access cost
  • Why not use virtual memory?

3/8/09 EECS 484: Database Management Systems, Kristen LeFevre 4

Two-Way External Merge Sort

  • Pedagogical use only!
  • Requires 3 Buffer pages
  • Pass 1: Read a page, sort it, write it (a run).
    • only one buffer page is used
  • Pass 2, 3, …, etc.:
    • three buffer pages used.

Main memory buffers

INPUT 1

INPUT 2

OUTPUT

Disk Disk

3/8/09 EECS 484: Database Management Systems, Kristen LeFevre 5

Two-Way External Merge Sort

3,4 6,2 9,4 8,7 5,6 3,1 2 Input file

1-page runs

PASS 0

3,4 2,6 4,9 7,8 5,61,3 2

2-page runs

PASS 1

2,

4,

4,

8,

1,

5,6 2

4-page runs

PASS 2

2,

4,

6,

8,

1,

3,

6

8-page runs

PASS 3

9

1,

2,

3,

4,

6,

7,

  • Read & write entire

file in each pass

  • Divide and

conquer

3/8/09 EECS 484: Database Management Systems, Kristen LeFevre 6

General External Merge Sort

• Sort a file with N pages using B buffer pages:

  • Pass 0: use B buffer pages (run size = B pgs).

Produce sorted runs of B pages each.

  • Pass 2, 3, …: merge B-1 runs.

N/B

B-1 way merge.

Total buffer pages: B

INPUT 1

INPUT B-

OUTPUT

Disk Disk

INPUT 2

3/8/09 EECS 484: Database Management Systems, Kristen LeFevre 10

Replacement Sort

  • Start by reading pages from file until buffer is full
    • Maintain current set in memory
  • Repeatedly pick smallest value from current set

that is greater than largest value in output buffer

  • Write to output buffer (run)
  • Start a new run when no value in current set

larger than all values in output

  • On average, produces runs of size 2B

3/8/09 EECS 484: Database Management Systems, Kristen LeFevre 11

Replacement Sort (Example)

Input Current (3) Output

Start new run

3/8/09 EECS 484: Database Management Systems, Kristen LeFevre 12

Blocked I/Os

  • Single request to read a block of pages often cheaper

than independent requests for each page – Why?

  • Make each buffer a block of pages instead
    • Reduces cost per page I/O
    • Reduces fan-out during merge passes – Side effect?
    • First Pass: Each run 2B pages, ⌈ N/2B ⌉ runs (where B is the

size of the buffer pool in #pages)

  • Assuming we use replacement sort optimization…
  • Merge Tree Fanout: F = ⌊ B/b ⌋ - 1 , b is block size
  • passes: ⌈ **log

F**

  • In practice, buffer pools are large
    • most files still sorted in 2-3 passes

3/8/09 EECS 484: Database Management Systems, Kristen LeFevre 13

Double Buffering

  • Overlap CPU and IO processing
  • Prefetch into shadow block.
    • Potentially, more passes; in practice, 2-3 passes.

OUTPUT

OUTPUT'

Disk

Disk

INPUT 1

INPUT k

INPUT 2

INPUT 1'

INPUT 2'

INPUT k'

block size

b

B main memory buffers, k-way merge

Reduces response time.

What about throughput?

3/8/09 EECS 484: Database Management Systems, Kristen LeFevre 14

Using B+ Trees for Sorting

  • Scenario: Table to be sorted has B+ tree index on

sorting column(s).

  • Idea: Can retrieve records in order by traversing

leaf pages.

  • Is this a good idea?
  • Cases to consider:
    • B+ tree is clustered Good idea!
    • B+ tree is not clustered Could be a very bad idea!

3/8/09 EECS 484: Database Management Systems, Kristen LeFevre 15

Clustered B+ Tree Used for Sorting

  • Go to the left-most leaf,

then retrieve all leaf pages

  • Alt 1: Done!
    • pages?

  • Alt 2: Retrieving data

records, each page

fetched just once

 Faster than

external sorting!

Data Records

Index

Data Entries

Why not scan the data file directly?