The Secrets of Oracle Bitmap Indexes

本文详细介绍了Oracle数据库中Bitmap索引的特点与应用场景。Bitmap索引适用于低基数列,并且在数据仓库环境中表现优异。文章还探讨了Bitmap索引的优势与劣势,包括其压缩特性、快速读取能力以及维护开销等。

Overview

Oracle's two major index types are Bitmap indexes and B-Tree indexes. B-Tree indexes are the regular type that OLTP systems make much use of, and bitmap indexes are a highly compressed index type that tends to be used primarily for data warehouses.

Characteristic of Bitmap Indexes

  • For columns with very few unique values (low cardinality)

Columns that have low cardinality are good candidates (if the cardinality of a column is <= 0.1 %  that the column is ideal candidate, consider also 0.2% – 1%)

  • Tables that have no or little insert/update are good candidates (static data in warehouse)
     
  • Stream of bits: each bit relates to a column value in a single row of table

create bitmap index person_region on person (region);

        Row     Region   North   East   West   South
        1       North        1      0      0       0
        2       East         0      1      0       0
        3       West         0      0      1       0
        4       West         0      0      1       0
        5       South        0      0      0       1
        6       North        1      0      0       0

Advantage of Bitmap Indexes

The advantages of them are that they have a highly compressed structure, making them fast to read and their structure makes it possible for the system to combine multiple indexes together for fast access to the underlying table.

Compressed indexes, like bitmap indexes, represent a trade-off between CPU usage and disk space usage. A compressed structure is faster to read from disk but takes additional CPU cycles to decompress for access - an uncompressed structure imposes a lower CPU load but requires more bandwidth to read in a short time.

One belief concerning bitmap indexes is that they are only suitable for indexing low-cardinality data. This is not necessarily true, and bitmap indexes can be used very successfully for indexing columns with many thousands of different values.

Disadvantage of Bitmap Indexes

The reason for confining bitmap indexes to data warehouses is that the overhead on maintaining them is enormous. A modification to a bitmap index requires a great deal more work on behalf of the system than a modification to a b-tree index. In addition, the concurrency for modifications on bitmap indexes is dreadful.

Bitmap Indexes and Deadlocks

Bitmap indexes are not appropriate for tables that have lots of single row DML operations (inserts) and especially concurrent single row DML operations. Deadlock situations are the result of concurrent inserts as the following example shows: Open two windows, one for Session 1 and one for Session 2

Session 1 Session 2

create table bitmap_index_demo (
  value varchar2(20)
);

insert into bitmap_index_demo
select decode(mod(rownum,2),0,'M','F')
  from all_objects;

 

create bitmap index
  bitmap_index_demo_idx
  on bitmap_index_demo(value);

insert into bitmap_index_demo
  values ('M');
1 row created.

insert into bitmap_index_demo
  values ('F');
1 row created.

insert into bitmap_index_demo
  values ('F');
...... waiting ......

ERROR at line 1:
ORA-00060: deadlock detected while waiting for resource

insert into bitmap_index_demo
  values ('M');
...... waiting ......

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/23895263/viewspace-699995/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/23895263/viewspace-699995/

在 Android 开发中,`Bitmap` 是图像处理中最常用的数据结构,但也是内存消耗大户。为了优化内存使用和提升性能,**Bitmap 压缩策略**非常重要。下面是常见的 Bitmap 压缩策略及使用方法: --- ### 一、Bitmap 压缩策略分类 #### 1. **质量压缩(Quality Compression)** **原理**:通过降低图片的 JPEG 质量来减少文件大小,不改变图片的尺寸。 **适用场景**:保存图片到本地、上传图片等。 ```java Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image); ByteArrayOutputStream baos = new ByteArrayOutputStream(); // 100 表示不压缩,数值越小压缩率越高 bitmap.compress(Bitmap.CompressFormat.JPEG, 50, baos); byte[] compressedData = baos.toByteArray(); // 如果需要再转成 Bitmap Bitmap compressedBitmap = BitmapFactory.decodeByteArray(compressedData, 0, compressedData.length); ``` > ⚠️ 注意:JPEG 支持质量压缩,PNG 不支持(因为 PNG 是无损压缩)。 --- #### 2. **尺寸压缩(Scaling)** **原理**:按比例缩小图片的宽高,减少像素数量,从而降低内存占用。 **适用场景**:显示缩略图、头像等不需要高清图的场景。 ```java Bitmap originalBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image); float scale = 0.5f; // 缩小为原来的一半 Matrix matrix = new Matrix(); matrix.setScale(scale, scale); Bitmap scaledBitmap = Bitmap.createBitmap(originalBitmap, 0, 0, originalBitmap.getWidth(), originalBitmap.getHeight(), matrix, true); ``` --- #### 3. **采样率压缩(inSampleSize)** **原理**:加载图片时设置 `BitmapFactory.Options.inSampleSize`,按比例加载图片,减少内存占用。 **适用场景**:从资源文件或本地加载大图时。 ```java BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeResource(getResources(), R.drawable.large_image, options); // 设置 inSampleSize 为 2,表示宽高各缩小为原来的 1/2,像素数变为 1/4 options.inJustDecodeBounds = false; options.inSampleSize = calculateInSampleSize(options, 1024, 768); Bitmap sampledBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.large_image, options); // 计算 inSampleSize 的方法 public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) { final int width = options.outWidth; final int height = options.outHeight; int inSampleSize = 1; if (height > reqHeight || width > reqWidth) { final int halfWidth = width / 2; final int halfHeight = height / 2; while ((halfWidth / inSampleSize) >= reqWidth && (halfHeight / inSampleSize) >= reqHeight) { inSampleSize *= 2; } } return inSampleSize; } ``` --- #### 4. **Bitmap 内存复用(BitmapPool)** **原理**:复用已有的 Bitmap 对象,避免频繁创建和回收,减少内存抖动。 **适用场景**:频繁加载图片的场景(如 RecyclerView)。 ```java Bitmap reusableBitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888); BitmapFactory.Options options = new BitmapFactory.Options(); options.inBitmap = reusableBitmap; // 复用该 Bitmap options.inJustDecodeBounds = false; Bitmap decodedBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image, options); ``` > ⚠️ 注意:`inBitmap` 要求复用的 Bitmap 必须是可变的(mutable),并且格式兼容。 --- ### 二、综合使用策略建议 | 场景 | 推荐策略 | |------|----------| | 显示缩略图 | 尺寸压缩 + inSampleSize | | 图片上传 | 质量压缩 + 尺寸压缩 | | 加载大图 | inSampleSize + BitmapFactory | | 频繁加载图片 | inBitmap + inSampleSize | --- ### ✅ 总结 | 压缩方式 | 是否改变尺寸 | 是否减少内存占用 | 是否影响画质 | 适用场景 | |----------|---------------|------------------|----------------|----------| | 质量压缩 | 否 | 否(内存)/是(文件) | 是 | 图片保存、上传 | | 尺寸压缩 | 是 | 是 | 是 | 显示缩略图 | | inSampleSize | 是 | 是 | 是 | 加载大图 | | inBitmap | 否 | 是(减少频繁分配) | 否 | 高频图片加载 | ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值