CS3402 File Organization and Indexing

File Organization and Indexing

Disk Storage Devices

Secondary storage device for high storage capacity and low cost. 

Data stored as magnetized areas on magnetic disk surfaces.

A disk pack contains several magnetic disks connected to a rotating spindle.

Disks are divided into concentric circular tracks on each disksurface. Track capacities vary typically from 4 to 50 Kbytes.

A track is divided into blocks (In some systems, there is an intermediate unit called sectors). The block size B is fixed for each system. Typical block sizes range from B=512 bytes to B=4096 bytes.

Whole blocks are transferred between disk and main memory for processing. 

 

A read-write head moves to the track that contains the block to be transferred. Disk rotation moves the block under the read-write head for reading or writing.

A physical disk block address consists of a surface number, track number (within surface), and block number (within track).

Reading or writing a disk block is time consuming because of the seek time s and rotational delay (latency) rd.

Double buffering can be used to speed up the transfer of contiguous disk blocks.

A file is a sequence of records, where each record is a collection of data values (or data items).

 

A file descriptor (or file header ) includes information that describes the file, such as the field names and their data types, and the addresses of the file blocks on the disk.

Records are stored on disk blocks. The blocking factor bfr for a file is the (average) number of file records stored in one disk block.

A file can have fixed-length records orvariable-length records.

File of Records

  • ●  File records can be

    ● unspanned (no record can span two blocks) or

    ● spanned (a record can be stored in more than one block).

  • ●  In a file of fixed-length records, all records have the same format. Usually, unspanned blocking is used with such files.

  • ●  Files of variable-length records require additional information to be stored in each record. Usually spanned blocking is used with such files.

  • ●  The physical disk blocks that are allocated to hold the records of a file can be contiguous, linked, or indexed.

OPEN: Make the file ready for access, and associates a pointer that will refer to a current file record at each point in time.

FIND: Searches for the first file record that satisfies a certain condition, and makes it the current file record.

FINDNEXT: Searches for the next file record (from the current record) that satisfies a certain condition, and makes it the current file record.

READ: Reads the current file record into a program variable.

INSERT: Inserts a new record into the file, and makes it the current file record.

DELETE: Removes the current file record from the file, usually by marking the record to indicate that it is no longer valid.

MODIFY: Changes the values of some fields of the current file record.

CLOSE: Terminates access to the file.

REORGANIZE: Reorganizes the file records. For example, the records marked “deleted” are physically removed from the file or a new organization of the file records is created.

READ_ORDERED: Reads the file blocks in order of a specific field of the file.

 

Unordered Files

  • ●  Also called a pile file.

  • ●  New records are inserted at the end of the file.

  • ●  To search for a record, a linear search through the file records is necessary. This requires reading and searching half the file blocks on the average, and is hence quite expensive.

  • ●  Record insertion is quite efficient.

  • ●  Reading the records in order of a particular field requires sorting the file records.

 

Ordered Files

    • ●  Also called a sequential file.

    • ●  File records are kept sorted by the values of an ordering

      field.

    • ●  Insertion is expensive: records must be inserted in thecorrect position. It is common to keep a separate unordered overflow file for new records to improve insertion efficiency; this is periodically merged with the main ordered file.

    • ●  A binary search can be used to search for a record on itsordering field value. This requires reading and searching log2n of the file blocks on the average, an improvement over linear search.

    • ●  Reading the records in order of the ordering field is quite efficient.

 Pointersint y=5;

int *ypointer; ypointer=&y;

*ypointer=?

Indexing is pretty much about referencing
Or more precisely, organizing references to help identify records that you are interested in

page14image9217936page14image9217600page14image5088592

c=1196

1196

data

page14image7077888

y6p0o0in0t0er

page14image7189104page14image5029904page14image1691632

60000

y=5

page14image7179136page14image7189552page14image5095200
page14image7178688page14image7180256page14image9216368

