God_zmd 2021-06-29 13:36 采纳率: 59.2%
浏览 351
已采纳

redis的keys模糊查询会遍历所有的key吗

redis使用keys去模糊查询,会遍历redis中的所有key吗?

  • 写回答

1条回答 默认 最新

  • 小P聊技术 2021-06-29 15:39
    关注
    优先使用scan, 代替keys,scan每次遍历设置的值,对效率有较大的影响
    项目中亲测: 当开发环境缓存有10几条的时候,设置每次查询的条数为10,耗时2000毫秒左右
    设置每次查询的条数为100时,效率提升十倍
    
    Jedis jedis=RedisUtils.getConn();
    
    ScanParams scanParams = new ScanParams();
    
    scanParams.match(“key*”);
    **// 这里设置的key对效率影响较大
    **scanParams.count(100);
    Long startTime = System.currentTimeMillis();
    List<String> retList = new ArrayList<String>();
    final String scanRet = "0";
    do {
    ScanResult<String> ret = jedis.scan(scanRet, scanParams);
    scanRet = ret.getStringCursor();// 返回用于下次遍历的游标,非零则还有数据没遍历
    retList.addAll(ret.getResult());// 返回结果
    } while (!scanRet.equals("0"));
    System.out.println("retList size:"+retList.size());
    Long endTime = System.currentTimeMillis();
    System.out.println("using time is:" + (endTime - startTime)+"ms");
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?