版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、External Sorting“There it was, hidden in alphabetical order.”Rita Holt R&G Chapter 13Why Sort? A classic problem in computer science! Data requested in sorted order e.g., find students in increasing gpa order Sorting is first step in bulk loading B+ tree index. Sorting useful for eliminating dup
2、licate copies in a collection of records (Why?) Sorting is useful for summarizing related groups of tuplesSort-merge join algorithm involves sorting. Problem: sort 100Gb of data with 1Gb of RAM.why not virtual memory?Streaming Data Through RAMAn important method for sorting & other DB operations
3、Simple case: Compute f(x) for each record, write out the result Read a page from INPUT to Input Buffer Write f(x) for each item to Output Buffer When Input Buffer is consumed, read another page When Output Buffer fills, write it to OUTPUTReads and Writes are not coordinated E.g., if f() is Compress(
4、), you read many pages per write. E.g., if f() is DeCompress(), you write many pages per read.f(x)RAMInputBufferOutputBufferOUTPUTINPUT2-Way Sort: Requires 3 Buffers Pass 0: Read a page, sort it, write it.only one buffer page is used (as in previous slide) Pass 1, 2, 3, , etc.: requires 3 buffer pag
5、es merge pairs of runs into runs twice as long three buffer pages used.Main memory buffersINPUT 1INPUT 2OUTPUTDiskDiskTwo-Way External Merge SortEach pass we read + write each page in file.N pages in the file = the number of passesSo total cost is: Idea: Divide and conquer: sort subfiles and mergelo
6、g21N212NNlog+Input file1-page runs2-page runs4-page runs8-page runsPASS 0PASS 1PASS 2PASS 393,46,29,48,75,63,123,45,62,64,97,81,322,34,64,78,91,35,622,34,46,78,91,23,561,22,33,44,56,67,8General External Merge Sort To sort a file with N pages using B buffer pages:Pass 0: use B buffer pages. Produce s
7、orted runs of B pages each. Pass 1, 2, , etc.: merge B-1 runs. NB/B Main memory buffersINPUT 1INPUT B-1OUTPUTDiskDiskINPUT 2. . . . . . .* More than 3 buffer pages. How can we utilize them?Cost of External Merge Sort Number of passes: Cost = 2N * (# of passes) E.g., with 5 buffer pages, to sort 108
8、page file:Pass 0: = 22 sorted runs of 5 pages each (last run is only 3 pages) Pass 1: = 6 sorted runs of 20 pages each (last run is only 8 pages)Pass 2: 2 sorted runs, 80 pages and 28 pagesPass 3: Sorted file of 108 pages11+-log/BNB1085/224/Formula check: log4 22= 3 + 1 4 passes Number of Passes of
9、External Sort N B=3 B=5 B=9 B=17 B=129 B=257 100 7 4 3 2 1 1 1,000 10 5 4 3 2 2 10,000 13 7 5 4 2 2 100,000 17 9 6 5 3 3 1,000,000 20 10 7 5 3 3 10,000,000 23 12 8 6 4 3 100,000,000 26 14 9 7 4 4 1,000,000,000 30 15 10 8 5 4 ( I/O cost is 2N times number of passes)Internal Sort AlgorithmQuicksort is
10、 a fast way to sort in memory.Alternative: “tournament sort” (a.k.a. “heapsort”, “replacement selection”)Keep heap H in memoryread B-2 pages of records, inserting into H;while (records left) m = H.removeMinUnmarked (); append m to output buffer; if (m=NULL = all entries in H are marked) H.unmark(all
11、); start new output run; else read in new record r (use 1 buffer for input pages); H.insert(r at ms position); if (r m) H.mark(r); More on Heapsort Fact: average length of a run in heapsort is 2(B-2)The “snowplow” analogy Worst-Case:What is min length of a run?How does this arise? Best-Case:What is
12、max length of a run?How does this arise? Quicksort is faster, but . longer runs often means fewer passes!BI/O for External Merge Sort Do I/O a page at a time Not one I/O per record In fact, read a block (chunk) of pages sequentially! Suggests we should make each buffer (input/output) be a block of p
13、ages.But this will reduce fan-in during merge passes!In practice, most files still sorted in 2-3 passes.Double Buffering To reduce wait time for I/O request to complete, can prefetch into shadow block. Potentially, more passes; in practice, most files still sorted in 2-3 passes.OUTPUTOUTPUTDiskDiskI
14、NPUT 1INPUT kINPUT 2INPUT 1INPUT 2INPUT kblock sizebB main memory buffers, k-way mergeUsing 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 cluster
15、edGood idea!B+ tree is not clusteredCould be a very bad idea!Clustered B+ Tree Used for SortingCost: root to the left-most leaf, then retrieve all leaf pages (Alternative 1)If Alternative 2 is used? Additional cost of retrieving data records: each page fetched just once.* Always better than external
16、 sorting!(Directs search)Data RecordsIndexData Entries(Sequence set)Unclustered B+ Tree Used for Sorting Alternative (2) for data entries; each data entry contains rid of a data record. In general, one I/O per data record!(Directs search)Data RecordsIndexData Entries(Sequence set)External Sorting vs
17、. Unclustered Index* p: # of records per page* B=1,000 and block size=32 for sorting* p=100 is the more realistic value.N Sorting p=1 p=10 p=100 100 200 100 1,000 10,000 1,000 2,000 1,000 10,000 100,000 10,000 40,000 10,000 100,000 1,000,000 100,000 600,000 100,000 1,000,000 10,000,000 1,000,000 8,0
18、00,000 1,000,000 10,000,000 100,000,000 10,000,000 80,000,000 10,000,000 100,000,000 1,000,000,000 Summary External sorting is important External merge sort minimizes disk I/O cost:Pass 0: Produces sorted runs of size B (# buffer pages). Later passes: merge runs.# of runs merged at a time depends on B, and block size.Larger block size means less I/O cost per page.Larger block size means smaller # runs merged.In practice, # of runs rarely more
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 医学课件-创伤性齿突骨折疾病防治指南解读
- 企业信用报告-河北黑一橡胶有限公司
- 2026年护理专业模拟试卷目试卷(含答案)
- 2026年危险货物运输调度员适应性模拟题目及答案详解
- 2007年中国聚酯纤维(PTT)产业市场研究分析报告
- 2026年入学心理模拟题及答案详解
- 信息技术2026年:物联网应用实践考试及答案详解
- 2026年公司鱼油提炼工科技创新考核模拟试卷及答案详解
- 2026年创意设计师专业技能考核模拟试题(含答案)
- 2026年普联工厂测模拟题及答案详解
- 2026年云南省昆明市公安招聘辅警考试试卷带答案
- 26年秋八年级历史1-12课材料研读想一想课后活动答案
- 初中化学中考复习中和反应及反应后溶液成分探究教学设计
- 2026年初级注册安全工程师考试题库(附答案)
- 纪念中国工农红军长征胜利90周年应知应会100题及答案解析
- 新教材部编人教版五年级上册道德与法治(课件)第11课走进新时代
- DB53T 1176.1-2023 云南省行政村、社区编码 第1部分:总则
- 2026上海城投水务校园招聘103人易考易错模拟试题(共500题)试卷后附参考答案
- 2026年酸枣仁行业分析报告及未来发展趋势报告
- 2026年中考历史考前冲刺:小论文 满分方法指导讲义
- 金矿详查实施方案
评论
0/150
提交评论