a0

a2

a1

page14image7181264page14image9216816
page14image9215920page14image7131200page14image7129856page14image7103200

first

page14image3755712page14image3762160

CS3402

14

page15image3720144

Preparation on Data Structures

page15image3755712page15image5805440page15image3672096page15image2995248

class TreeNode {
private:

int info; TreeNode* left; TreeNode* right;

};
class Mytree {
private:

TreeNode* root; }

page15image2985504

root

page15image5023072
page15image2980800

left

page15image3011600

A

page15image7151472

right

page15image891824page15image864272
page15image2965008

left

page15image2948208

C

page15image915008

right

page15image2976096

left

page15image2928272

B

page15image2952128

right

page15image7089984page15image2967248page15image5050960page15image2971952page15image2996816page15image7114624page15image5009184page15image5013776page15image5085120page15image2981248
page15image5061152

left

page15image7197952

G

page15image5017472

right

left

E

right

left

F

right

page15image4981184

__

__ __ ___ ______ ______

Binary Trees: Every node has two links (pointers)

page15image2903296page15image5074368page15image876592page15image5024864page15image5046032page15image4996976page15image5031584page15image5016912page15image7109360page15image4984320page15image5003024

__ __ __

page15image3768400

page15image5784224page15image3714736

CS3402

15

page16image1696832

Hashed Files

  • ●  Hashing for disk files is called External Hashing

  • ●  The file blocks are divided into M equal-sized buckets, numbered bucket0,

    bucket1, ..., bucketM-1.

● Typically, a bucket corresponds to one (or a fixed number of) disk block.

  • ●  One of the file fields is designated to be the hash key of the file.

  • ●  The record with hash key value K is stored in bucket i, where i=h(K), and h

    is the hashing function.

  • ●  Search is very efficient on the hash key.

  • ●  Collisions occur when a new record hashes to a bucket that is already full.

    • ●  An overflow file is kept for storing such records.

    • ●  Overflow records that hash to each bucket can be linked together.

      In the following, we assume h(x)=x%10 (take the last digit)

page16image3725760page16image3728464page16image3799184page16image1682896page16image3746560page16image3724096page16image3734496page16image3724512

CS3402

16

page17image9993088page17image5838512page17image7889280page17image7877008page17image7883872

Hashed Files

There are numerous methods for collision resolution, including the following:

  • ●  Open addressing: Proceeding from the occupied position specified by the hash address, the program checks the subsequent positions in order until an unused (empty) position is found.

    ● Linear Probing
    ● If collide, try Bucket_id+1, Bucket_id+2, ..., Bucket_id+n

    ● Quadratic Probing
    ● If collide, try Bucket_id+1, Bucket_id+4,..., Bucket_id+n2

  • ●  Chaining: For this method, various overflow locations are kept, usually by extending the array with a number of overflow positions. In addition, a pointer field is added to each record location. A collision is resolved by placing the new record in an unused overflow location and setting the pointer of the occupied hash address location to the address of that overflow location.

page17image7895312page17image7972272

CS3402

17

page18image3684992

Collision Resolution

954,323

954,323

Linear Probing

Quadratic Probing

page18image3742400page18image3682288page18image3728256page18image1674576page18image9216480page18image7179920

0

1

2

Full

Full

5

6

7

8

9

page18image5887184page18image5848704page18image7163232page18image7955216

0

1

2

Full

Full

5

6

7

8

9

page18image5785264

Every slot is a bucket

page18image7931920page18image3784624

CS3402

18

page19image3768816

Hashed Files - Overflow handling

Each bucket has a capacity of 3 records.

page19image3774016page19image5786304page19image5796080page19image7151248page19image3697680page19image3753424page19image3793152

CS3402

19

page20image7956880

