《數(shù)據(jù)庫系統(tǒng)》英文教學(xué)課件
《數(shù)據(jù)庫系統(tǒng)》英文教學(xué)課件,數(shù)據(jù)庫系統(tǒng),數(shù)據(jù)庫,系統(tǒng),英文,教學(xué),課件
Storing Data:Disks and Files(R&G Chapter 9)“Yea,from the table of my memoryIll wipe away all trivial fond records.”-Shakespeare,HamletReviewArent Databases Great?Entity-Relationship modelRelational modelSQL Rails AssociationsDisks,Memory,and FilesQuery Optimizationand ExecutionRelational OperatorsFiles and Access MethodsBuffer ManagementDisk Space ManagementDBThe BIG pictureDisks and Files DBMS stores information on disks.In an electronic world,disks are a mechanical anachronism!This has major implications for DBMS design!READ:transfer data from disk to main memory(RAM).WRITE:transfer data from RAM to disk.Both are high-cost operations,relative to in-memory operations,so must be planned carefully!Why Not Store Everything in Main Memory?Costs too much.For$1000,PCConnection will sell you either 17GB of RAM 88GB of flash4.1 TB of diskMain memory is volatile.We want data to be saved between runs.(Obviously!)The Storage HierarchySource:Operating Systems Concepts 5th Edition Main memory(RAM)for currently used data.Disk for the main database(secondary storage).Tapes for archiving older versions of the data(tertiary storage).Smaller,FasterBigger,SlowerJim Grays Storage Latency Analogy:How Far Away is the Data?RegistersOn Chip CacheOn Board CacheMemory Disk1210100Tape/Optical Robot109106SacramentoThis Lecture HallThis RoomMy Head10 min1.5 hr2 Years1 minPluto2,000 YearsAndromedaDisksSecondary storage device of choice.Main advantage over tapes:random access vs.sequential.Data is stored and retrieved in units called disk blocks or pages.Unlike RAM,time to retrieve a disk block varies depending upon location on disk.Therefore,relative placement of blocks on disk has major impact on DBMS performance!Components of a Disk Platters The platters spin(say,120 rps).Spindle The arm assembly is moved in or out to position a head on a desired track.Tracks under heads make a cylinder(imaginary!).Disk headArm movementArm assemblyOnly one head reads/writes at any one time.TracksSectorv Block size is a multiple of sector size(which is fixed).Accessing a Disk PageTime to access(read/write)a disk block:seek time(moving arms to position disk head on track)rotational delay(waiting for block to rotate under head)transfer time(actually moving data to/from disk surface)Seek time and rotational delay dominate.Seek time varies between about 0.3 and 10msecRotational delay varies from 0 to 4msecTransfer rate around.08msec per 8K blockKey to lower I/O cost:reduce seek/rotation delays!Hardware vs.software solutions?Arranging Pages on DiskNext block concept:blocks on same track,followed byblocks on same cylinder,followed byblocks on adjacent cylinderBlocks in a file should be arranged sequentially on disk(by next),to minimize seek and rotational delay.For a sequential scan,pre-fetching several pages at a time is a big win!Disk Space ManagementLowest layer of DBMS software manages space on disk(using OS file system or not?).Higher levels call upon this layer to:allocate/de-allocate a pageread/write a pageBest if a request for a sequence of pages is satisfied by pages stored sequentially on disk!Responsibility of disk space manager.Higher levels dont know how this is done,or how free space is managed.Though they may make performance assumptions!Hence disk space manager should do a decent job.ContextQuery Optimizationand ExecutionRelational OperatorsFiles and Access MethodsBuffer ManagementDisk Space ManagementDBBuffer Management in a DBMSData must be in RAM for DBMS to operate on it!Buffer Mgr hides the fact that not all data is in RAMDBMAIN MEMORYDISKdisk pagefree framePage Requests from Higher LevelsBUFFER POOLchoice of frame dictatedby replacement policyWhen a Page is Requested.Buffer pool information table contains:If requested page is not in pool:Choose a frame for replacement.Only“un-pinned”pages are candidates!If frame is“dirty”,write it to diskRead requested page into chosen framePin the page and return its address.*If requests can be predicted(e.g.,sequential scans)pages can be pre-fetched several pages at a time!More on Buffer ManagementRequestor of page must eventually unpin it,and indicate whether page has been modified:dirty bit is used for this.Page in pool may be requested many times,a pin count is used.To pin a page,pin_count+A page is a candidate for replacement iff pin count=0(“unpinned”)CC&recovery may entail additional I/O when a frame is chosen for replacement.Write-Ahead Log protocol;more later!Buffer Replacement PolicyFrame is chosen for replacement by a replacement policy:Least-recently-used(LRU),MRU,Clock,etc.Policy can have big impact on#of I/Os;depends on the access pattern.LRU Replacement PolicyLeast Recently Used(LRU)for each page in buffer pool,keep track of time when last unpinnedreplace the frame which has the oldest(earliest)timevery common policy:intuitive and simpleWorks well for repeated accesses to popular pagesProblems?Problem:Sequential flooding LRU+repeated sequential scans.#buffer frames 3Indexes are file structures that enable us to answer such value-based queries efficiently.Record Formats:Fixed LengthInformation about field types same for all records in a file;stored in system catalogs.Finding ith field done via arithmetic.Base address(B)L1L2L3L4F1F2F3F4Address=B+L1+L2Record Formats:Variable LengthTwo alternative formats(#fields is fixed):*Second offers direct access to ith field,efficient storage of nulls(special dont know value);small directory overhead.$Fields Delimited by Special SymbolsF1 F2 F3 F4F1 F2 F3 F4Array of Field OffsetsPage Formats:Fixed Length Records*Record id=.In first alternative,moving records for free space management changes rid;may not be acceptable.Slot 1Slot 2Slot N.NM10.M .3 2 1PACKEDUNPACKED,BITMAPSlot 1Slot 2Slot NFreeSpaceSlot M11number of recordsnumberof slotsPage Formats:Variable Length Records*Can move records on page without changing rid;so,attractive for fixed-length records too.Page iRid=(i,N)Rid=(i,2)Rid=(i,1)Pointerto startof freespaceSLOT DIRECTORYN .2 1201624N#slotsSystem CatalogsFor each relation:name,file location,file structure(e.g.,Heap file)attribute name and type,for each attributeindex name,for each indexintegrity constraintsFor each index:structure(e.g.,B+tree)and search key fieldsFor each view:view name and definitionPlus statistics,authorization,buffer pool size,etc.*Catalogs are themselves stored as relations!Attr_Cat(attr_name,rel_name,type,position)attr_namerel_nametypepositionattr_nameAttribute_Catstring1rel_nameAttribute_Catstring2typeAttribute_Catstring3positionAttribute_Catinteger4sidStudentsstring1nameStudentsstring2loginStudentsstring3ageStudentsinteger4gpaStudentsreal5fidFacultystring1fnameFacultystring2salFacultyreal3pg_attributeSummaryDisks provide cheap,non-volatile storage.Random access,but cost depends on location of page on disk;important to arrange data sequentially to minimize seek and rotation delays.Buffer manager brings pages into RAM.Page stays in RAM until released by requestor.Written to disk when frame chosen for replacement(which is sometime after requestor releases the page).Choice of frame to replace based on replacement policy.Tries to pre-fetch several pages at a time.Summary(Contd.)DBMS vs.OS File SupportDBMS needs features not found in many OSs,e.g.,forcing a page to disk,controlling the order of page writes to disk,files spanning disks,ability to control pre-fetching and page replacement policy based on predictable access patterns,etc.Variable length record format with field offset directory offers support for direct access to ith field and null values.Slotted page format supports variable length records and allows records to move on page.Summary(Contd.)File layer keeps track of pages in a file,and supports abstraction of a collection of records.Pages with free space identified using linked list or directory structure(similar to how pages in file are kept track of).Indexes support efficient retrieval of records based on the values in some fields.Catalog relations store information about relations,indexes and views.(Information that is common to all records in a given collection.)
收藏