【问题标题】:How is Cassandra able to efficiently binary-search variable-length rows in its SSTables?Cassandra 如何在其 SSTables 中高效地对可变长度行进行二进制搜索?
【发布时间】:2021-06-11 23:30:41
【问题描述】:
据我了解,Cassandra 将允许给定表的不同行具有不同的长度。这可能会发生,因为 Cassandra 允许将带有映射和集合的列作为其类型,这些结构包含任意数量的元素(或者如果我们用 Thrift 术语思考,我们可以有宽行)。
我还知道 SSTables 以排序顺序存储给定表的记录,并带有辅助索引以帮助二进制搜索过程。很容易理解二进制搜索在表中的每一行都具有相同长度并且所有行都按顺序存储在磁盘上的世界中如何工作,但我很困惑如果它们如何有效地在记录之间“跳转”长度可变。
编辑:唯一想到的是索引结构是否包含对每一行开头的引用,但是在查找列的子集时我们仍然会遇到相同的问题(比如说, A 和 B)。
【问题讨论】:
标签:
database
cassandra
binary-search
【解决方案1】:
显然,通过阅读“Expert Apache Cassandra Administration (2018)”,索引文件包含(默认情况下)SSTable 中每 128 个键的偏移量。
Cassandra 能够对给定的键进行二进制搜索,粒度可达 128 个键块,然后不幸的是,需要从该偏移量开始线性扫描 SSTable,直到找到它正在寻找的键。
参考:
p124:
Index Files
An index file stores the offset of keys into the main data file, which is the SSTable.
By default, the database stores a part of the index file in memory.
The index file stores the offset for every 128th key in the SSTable,
and you can configure this value.
p127:
5. The database first performs a binary search on the sample index
to find the starting offset into the index file. Using this offset, the
database offsets into the index file and performs a sequential read
to get the offset into the SSTable for the actual key.
6. Using the offset for the actual key, the database offsets into the
SSTable file and returns the actual data from the SSTable by
performing a lookup of the SSTable.