【发布时间】:2017-08-02 19:41:48
【问题描述】:
我有:
- 具有 400 000 000 行的数据库表 (Cassandra 3)
- 大约 10 000 个关键字的列表
- 预计这两个数据集都会随着时间的推移而增长
我需要:
- 检查指定列是否包含关键字
- 求和列中包含关键字的行数
我应该选择哪种方法?
方法一(二级索引):
- 创建辅助SASI index on the table
- 随时查找给定关键字“on fly”的匹配项
- 但是,我害怕
- 容量问题 - 二级索引会消耗额外的空间,对于这么大的表来说可能太多了
- 性能 - 我不确定是否可以在合理的时间内在数亿行中找到关键字
方法 2(Java 作业 - 蛮力):
- 不断迭代数据的 Java 作业
- 匹配项保存到缓存中
-
缓存在下一次迭代中更新
// Paginate throuh data... String page = null; do { PagingState state = page == null ? null : PagingState.fromString(page); PagedResult<DataRow> res = getDataPaged(query, status, PAGE_SIZE, state); // Iterate through the current page ... for (DataRow row : res.getResult()) { // Skip empty titles if (row.getTitle().length() == 0) { continue; } // Find match in title for (String k : keywords) { if (k.length() > row.getTitle().length()) { continue; } if (row.getTitle().toLowerCase().contains(k.toLowerCase()) { // TODO: SAVE match break; } } } status = res.getResult(); page = res.getPage(); // TODO: Wait here to reduce DB load } while (page != null); -
问题
- 遍历整个表可能非常慢。如果我每 1000 行等待一秒钟,那么这个循环将在 4.6 天内完成
- 这需要额外的缓存空间;此外,从缓存中频繁删除会在 Cassandra 中产生墓碑
【问题讨论】:
-
如何运行选项 2 并创建一个缓存(可以是外部资源)并为将来的更新使用过滤机制,在存储数据后更新缓存
标签: java cassandra substring cassandra-3.0