《教妹学数据库系统》(六)存储管理

hello大家好,好久不见!今天我们的《教妹学数据库系统》来学习数据库系统中的存储管理。教妹学数据库,没见过这么酷炫的标题吧?“语不惊人死不休”,没错,标题就是这么酷炫。

我的妹妹小埋18岁,校园中女神一般的存在,成绩优异体育万能,个性温柔正直善良。然而,只有我知道,众人眼中光芒万丈的小埋,在过去是一个披着仓鼠斗篷,满地打滚,除了吃就是睡和玩的超级宅女。而这一切的转变,是从那一天晚上开始的。

从此之后,小埋经常让我帮她辅导功课。今天她想了解数据库系统中的存储管理。本篇教程通过我与小埋的对话的方式来谈一谈存储管理

存储介质(StorageMedia)

存储层级(TheMemoryHierarchy)

在这里插入图片描述

主存储器(PrimaryStorage)

  1. CPU可以使用加载/存储直接在主存储中操作数据(Data in primary storage can be operated on directly by CPUs using load/store)
  • Registers(寄存器)
  • Cache(高速缓存)
  • Mainmemory(内存)

主存储器按字节寻址。

  1. Secondary Storage(二级存储器)
    CPU无法直接处理辅助存储中的数据;必须首先使用读/写将其复制到主存储中
  • Magneticdisks(磁盘)/Harddiskdrives(HDD,机械硬盘)
  • Flashmemory(闪存)/Solidstatedrives(SSD,固态硬盘)

二级存储器按块寻址,并且是联机的。
(Secondarystorageisblock-addressable(按块寻址) and online(联机))

  1. Tertiary Storage(三级存储器)
    数据中间存储必须首先复制到第二存储
  • Magnetictape(磁带)
  • Opticaldisks(光盘)
  • NetworkStorage(网络存储)

二级存储器按块寻址,并且是脱机的。

  1. Access Time(访存时间)
    在这里插入图片描述
  2. Data Transfer Between Levels
    在这里插入图片描述

Categories of Storage Media

  1. VolatileStorage(易失存储器)

重新启动计算机时丢失数据非易失性存储(自动关机或崩溃后)

-主存储

  1. Non-volatileStorage(非易失存储器)

重新启动计算机时,非易失性存储中的数据将保留

-第三存储

  1. 非易失性主存储器正在兴起!(Non-volatile main memory is emerging!)

在这里插入图片描述

Phase-ChangeMemory(PCM,相变存储器)

  1. PCM使用相变材料(相变材料)存储数据
  • Amorphousphase(非晶态):高电阻率→0
  • Crystallinephase(晶态):低电阻率→1
  1. 通过电流脉冲设置相位
  • 快速冷却→无定形
  • 慢冷→结晶
  1. Characteristics of NVM
    在这里插入图片描述
  2. Magnetic Disks (磁盘)
    磁性磁盘由两个组件组成:
  • 磁盘组装:sectors(扇区)⊂tracks(磁道)⊂cylinders(柱面)

  • 头部组装:diskheads(磁头)anddiskarms(磁臂)

Database Pages(数据库页面)

  1. 页面是固定大小的数据块(512B–16KB)
  • 它可以包含元组,元数据,索引,日志记录…
  • 大多数DBMS不会混合页面类型
  • 一些DBMS要求页面是独立的
  1. 每个页面都被赋予唯一的标识符作为其页面ID(u〜)
  • DBMS使用间接层将页面ID映射到物理地点
  1. Page Layout
    每个页面都包含有关页面内容的元数据标题

页面大小
校验和
DBMS版本
交易可见度
压缩信息
在这里插入图片描述

  1. Tuple-Oriented Page Layout
    The most common page layout scheme is called slotted pages (分曹页面)。
    在这里插入图片描述