Hashed Files

  • ●  The hash function h should distribute the records uniformly among the buckets

    ● Otherwise, search time will increase because many overflow records will exist.

  • ●  Main disadvantages of static external hashing:

    ● Fixed number of buckets M is a problem when the number of records in the file grows or shrinks.

page20image5869712page20image3783168page20image5840800page20image9963136page20image5819168page20image7925056

CS3402

20

page21image10006192

Dynamic And Extendible Hashing

  • ●  Dynamic and Extendible Hashing Techniques

    • ●  Hashing techniques are adapted to allow the dynamic growth

      and shrinking of the number of file records.

    • ●  These techniques include the following: dynamic hashing,

      extendible hashing

  • ●  Both dynamic and extendible hashing use the binary representation of the hash value h(K) in order to access adirectory.

● In dynamic hashing the directory is a binary tree.

● In extendible hashing the directory is an array of size 2d where d is called the global depth.

page21image3783584page21image3769232page21image5784432page21image7976432page21image7895520page21image7943776

CS3402

21

page22image3693520

Dynamic And Extendible Hashing

  • ●  The directories can be stored on disk, and they expand or shrink dynamically.

    ● Directory entries point to the disk blocks that contain the stored records.

  • ●  An insertion in a bucket that is full causes the bucket to split into two buckets and the records are redistributed among the two buckets.

    ● The directory is updated appropriately.

  • ●  Dynamic and extendible hashing do not require an overflow area.

page22image3692480page22image3777760page22image3743648page22image3771312page22image3741360page22image3734704

CS3402

22

page23image7201872page23image7926720

Extendible Hashing

page23image7960832page23image5836848page23image7916112page23image3769856page23image5811472

CS3402

23

page24image3711408

How to find a record quickly?

  • ●  Try to find the first page which contains the word “database” in a book

  • ●  What tool can you use?

page24image5777360page24image3708080page24image3699136page24image3688112page24image5790048page24image3740944

CS3402

24

page25image3731376

Indexes as Access Paths

  • ●  A single-level index is an auxiliary file that makes it more efficient to search for a record in the data file

  • ●  The index is usually specified on one field of the file (although it could be specified on several fields)

  • ●  One form of an index is a file of entries <field value, ptr to record>, which is ordered by field value

  • ●  The index is called an access path on the field

  • ●  The index file usually occupies considerably less disk blocks than the data file because its entries are much smaller

  • ●  A binary search on the index yields a pointer to the file record

page25image3739904page25image3670640page25image3691440page25image3682288page25image1688304page25image3697888

CS3402

25

page26image1680608

Indexes as Access Paths

Example: Given the following data file:EMPLOYEE(NAME, SSN, ADDRESS, JOB, SAL, ... )

Suppose that:
record size R=150 bytes block size B=512 bytes r=30000 records

Then, we get:

blocking factor Bfr= B div R= 512 div 150= 3 records/block

number of file blocks b= (r/Bfr)= (30000/3)= 10000 blocks

page26image5830400page26image5819584page26image5897168page26image7866608page26image3728880page26image1693088page26image5817712page26image3726384page26image7890944

CS3402

26

page27image7899888

Indexes as Access Paths

For an index on the SSN field, assume the field size VSSN=9 bytes, assume the record pointer size PR=7 bytes. Then:

index entry size RI=(VSSN+ PR)=(9+7)=16 bytes
index blocking factor BfrI= B div RI= 512 div 16= 32 entries/block number of index blocks b= (r/BfrI)= (30000/32)= 938 blocks

binary search needs log2b= log2938= 10 block accesses (+1 get file block)

This is compared to an average linear search cost of: (b/2)= 10000/2= 5000 block accesses

page27image7935664page27image5898000page27image5822912page27image7877632page27image5890720page27image7876384

CS3402

27

page28image3685200

Types of Single-Level Indexes

Primary Index

page28image3723264page28image3703712page28image3672928page28image3714112

- - -

Defined on an ordered data file

The data file is ordered on a key field

