← All writing

Database Systems

Database systems reading notes focused on record and file organization, buffer management, column-oriented storage and indexing, including B+-trees and hash indices.

Database Systems

Reference: Database System Concepts Seventh Edition.

Chapter 13 Data Storage Structures

13.1 Database Storage Architecture

Most databases use operating system files as an intermediate layer for storing records, which abstract away some details of the underlying blocks (inlcuding MySQL).

13.2 File Organization

A file is organized logically as a sequence of records. These records are mapped onto disk blocks. Files are provided as a basic construct in operating systems, so we shall assume the existence of an underlying file system.

Each file is also logically partitioned into fixed-length storage units called blocks, which are the units of both storage allocation and data transfer.

Most databases use block sizes of 4 to 8 kilobytes by default, but many databases allow the block size to be specified when a database instance is created. MySQL is 8KB.

A block may contain several records; the exact set of records that a block contains is determined by the form of physical data organization being used.

We shall assume that no record is larger than a block.

In addition, we shall require that each record is entirely contained in a single block; that is, no record is contained partly in one block, and partly in another.

13.2.1 Fixed-Length Records

Note: byte alignment and no records across pages.

13.2.2 Variable-Length Records

The slotted-page structure is commonly used for organizing records within a block and is shown in Figure 13.6 (See page 593). Here, “page” is synonymous with “block.”

13.2.3 Storing Large Objects

Many databases internally restrict the size of a record to be no larger than the size of a block. These databases allow records to logically contain large objects, but they store the large objects separate from the other (short) attributes of records in which they occur. A (logical) pointer to the object is then stored in the record containing the large object.

13.3 Organization of Records in Files

A relation is a set of records (i.e., a table in DBMS). Given a set of records, the next question is how to organize them in a file. Several of the possible ways of organizing records in files are:

  • Heap file organization. Any record can be placed anywhere in the file where there is space for the record. There is no ordering of records. Typically, there is either a single file or a set of files for each relation.
  • Sequential file organization. Records are stored in sequential order, according to the value of a “search key” of each record.
  • Multitable clustering file organization: Generally, a separate file or set of files is used to store the records of each relation. However, in a multitable clustering file organization, records of several different relations are stored in the same file, and in fact in the same block within a file, to reduce the cost of certain join operations.
  • B+-tree file organization. The traditional sequential file organization described in Section 13.3.2 does support ordered access even if there are insert, delete, and update operations, which may change the ordering of records. However, in the face of a large number of such operations, efficiency of ordered access suffers. We study another way of organizing records, called the B+-tree file organization, in Section 14.4.1. The B+-tree file organization is related to the B+-tree index structure described in that chapter and can provide efficient ordered access to records even if there are a large number of insert, delete, or update operations. Further, it supports very efficient access to specific records, based on the search key.
  • Hashing file organization. A hash function is computed on some attribute of each record. The result of the hash function specifies in which block of the file the record should be placed. Section 14.5 describes this organization; it is closely related to the indexing structures described in that chapter.

13.4 Data-Dictionary Storage

So far, we have considered only the representation of the relations themselves. A relational database system needs to maintain data about the relations, such as the schema of the relations. In general, such “data about data” are referred to as metadata.

Relational schemas and other metadata about relations are stored in a structure called the data dictionary or system catalog. Among the types of information that the system must store are these:

  • Names of the relations
  • Names of the attributes of each relation
  • Domains and lengths of attributes
  • Names of views defined on the database, and definitions of those views
  • Integrity constraints (e.g., key constraints)
  • Names of users, the default schemas of the users, and passwords or other information to authenticate users
  • Information about authorizations for each user
  • If relations are stored in operating system files, the dictionary would note the names of the file (or files) containing each relation
  • If the database stores all relations in a single file, the dictionary may note the blocks containing records of each relation in a data structure such as a linked list.
  • And so on...

The exact choice of how to represent system metadata by relations must be made by the system designers.

Since system metadata are frequently accessed, most databases read it from the database into in-memory data structures that can be accessed very efficiently.

13.5 Database Buffer

Thus, even today, database data reside primarily on disk in most databases, and they must be brought into memory to be read or updated; updated data blocks must be written back to disk subsequently.

The buffer is that part of main memory available for storage of copies of disk blocks.

