MAPPING TABLE

本文详细介绍了Oracle数据库中如何在分区或不分区的索引组织表上创建位图索引,以及创建位图索引时所需MAPPING TABLE的作用。MAPPING TABLE是一个HEAP表,用于存储索引组织表的逻辑ROWID。通过MAPPING TABLE,查询过程能够将位图索引中的ROWID转换为对应的物理ROWID,进而访问索引组织表或普通表。此外,文章还讨论了创建和删除位图索引及MAPPING TABLE的过程,并提供了重建和恢复MAPPING TABLE的方法。

Oracle supports bitmap indexes on partitioned and nonpartitioned index-organized tables.A mapping table is required for creating bitmap indexes on an index-organized table.


The mapping table is a heap-organized table that stores logical rowids of the index-organized table. Specifically,each mapping table row stores one logical rowid for the corresponding index-organized table row. Thus,the mapping table provides one-to-one mapping between logical rowids of the index-organized table rows and physical rowids of the mapping table rows.


A bitmap index on an index-organized table is similar to that on a heap-organized table except that the rowids used in the bitmap index on an index-organized table are those of the mapping table as opposed to the base table. There is one mapping table for each index-organized table and it is used by all the bitmap indexes created on that index-organized table.


In both heap-organized and index-organized base tables, a bitmap index is accessed using a search key. If the key is found, the bitmap entry is converted to a physical rowid. In the case of heap-organized tables, this physical rowid is then used to access the base table. However, in the case of index-organized tables, the physical rowid is then used to access the mapping table. The access to the mapping table yields a logical rowid. This logical rowid is used to access the index-organized table.


Though a bitmap index on an index-organized table does not store logical rowids, it is still logical in nature.


大意是说:Oracle数据库支持在分区或者不分区的索引组织表(IOT)上创建位图索引。在向索引组织表创建位图索引的时候必须要有MAPPING TABLE。

MAPPING TABLE是一个HEAP表(即普通表),用来存储索引组织表的逻辑rowid。具体来说,MAPPING TABLE的每一行存储了一个逻辑rowid,用来关联索引组织表的行。因此,MAPPING TABLE的物理rowid和索引组织表的行之间产生了一对一的关联。因为索引组织表的rowid会经常变化,所以只有逻辑rowid,而不像普通表的行具有物理rowid,可以直接对应到具体的物理存放地点。在索引组织表上创建的位图索引和在普通HEAP表上创建的位图索引是类似的,只不过如果是索引组织表,那位图索引会使用MAPPING TABLE的rowid。在查询的时候,2种表都是通过key字段访问,在key字段找到之后,普通HEAP表会直接把key转换成rowid,然后通过rowid定位到具体的数据块中。但是如果是索引组织表,会通过key转换成rowid后,到MAPPING TABLE中去查找逻辑rowid,然后通过这个逻辑rowid去访问索引组织表里的数据。


SQL> create table t_testmap(eno number primary key,ename varchar2(10)) organization index;


Table created.


SQL> create bitmap index idx_map on t_testmap(ename);
create bitmap index idx_map on t_testmap(ename)
                               *
ERROR at line 1:
ORA-28669: bitmap index can not be created on an IOT with no mapping table

无法在没有mapping table的情况下给IOT创建位图索引。


SQL> create table t_testmap2(eno number primary key,ename varchar2(10)) organization index mapping table;


Table created.


SQL> create bitmap index idx_map on t_testmap2(ename);


Index created.


位图索引创建成功。


SQL> select table_name,iot_name,iot_type from user_tables;


TABLE_NAME                                   IOT_NAME                        IOT_TYPE
------------------------------            ------------------------------             ---------------------
SYS_IOT_MAP_10258                 T_TESTMAP2                     IOT_MAPPING
T_TESTMAP2                                          IOT
T_TESTMAP                                             IOT

SQL> select index_name, index_type, table_name, uniqueness from user_indexes;


INDEX_NAME                     INDEX_TYPE                  TABLE_NAME                     UNIQUENES
------------------------------    ---------------------------         ------------------------------       ------------------
IDX_MAP                                      BITMAP                      T_TESTMAP2                     NONUNIQUE
SYS_IOT_TOP_10258              IOT - TOP                   T_TESTMAP2                     UNIQUE
SYS_IOT_TOP_10256              IOT - TOP                   T_TESTMAP                       UNIQUE

