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.