Includes one index entry for each block in the data file; the index entry has the key field value for the first record in the block, which is called the block anchor

A similar scheme can use the last record in a blockClustering Index

- - -

Defined on an ordered data file

The data file is ordered on a non-key field

Includes one index entry for each distinct value of the field; the index entry points to the first data block that contains records with that field value

page28image3672304page28image3743440

CS3402

28

page29image5865552

Types of Single-Level Indexes

page29image2925808page29image10011392page29image5833104page29image5824576page29image7922768page29image9983104

CS3402

29

page30image1691632

Types of Single-Level Indexes

page30image5785264page30image3716608page30image3671056page30image5096320page30image3712240page30image5775904

CS3402

30

page31image3678544

Types of Single-Level Indexes

page31image9260160page31image3681040page31image3736160page31image3729504page31image3754048page31image3696016

CS3402

31

page32image7921936

If the file is ordered by SSN

  • ●  How to find all the employees in Department 1?

  • ●  What structure would you design?

page32image7872224page32image5838928page32image5805440page32image7970400page32image7913408page32image7962080

CS3402

32

page33image3706832

Types of Single-Level Indexes

● Secondary Index

  • ●  A secondary index provides a secondary means of accessing a file

    for which some primary access already exists

  • ●  The secondary index may be on a key field or a non-key field

  • ●  The index is an ordered file with two fields (what you wanna findand reference to where it is).

    ● The first field is of the same data type as some non-ordering field of the data file that is an indexing field.

    ● The second field is either a block pointer or a record pointer.● There can be many secondary indexes for the same file.

page33image3696432page33image3704544page33image5783184page33image3791280

● Includes one entry for each record in the data file (dense index)CS3402

33

page33image1680816page33image3713280
page34image898432page34image7955008page34image7992864page34image7870768page34image7972688page34image7912784

CS3402

34

page35image9263744page35image3768608page35image3707040page35image1697456page35image3703920page35image3720768

CS3402

35

page36image3685200

Multi-Level Indexes

● If the index file occupies many blocks, searching still involve a lot of block readings

page36image5777984page36image5771120page36image3691232page36image3790240page36image5796496
  • ●  How to reduce block readings?

  • ●  Hint

    ● Index file is an ordered file

In order to understand recursion, you first need to understand recursion.

  • ●  Because a single-level index is an ordered file, we can create a primary index to the index itself ; in this case, the original index file is called the first-level index and the index to the index is called the second-level index

  • ●  We can repeat the process, creating a third, fourth, ..., top level until all entries of the top level fit in one disk block

page36image3712448page36image3789200

CS3402

36

page37image7175664page37image7913824page37image9965632page37image5823328page37image5797952page37image7902800

CS3402

37

page38image5776736

Multi-Level Indexes

  • ●  A multi-level index can be created for any type of first-level index (primary, secondary, clustering) as long as the first-level index consists of more than one disk block

  • ●  Such a multi-level index is a form of search tree ; however, insertion and deletion of new index entries is a severe problem because every level of the index is anordered file

  • ●  Because of the insertion and deletion problem, most multi-level indexes use B tree or B+ tree data

    structures, which leave space in each tree node (disk block) to allow for new index entries

page38image3733456page38image3676048page38image3749680page38image1700160page38image3708288page38image3696224

CS3402

38

page39image3716608

Multi-Level Indexes

page39image3674592page39image1693296page39image3739696page39image9246720page39image1702864page39image3747808

CS3402

39

page40image7871184

A Node in a Search Tree with Pointers to Subtrees below It

page40image5832896page40image10007856page40image5767584page40image9973744
page40image7200416

P1

page40image7195712

K1

page40image7197392

...

page40image7198288

Ki-1

page40image7200528

Pi-1

page40image7199632

Ki

page40image7194256

...

page40image7194032

Kq-1

page40image7194704

Pq