这里可以看出索引组织表虽然是表,但是存储的结构是索引


SQL> drop table SYS_IOT_MAP_10258;
drop table SYS_IOT_MAP_10258
           *
ERROR at line 1:
ORA-28668: cannot reference mapping table of an index-organized table


mapping table 无法被直接删除,但是可以通过move进行删除


SQL> alter table t_testmap2 move nomapping;
alter table t_testmap2 move nomapping
            *
ERROR at line 1:
ORA-28670: mapping table cannot be dropped due to an existing bitmap index

在创建了bitmap index的时候,mapping table也无法被删除。


SQL> drop index idx_map;


Index dropped.


SQL> alter table t_testmap2 move nomapping;


Table altered.


SQL> select table_name,iot_name,iot_type from user_tables;


TABLE_NAME                     IOT_NAME                       IOT_TYPE
------------------------------ ------------------------------ ------------
T_TESTMAP2                                                                     IOT
T_TESTMAP                                                                       IOT


这时再查询就发现mapping table被删除了。


SQL> select object_name, original_name, type from recyclebin;


no rows selected

而且这种删除不会把mapping table放入回收站。

如果因为误操作等原因想恢复mapping table,可以使用以下命令重建。


SQL> alter table t_testmap move mapping table;


Table altered.


SQL> select table_name,iot_name,iot_type from user_tables;


TABLE_NAME                     IOT_NAME                        IOT_TYPE
------------------------------    -----------------------            --------------------------
SYS_IOT_MAP_10256     T_TESTMAP                      IOT_MAPPING
T_TESTMAP2                                                                          IOT
T_TESTMAP                                                                            IOT


此时mapping table又重新创建。


SQL> create bitmap index idx_map on t_testmap(ename);


Index created.

重新创建索引以进行下面的实验。


SQL> drop table t_testmap;


Table dropped.


SQL> select object_name, original_name, type from recyclebin;


OBJECT_NAME                                         ORIGINAL_NAME                             TYPE
------------------------------                             --------------------------------            -------------------------
BIN$tKX6TiUGBMLgQAB/AQATjQ==$0    IDX_MAP                                        INDEX
BIN$tKX6TiUHBMLgQAB/AQATjQ==$0    SYS_IOT_TOP_10256                IOT TOP INDEX
SYS_IOT_MAP_10256                                 SYS_IOT_MAP_10256                IOT MAPPING TABLE
BIN$tKX6TiUIBMLgQAB/AQATjQ==$0       T_TESTMAP                                 TABLE


如果直接删除表t_testmap,那么该索引组织表上关联的mapping table 和 bitmap index 会一起被放入回收站中。


SQL> flashback table t_testmap to before drop;


Flashback complete.


SQL> select table_name,iot_name,iot_type from user_tables;


TABLE_NAME                                    IOT_NAME                         IOT_TYPE
------------------------------         ------------------------------               ------------
SYS_IOT_MAP_10256                     T_TESTMAP                      IOT_MAPPING
T_TESTMAP                                       IOT
T_TESTMAP2                                     IOT


SQL> select index_name, index_type, table_name from user_indexes;


INDEX_NAME                                                      INDEX_TYPE                  TABLE_NAME
------------------------------ --------                     ---------------------------               ----------------------
BIN$tKX6TiUGBMLgQAB/AQATjQ==$0            BITMAP                            T_TESTMAP
BIN$tKX6TiUHBMLgQAB/AQATjQ==$0            IOT - TOP                        T_TESTMAP
SYS_IOT_TOP_10258                                         IOT - TOP                        T_TESTMAP2


如果使用闪回的话,那么表及其关联的mapping table , bitmap index 会一起恢复