Record ID

  1. 为了跟踪单个元组,DBMS为每个元组分配一个唯一记录标识符

  2. 最常见的记录ID是(PageID,Slot#)

  • PageID:包含元组的页面的ID
  • Slot#:元组在页面中存储的插槽号
    在这里插入图片描述

Large Values

Most DBMSs don’t allow a tuple to exceed the size of a single page

To store values that are larger than a page, the DBMS uses separate overflow storage pages

大多数DBMS不允许元组超过单个页面的大小

为了存储大于页面的值,DBMS使用单独的溢出存储页面

Log-Structured Page Layout

Instead of storing tuples in pages, the DBMS only appends log records to the file of how the database was modified

  • Inserts store the entire tuple

  • Deletes mark the tuple as deleted

  • Updates contain the delta of just the attributes that were modified

To read a tuple, the DBMS scans the log backwards and “recreates” the tuple to find what it needs

DBMS不在页面中存储元组,而是仅将日志记录追加到如何修改数据库的文件中

  • 插入存储整个元组

  • 删除将元组标记为已删除

  • 更新仅包含已修改属性的增量

为了读取元组,DBMS向后扫描日志,然后“重新创建”元组以查找所需的元组
在这里插入图片描述
Build indexes to allow the DBMS to jump to locations in the log
在这里插入图片描述
Periodically compact the log

  • 日志大小
  • 系统负载
    在这里插入图片描述

磁盘文件组织形式

Heap File Organization

  1. A heap file (⌃áˆ) is an unordered collection of pages where tuples that are stored in random order
  • Create/get/write/delete pages

  • Must also support iterating over all pages

  1. Need meta-data to keep track of what pages exist and which ones have free space

  2. Two ways to represent a heap file

  • Approach #1: Linked lists

  • Approach #2: Page directory

1.堆文件(⌃áˆ)是无序的页面集合,其中元组以随机顺序存储

  • 创建/获取/写入/删除页面

  • 还必须支持遍历所有页面

2.需要元数据来跟踪存在哪些页面以及哪些页面具有可用空间

3.表示堆文件的两种方法

  • 方法1:链接列表

  • 方法2:页面目录

系统目录(System Catalogs)

A DBMS stores meta-data about databases in its internal catalogs

  • Tables, columns, indexes, views
  • Users, permissions
  • Internal statistics

Almost every DBMS stores their a database’s catalog in itself

You can query the DBMS’s internal INFORMATION SCHEMA catalog to get
info about the database

  • ANSI standard set of read-only views that provide info about all of the tables, views, columns, and procedures in a database

DBMSs also have non-standard shortcuts to retrieve this informatio

DBMS在其内部目录中存储有关数据库的元数据

  • 表,列,索引,视图
  • 用户,权限
  • 内部统计

几乎每个DBMS都会自己存储数据库目录

您可以查询DBMS的内部INFORMATION SCHEMA目录以获取
有关数据库的信息

  • ANSI标准的只读视图集,提供有关数据库中所有表,视图,列和过程的信息

DBMS还具有非标准的快捷方式来检索此信息。
在这里插入图片描述

缓冲管理(Buffer Management)

缓冲池 (Buffer Pool)

  • The available memory region is partitioned into an array of fixed-size pages, which are collectively called the buffer pool

  • The pages in the bu↵er pool are called frames

  • 可用的内存区域被分成固定大小的页面数组,这些页面统称为缓冲池

  • 缓冲池中的页面称为框架
    在这里插入图片描述

  1. Buffer Manager
    The buffer manager is responsible for bringing pages into the buffer pool as needed
  • The buffer manager decides what existing page in the bu↵er pool to replace to make space for the new page (if the bu↵er pool is full)

缓冲区管理器负责根据需要将页面带入缓冲池

  • 缓冲区管理器确定缓冲区池中要替换的现有页面,以为新页面腾出空间(如果缓冲区池已满)
  1. Buffer Pool Internals: Page Table
    The page table keeps track of pages that are currently in the buffer pool.

页面表跟踪当前在缓冲池中的页面。

  1. Page Requests
    1 Check the page table to see if some frame contains the requested
    page P
    2 If P is in the bu↵er pool, pin page P, i.e., increment the pin count
    of the frame containing P
    3 Return the pointer of the frame containing P
    Example: Request page #2

1 检查页表以查看某帧是否包含请求的页P
2 如果P在缓冲区池中,则在P页上固定引脚,即增加包含P的帧的引脚数
3 返回包含P的帧的指针示例:请求页面#2

  1. Buffer Pool Internals: Frame’s Meta-Data
    The buffer manager maintains two variables for each frame

pin count: the number of times that the page currently in the frame
has been requested but not released, i.e., the number of current users
of the page

The buffer manager maintains two variables for each frame

dirty: the status whether the page in the frame has been modified
since it was brought into the buffer pool

缓冲区管理器为每个帧维护两个变量

pin count:当前页面在框架中的次数已被请求但未被释放,即当前用户数页面的

缓冲区管理器为每个帧维护两个变量

dirty:框架中页面是否被修改的状态因为它被带入缓冲池

Page Requests (Cont’d)

如果请求的页面不在缓冲区池中,

  1. 选择针数= 0的框架进行更换,使用替换策略,并增加其引脚数
  2. 如果替换框架的脏位打开,则写入它包含到磁盘的页面
  3. 将请求的页面读入替换框中

总结

咱们玩归玩,闹归闹,别拿学习开玩笑。

本篇介绍了数据库系统中的存储管理,包括存储器,文件组织表示形式以及缓冲池等。学习时要注重牢记存储管理的知识要点。

猜你喜欢

转载自blog.csdn.net/JAck_chen0309/article/details/105111920