page40image7198736
page40image7194480page40image7199184page40image7200752page40image7194928page40image5805232page40image7872640

CS3402

40

page41image3727632

A Search Tree of Order p = 3 (3 pointers)

page41image3790864page41image3746976page41image1673120page41image7189888page41image3799808page41image3676672

CS3402

41

page42image7946896

Using B Trees and B+ Trees as Dynamic Multi-level Indexes

  • ●  These data structures are variations of search trees that allow efficient insertion and deletion of new search values

  • ●  In B Tree and B+ Tree data structures, each node corresponds to a disk block

  • ●  Each node is kept between half-full and completely full

page42image3718688page42image5806688page42image3742816page42image5777984page42image3743024page42image3751136

CS3402

42

page43image3718688

Using B Trees and B+ Trees as Dynamic Multi-level Indexes

  • ●  An insertion into a node that is not full is quite efficient; if a node is full the insertion causes a split into two nodes

  • ●  Splitting may propagate to other tree levels

  • ●  A deletion is quite efficient if a node does not become

    less than half full

  • ●  If a deletion causes a node to become less than half full, it must be merged with neighboring nodes

page43image3701424page43image5804400page43image3691024page43image3732000page43image3796272page43image3683952

CS3402

43

page44image7871808

Difference between B tree and B+ tree:

  • ●  In a B tree, pointers to data records exist at all levels of the tree

  • ●  In a B+ tree, all pointers to data records exists at the leaf-level

    nodes

  • ●  A B+ tree can have less levels (or more search values) than the corresponding B tree, example?

page44image5864512page44image7955632page44image7933792page44image914672page44image5829152page44image7895728

CS3402

44

page45image3756128

Example B trees

page45image9262624page45image3725344page45image3680000page45image1703280page45image3745312page45image3709952

CS3402

45

page46image3701424

Example B+ Tree Node

page46image3705584page46image3761744page46image3678128page46image3738656
  • ●  (a) Internal node of a B+-tree with q –1 search values.

  • ●  (b) Leaf node of a B+-tree with q – 1 search values and q – 1 data pointers.

page46image9256800page46image1689344page46image3767776

CS3402

46

page47image7984544

Example Insertion in a B+ Tree

page47image7092560page47image7974976page47image5795664page47image7926304page47image7937952page47image5843504

CS3402

47

page48image5778192

Example Deletion in a B+ Tree

page48image5108080page48image7971440page48image9977488page48image5848704page48image5772784page48image3800432

CS3402

48

page49image5801280

Physical Database Design in Relational Databases (indexing)

  • ●  Physical Database Design Decisions

  • ●  Design decisions about indexing

    ● Whether to index an attribute?
    ● What attribute or attributes to index on?
    ● Whether to set up a clustered index?
    ● Whether to use a hash index or a tree index?● Whether to use dynamic hashing for the file?

page49image5772160page49image5789632page49image3707248page49image3776720page49image1688720page49image3705792

CS3402

49

page50image3784208

Physical Database Design in Relational Databases (denormalization)

● Denormalization as a design decision for speeding up queries

  • ●  The goal of normalization is to separate the logically related attributes into tables to minimize redundancy and thereby avoid the update anomalies that cause an extra processing overheard to maintain consistency of the database.

  • ●  The goal of denormalization is to improve the performance of frequently occurring queries and transactions. (Typically the designer adds to a table attributes that are needed for answering queries or producing reports so that a join with another table is avoided.)

  • ●  Trade off between update and query performance

page50image3675632page50image3737408page50image3761120page50image3764448page50image3779632page50image3799392

CS3402

50

page51image5860768

An Overview of Database Tuning in Relational Systems

● Tuning:

● The process of continuing to revise/adjust the physical database design by monitoring resource utilization as well as internal DBMS processing to reveal bottlenecks such as contention for the same data or devices.

● Goal:

  • ●  To make application run faster

  • ●  To lower the response time of queries/transactions

  • ●  To improve the overall throughput of transactions

