search

Home  >  Q&A  >  body text

Redis中keys()方法作用原理是什么?

鄙人蠢,读不懂redis源码~
有一天用redis-py连上Redis备用服务器,想看一下某类型的key有多少个。

rds = redis.Redis(ip, host)
print len(rds.keys('xxx_*'))

最后有大概400w个,阻塞了很久才算出来结果。
令我疑惑的是,这个客户端进程CPU占用率一直在100%以上(为什么会超过100%我也不知道),反倒内存占用率不高。

理论上keys的运算结果应该是服务端计算出来的,客户端接收数据顶多就是内存消耗多点儿,为什么实际情况中客户端CPU占用率如此之高,客户端自己做了什么奇怪的计算?

大家讲道理大家讲道理2775 days ago783

reply all(3)I'll reply

  • PHPz

    PHPz2017-04-21 11:17:47

    Traversing matches is definitely slow. The author has said that try not to use the keys command

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-21 11:17:47

    The reason for the high CPU is mainly decoding the transmission format and converting it into a python object

    reply
    0
  • PHP中文网

    PHP中文网2017-04-21 11:17:47

    Are you using top to watch? More than 100% is probably because of the multi-core CPU

    It is best not to use key. You can use redis-rdb-tools, a small tool of redis. Someone seems to have mentioned how to use it here

    reply
    0
  • Cancelreply