《數(shù)據(jù)庫(kù)系統(tǒng)》英文教學(xué)課件
《數(shù)據(jù)庫(kù)系統(tǒng)》英文教學(xué)課件,數(shù)據(jù)庫(kù)系統(tǒng),數(shù)據(jù)庫(kù),系統(tǒng),英文,教學(xué),課件
File Organizations and IndexingR&G Chapter 8If you dont find it in the index,look very carefully through the entire catalogue.-Sears,Roebuck,and Co.,Consumers Guide,1897ContextQuery Optimizationand ExecutionRelational OperatorsFiles and Access MethodsBuffer ManagementDisk Space ManagementDBAlternative File OrganizationsMany alternatives exist,each good for some situations,and not so good in others:Heap files:Suitable when typical access is a file scan retrieving all records.Sorted Files:Best for retrieval in search key order,or only a range of records is needed.Clustered Files(with Indexes):Coming soonCost Model for AnalysisWe ignore CPU costs,for simplicity:B:The number of data blocksR:Number of records per blockD:(Average)time to read or write disk blockMeasuring number of block I/Os ignores gains of pre-fetching and sequential access;thus,even I/O cost is only loosely approximated.Average-case analysis;based on several simplistic assumptions.*Good enough to show the overall trends!Some Assumptions in the AnalysisSingle record insert and delete.Equality selection-exactly one match(what if more or less?).Heap Files:Insert always appends to end of file.Sorted Files:Files compacted after deletions.Selections on search key.Cost of Operations B:The number of data pagesR:Number of records per pageD:(Average)time to read or write disk pageHeap FileSorted FileClustered FileScan all recordsEquality SearchRange SearchInsertDeleteCost of Operations B:The number of data pagesR:Number of records per pageD:(Average)time to read or write disk pageHeap FileSorted FileClustered FileScan all recordsBDBDEquality SearchRange SearchInsertDeleteCost of Operations B:The number of data pagesR:Number of records per pageD:(Average)time to read or write disk pageHeap FileSorted FileClustered FileScan all recordsBDBDEquality Search0.5 BD(log2 B)*DRange SearchInsertDeleteCost of Operations B:The number of data pagesR:Number of records per pageD:(Average)time to read or write disk pageHeap FileSorted FileClustered FileScan all recordsBDBDEquality Search0.5 BD(log2 B)*DRange SearchBD(log2 B)+#match pg*DInsertDeleteCost of Operations B:The number of data pagesR:Number of records per pageD:(Average)time to read or write disk pageHeap FileSorted FileClustered FileScan all recordsBDBDEquality Search0.5 BD(log2 B)*DRange SearchBD(log2 B)+#match pg*DInsert2D(log2B)+B)D (because R,W 0.5)DeleteCost of Operations B:The number of data pagesR:Number of records per pageD:(Average)time to read or write disk pageHeap FileSorted FileClustered FileScan all recordsBDBDEquality Search0.5 BD(log2 B)*DRange SearchBD(log2 B)+#match pg*DInsert2D(log2B)+B)DDelete0.5BD+D(log2B)+B)D (because R,W 0.5)IndexesSometimes,we want to retrieve records by specifying the values in one or more fields,e.g.,Find all students in the“CS”departmentFind all students with a gpa 3An index on a file is a disk-based data structure that speeds up selections on the search key fields for the index.Any subset of the fields of a relation can be the search key for an index on the relation.Search key is not the same as key(e.g.doesnt have to be unique ID).An index contains a collection of data entries,and supports efficient retrieval of all records with a given search key value k.First Question to Ask About IndexesWhat kinds of selections do they support?Selections of form field constantEquality selections(op is=)Range selections(op is one of,=,BETWEEN)More exotic selections:2-dimensional ranges(“east of Berkeley and west of Truckee and North of Fresno and South of Eureka”)Or n-dimensional2-dimensional distances(“within 2 miles of Soda Hall”)Or n-dimensionalRanking queries(“10 restaurants closest to Berkeley”)Regular expression matches,genome string matches,etc.One common n-dimensional index:R-treeSupported in Oracle and InformixSee http:/gist.cs.berkeley.edu for research on this topicIndex BreakdownWhat selections does the index supportRepresentation of data entries in indexi.e.,what kind of info is the index actually storing?3 alternatives hereClustered vs.Unclustered IndexesSingle Key vs.Composite IndexesTree-based,hash-based,otherAlternatives for Data Entry k*in IndexThree alternatives:Actual data record(with key value k)Choice is orthogonal to the indexing technique.Examples of indexing techniques:B+trees,hash-based structures,R trees,GiSTs,Typically,index contains auxiliary information that directs searches to the desired data entriesCan have multiple(different)indexes per file.E.g.file sorted by age,with a hash index on salary and a B+tree index on name.Alternatives for Data Entries(Contd.)Alternative 1:Actual data record(with key value k)If this is used,index structure is a file organization for data records(like Heap files or sorted files).At most one index on a given collection of data records can use Alternative 1.This alternative saves pointer lookups but can be expensive to maintain with insertions and deletions.Alternatives for Data Entries(Contd.)Alternative 2 and Alternative 3 Easier to maintain than Alt 1.If more than one index is required on a given file,at most one index can use Alternative 1;rest must use Alternatives 2 or 3.Alternative 3 more compact than Alternative 2,but leads to variable sized data entries even if search keys are of fixed length.Even worse,for large rid lists the data entry would have to span multiple blocks!Index ClassificationClustered vs.unclustered:If order of data records is the same as,or close to,order of index data entries,then called clustered index.A file can be clustered on at most one search key.Cost of retrieving data records through index varies greatly based on whether index is clustered or not!Alternative 1 implies clustered,but not vice-versa.Clustered vs.Unclustered IndexSuppose that Alternative(2)is used for data entries,and that the data records are stored in a Heap file.To build clustered index,first sort the Heap file(with some free space on each block for future inserts).Overflow blocks may be needed for inserts.(Thus,order of data recs is close to,but not identical to,the sort order.)Index entriesData entriesdirect search for(Index File)(Data file)Data Recordsdata entriesData entriesData RecordsCLUSTEREDUNCLUSTEREDUnclustered vs.Clustered IndexesWhat are the tradeoffs?Clustered ProsEfficient for range searchesMay be able to do some types of compressionPossible locality benefits(related data?)?Clustered ConsExpensive to maintain(on the fly or sloppy with reorganization)Pages tend to be only 2/3 full!Cost of Operations B:The number of data pagesR:Number of records per pageD:(Average)time to read or write disk pageHeap FileSorted FileClustered FileScan all recordsBDBD1.5 BDEquality Search0.5 BD(log2 B)*D(logF 1.5B)*DRange SearchBD(log2 B)+#match pg*D(logF 1.5B)+#match pg*DInsert2D(log2B)+B)D(logF 1.5B)+1)*DDelete0.5BD+D(log2B)+B)D (because R,W 0.5)(logF 1.5B)+1)*DComposite Search KeysSearch on a combination of fields.Equality query:Every field value is equal to a constant value.E.g.wrt index:age=20 and sal=75Range query:Some field value is not a constant.E.g.:age 20;or age=20 and sal 10Data entries in index sorted by search key to support range queries.Lexicographic order Like the dictionary,but on fields,not letters!sue 1375bobcaljoe121020801112name age sal12,2012,1011,8013,7520,1210,1275,1380,111112121310207580Data recordssorted by nameData entries in indexsorted by Data entriessorted by Examples of composite keyindexes using lexicographic order.SummaryFile Layer manages access to records in pages.Record and page formats depend on fixed vs.variable-length.Free space management an important issue.Slotted page format supports variable length records and allows records to move on page.Many alternative file organizations exist,each appropriate in some situation.If selection queries are frequent,sorting the file or building an index is important.Hash-based indexes only good for equality search.Sorted files and tree-based indexes best for range search;also good for equality search.(Files rarely kept sorted in practice;B+tree index is better.)Index is a collection of data entries plus a way to quickly find entries with given key values.Summary(Contd.)Data entries in index can be actual data records,pairs,or pairs.Choice orthogonal to indexing structure(i.e.,tree,hash,etc.).Usually have several indexes on a given file of data records,each with a different search key.Indexes can be classified as clustered vs.unclusteredDifferences have important consequences for utility/performance.Catalog relations store information about relations,indexes and views.
收藏