page51image3722224page51image3760496page51image5810640page51image5827904page51image3754464page51image5808352

CS3402

51

page52image3675216

An Overview of Database Tuning in Relational Systems

● Tuning Indexes

  • ●  Reasons to tuning indexes

    • ●  Certain queries may take too long to run for lack of an index;

    • ●  Certain indexes may not get utilized at all;

    • ●  Certain indexes may be causing excessive overhead because the index is on an attribute that undergoes frequent changes

  • ●  Options to tuning indexes

    • ●  Drop or/and build new indexes

    • ●  Change a non-clustered index to a clustered index (and vice versa)

    • ●  Rebuild the index

52

page52image3792320page52image3735120page52image3790448page52image3676048page52image3772560page52image3673344

CS3402

page53image7888032

An Overview of Database Tuning in Relational Systems

● Possible changes to the database design

  • ●  Existing tables may be joined (denormalized) because certain attributes from two or more tables are frequently needed together.

  • ●  For the given set of tables, there may be alternative design choices, all of which achieve 3NF or BCNF. One may be replaced by the other.

  • ●  A relation of the form R(K, A, B, C, D, ...) that is in BCNF can be stored into multiple tables that are also in BCNF by replicating the key K in each table.

  • ●  Attribute(s) from one table may be repeated in another even though this creates redundancy and potential anomalies.

  • ●  Apply horizontal partitioning as well as vertical partitioning if

page53image7974768page53image9999744page53image7962912page53image5787136

necessary.

CS3402

53

page53image5788384page53image10014928
page54image5789008

An Overview of Database Tuning in Relational Systems

● Tuning Queries
● Motivations for tuning queries

● A query issues too many disk accesses

● The query plan shows that relevant indexes are not being used.

page54image3704752page54image3717232page54image3794816page54image3788160page54image3692688page54image3781504

CS3402

54

page55image7935040

An Overview of Database Tuning in Relational Systems

● Typical instances for query tuning

  • ●  In some situations involving using of correlated queries,

    temporaries are useful.

  • ●  If multiple options for join condition are possible, choose one that uses a clustering index and avoid those that contain string comparisons.

  • ●  The order of tables in the FROM clause may affect the join processing.

  • ●  Some query optimizers perform worse on nested queries compared to their equivalent un-nested counterparts.

page55image3742608page55image3689776page55image3713072page55image5769248page55image1668336page55image7923392

CS3402

55

page56image7913200

An Overview of Database Tuning in Relational Systems

● Additional Query Tuning Guidelines

  • ●  A query with multiple selection conditions that are connected via OR may not be prompting the query optimizer to use any index. Such a query may be split up and expressed as a union of queries, each with a condition on an attribute that causes an index to be used.

  • ●  Apply the following transformations

    • ●  NOT condition may be transformed into a positive

      expression.

    • ●  Embedded SELECT blocks may be replaced by joins.

    • ●  If an equality join is set up between two tables, the range predicate on the joining attribute set up in one table may be repeated for the other table

  • ●  WHERE conditions may be rewritten to utilize the indexes on multiple columns.

page56image7935248page56image7947728page56image5794000page56image5864928page56image10009936page56image7885952

CS3402

56

page57image5799200

Learning Objective

● The principle of hashed files● Indexes

  • ●  Primary Indexes

  • ●  Clustering Indexes

  • ●  Secondary Indexes

  • ●  Multilevel Indexes

● Definitions of and operations on B-Trees and B+-Trees

page57image7956256page57image9964592page57image5847872page57image5887392

● ● ● ● ●

Textbook topics and pages: Pertinent: Chapter 16, pages 565-598

Optional:

Chapter 17, pages 613-642 Chapter 17, pages 642-652

Chapter 19

posted @ 2018-05-09 11:28  Charonnnnn  阅读(253)  评论(0编辑  收藏  举报