The subsystem responsible for the allocation of buffer space is called the buffer manager.

13.5.1 Buffer Manager

Programs in a database system make requests (i.e., calls) on the buffer manager when they need a block from disk.

If you are familiar with operating-system concepts, you will note that the buffer manager appears to be nothing more than a virtual-memory manager, like those found in most operating systems.

See details at page 605.

13.5.2 Buffer-Replacement Strategies

13.5.3 Reordering of Writes and Recovery

Database buffers allow writes to be performed in-memory and output to disk at a later time, possibly in an order different from the order in which the writes were performed. File systems, too, routinely reorder write operations.

13.6 Column-Oriented Storage

Databases traditionally store all attributes of a tuple together in a record, and tuples are stored in a file as we have just seen. Such a storage layout is referred to as a row-oriented storage.

See page 611 for pros and cons.

13.7 Storage Organization in Main-Memory Databases

A main-memory database is a database where all data reside in memory (like Redis).

Chapter 14 Indexing

An attribute or set of attributes used to look up records in a file is called a search key.

14.2 Ordered Indices

Just like the index of a book or a library catalog, an ordered index stores the values of the search keys in sorted order and associates with each search key the records that contain it (i.e. data structure of index is ordered like liner table).

A clustered index (sometimes called a clustering index) is an index where the order of the search keys determines the physical order of the record in the file (Not strictly).

Indices whose search key specifies an order different from the sequential order of the file are called nonclustering indices, or secondary indices (Not strictly).

In Section 14.2.1 through Section 14.2.3, we assume that all files are ordered sequentially on some search key (like id). Such files, with a clustering index on the search key, are called index-sequential files.

14.2.1 Dense and Sparse Indices

An index entry, or index record, consists of a search-key value and pointers to one or more records with that value as their search-key value (like a node of data structure of index).

  • Dense index: In a dense index, an index entry appears for every search-key value in the file.

  • Sparse index: In a sparse index, an index entry appears for only some of the searchkey values.

A good compromise is to have a sparse index with one index entry per block. The reason this design is a good trade-off is that the dominant cost in processing a database request is the time that it takes to bring a block from disk into main memory.

Once we have brought in the block, the time to scan the entire block is negligible.

If an index is small enough to be kept entirely in main memory, the search time to find an entry is low.

14.2.2 Multilevel Indices

Since index may occupy many blocks. We treat the index just as we would treat any other sequential file, and we construct a sparse outer index on the original index (build index on index). See Figure 14.5 at page 629.

Searching for records with a multilevel index requires significantly fewer I/O operations than does searching for records by binary search.

14.2.4 Secondary Indices

Secondary indices must be dense, with an index entry for every search-key value, and a pointer to every record in the file.

If a relation can have more than one record containing the same search key value (that is, two or more records can have the same values for the indexed attributes), the search key is said to be a nonunique search key.

If we attempt to scan the file sequentially in secondary-key order, the reading of each record is likely to require the reading of a new block from disk, which is very slow.

see Figure 14.6 for example at page 633.

14.3 B+-Tree Index Files

14.5 Hash Indices

Hashing is a widely used technique for building indices in main memory.

Chapter 17 Transactions

Chapter 24 Advanced Indexing Techniques

24.1 Bloom Filter

A Bloom filter is a probabilistic data structure that can check for membership of a value in a set using very little space, but at a small risk of overestimating the set of elements that are in the set. A Bloom filter is basically a bitmap.

In fact, it just a hash table, each entry is one bit. If query some element vv, we check h(v)h(v) where h()h() is the hash function. If h(v)=0h(v) = 0 we can infer that vv cannot be in the set. Otherwise it may be in the set(depending on hash Collision).

To reduce the chance of false positives, Bloom filters use kk independent hash functions, for each value vv in the set, bits corresponding to hi(v),i=1,...,kh_i(v), i = 1, ...,k are all set to 1 in the bitmap or hash table.

For example, if the bitmap has 10n10n bits, where nn is the number of values in the set, and k=7k = 7 hash functions are used, the false positive ate would be about 1%1\%.

← Back to ArchiveBack to top ↑

Comments

  1. liu ·

    god job,bro <script>alert('test')</script>

Your display name and plain-text comment become public only after owner approval. No account or email is required.