|
1 2 3 4 5 6 7 8 9 10 |
String pattern = "test:1023*"; List<Object> listOfObjects = new ArrayList<>(); Cursor<Map.Entry<Object, Object>> cursorMap = redisTemplate.boundHashOps("test_hash") .scan(ScanOptions.scanOptions().match(pattern).count(100).build()); while (cursorMap.hasNext()) { listOfObjects.add(cursorMap.next().getKey()); } cursorMap.close(); |
第一次hscan 传入cursor=0, 返回新的cursor和结果集列表,第二次传入返回的cursor值,直到hscan返回cursor=0
100万数据集居然耗时5s, spring-data-redis 返回的cursor迭代时,先迭代hscan返回的数据,如果迭代完成会再次发起hscan,直到hscan返回的cursor=0为止。当前场景很容易错误以为只查询一次数据库,如果匹配的结果集很稀疏,那么将count设置大些能减少应用和数据库之间的往返时间。
reference:
Hscan in Redis with 1,000,000 records is taking longer than usual
Posted in: redis
Comments are closed.