设集合元素个数为n,数组大小为m, 散列函数个数为k

有一个规律是当 k=m/n*ln2 时,误算率最低。参考Bloom_filter wiki

rocksdb中memtable和SST file都属于集合类数据且不需要删除数据,比较适合于Bloom filter.

rocksdb memtable和SST file都支持bloom filter, memtable 的bloom filter数组就存储在内存中,而SST file的bloom filter持久化在bloom filter中.

  • SST Bloom filter
    SST Boomfilter 在Flush生成SST files时通过计算产生,分为两个阶段
    1. 将hash_entries_所有映射到Bloom filter的数组中

SST Bloom filter相关参数有

filter_policy=bloomfilter:10:false;其中bits_per_key_=10, bits_per_key_实际就是前面公式k=m/n*ln2 中的m/n. 从而如下计算k即num_probes_的方式

use_block_based_builder_表示是使用block base filter还是full filter partition_filters 表示时否使用partitioned filter,SST数据有序排列,按block_size进行分区后再产生filter,index_on_filter block存储分区范围. 开启partition_filters 需配置index_type =kTwoLevelIndexSearch

filter 参数优先级如下 block base > partitioned > full. 比如说同时指定use_block_based_builder_=true和partition_filters=true实际使用的block based filter

whole_key_filtering,取值true, 表示增加全key的filter. 它和前缀filter并不冲突可以共存。

屏幕快照 2017-08-31 上午10.46.09.png

  • memtable 在每次Add数据时都会更新Bloom filter.
  • Bloom filter提供参数memtable_prefix_bloom_size_ratio,其值不超过0.25, Bloom filter数组大小为write_buffer_size* memtable_prefix_bloom_size_ratio.
  • memtable Bloom filter 中的num_probes_取值硬编码为6

在myrocks中,Bloom filter是全局的,设置了Bloom filter后,所有表都有Bloom filter。Bloom filter和索引是绑定在一起的。也就是说,表在查询过程中,如果可以用到某个索引,且设置了Bloom filter,那么就有可能会用到索引的Bloom filter.

MyRocks可以使用Bloom filter的条件如下,详见函数can_use_bloom_filter

  • 必须是索引前缀或索引全列的等值查询

我们可以通过以下两个status变量来观察Bloom filter使用情况 rocksdb_bloom_filter_prefix_checked:是否使用了Bloom filter rocksdb_bloom_filter_prefix_useful:使用Bloom filter判断出不存在 rocksdb_bloom_filter_useful:BlockBasedTable::Get接口使用Bloom filter判断出不存在

设置参数rocksdb_skip_bloom_filter_on_read可以让查询不使用Bloom filter。

最后给个示例 参数设置如下,使用partitioned filter

SQL