UFS(Universal Flash Storage)是一种用于移动设备和嵌入式系统的高速存储接口标准。UFS映射表(Mapping Table)在UFS协议中起着至关重要的作用,主要负责逻辑块地址(LBA, Logical Block Address)到物理块地址(PBA, Physical Block Address)的映射。这种机制类似于SSD中的FTL(Flash Translation Layer),用于管理闪存的逻辑地址与物理地址之间的关系。 ### UFS映射表的基本原理 UFS映射表的主要功能是将主机发送的逻辑地址转换为实际的物理地址。UFS设备内部的控制器会维护一个映射表,该表记录了每个逻辑块地址对应的物理块地址。当主机请求读取或写入数据时,控制器会根据映射表查找相应的物理地址,并执行操作。这种机制使得UFS设备能够高效地管理存储空间,同时隐藏了底层闪存的复杂性。 UFS映射表的设计需要考虑以下几点: 1. **地址转换**:逻辑地址到物理地址的映射是UFS映射表的核心功能。由于闪存的特性,数据不能直接覆盖写入,因此需要动态管理地址映射。 2. **磨损均衡(Wear Leveling)**:为了延长闪存的使用寿命,UFS控制器会通过映射表实现磨损均衡算法,确保数据写入均匀分布到所有物理块上。 3. **垃圾回收(Garbage Collection)**:当某些物理块中的数据被标记为无效时,垃圾回收机制会将这些块中的有效数据迁移到新的块中,并更新映射表,以便释放无效数据占用的空间。 4. **坏块管理(Bad Block Management)**:UFS映射表还需要处理闪存中的坏块问题,当检测到坏块时,控制器会将数据迁移到备用块,并更新映射表以反映新的地址映射[^1]。 ### UFS映射表的实现细节 UFS映射表的实现通常依赖于FTL(Flash Translation Layer)技术。FTL是一种软件或硬件机制,负责管理逻辑地址与物理地址之间的映射。UFS设备中的FTL可以采用以下几种方式实现: 1. **全映射(Full Mapping)**:在这种模式下,每个逻辑块地址都有一个对应的物理块地址。映射表存储在内存中,适用于小容量设备。由于映射表的大小与存储容量成正比,因此不适用于大容量设备。 2. **部分映射(Partial Mapping)**:为了减少内存占用,部分映射模式仅存储部分逻辑块地址的映射信息。未存储的映射信息可以通过其他方式推导或计算。 3. **混合映射(Hybrid Mapping)**:结合全映射和部分映射的优点,混合映射模式将频繁访问的逻辑块地址存储在内存中,而较少访问的地址则通过其他方式管理。 UFS映射表的实现还涉及以下关键技术: - **缓存机制**:为了提高性能,UFS控制器通常会使用高速缓存来存储映射表的部分内容。这样可以减少地址转换的延迟。 - **日志结构(Log-Structured)**:某些UFS设备采用日志结构的FTL,将数据写入连续的物理块中,并通过映射表管理逻辑地址与物理地址的关系。 - **错误纠正(Error Correction)**:UFS映射表还需要结合错误纠正码(ECC, Error Correction Code)技术,以确保数据的完整性和可靠性[^2]。 ### UFS映射表的应用场景 UFS映射表广泛应用于移动设备、嵌入式系统和高性能存储设备中。例如,智能手机和平板电脑中的UFS存储设备依赖映射表来管理数据的存储和访问。此外,UFS映射表也用于企业级存储解决方案,以提供高速、可靠的存储服务。 ### 示例代码 以下是一个简单的示例代码,展示了如何通过逻辑地址查找物理地址的映射关系(假设映射表以数组形式存储): ```c #include <stdio.h> #include <stdint.h> #define MAX_LBA 1024 // 假设最大逻辑块地址为1024 #define MAX_PBA 2048 // 假设最大物理块地址为2048 // 映射表,记录逻辑块地址到物理块地址的映射 uint32_t mapping_table[MAX_LBA]; // 初始化映射表 void init_mapping_table() { for (int i = 0; i < MAX_LBA; i++) { mapping_table[i] = i; // 初始情况下,逻辑地址等于物理地址 } } // 查找物理地址 uint32_t get_physical_address(uint32_t logical_address) { if (logical_address >= MAX_LBA) { return -1; // 地址越界 } return mapping_table[logical_address]; } int main() { init_mapping_table(); uint32_t logical_address = 100; uint32_t physical_address = get_physical_address(logical_address); printf("Logical Address %u maps to Physical Address %u\n", logical_address, physical_address); return 0; } ``` ### 总结 UFS映射表是UFS协议中的核心组件,负责管理逻辑地址与物理地址之间的映射。通过地址转换、磨损均衡、垃圾回收和坏块管理等机制,UFS映射表确保了存储设备的高效性和可靠性。其具体实现依赖于FTL技术,并结合缓存、日志结构和错误纠正等方法来优化性能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值