redis批量查询优化(管道)

Redis集群模式:

redis集群 应该注意的问题:https://blog.csdn.net/zhang89xiao/article/details/51273970

Redis连接池---jedis-2.9.0+commons-pool2-2.4.2:https://blog.csdn.net/wangshuang1631/article/details/54091673

jedis连接redis3.2.9集群密码问题:https://blog.csdn.net/qq_35440040/article/details/80064908

 

Redis管道模式:pipeline

Redis中PipeLine使用(二)---批量get与批量set:https://blog.csdn.net/ouyang111222/article/details/49640899

redis事务、管道及消息通知探究:https://blog.csdn.net/koushr/article/details/50976515

在Redis集群中使用pipeline批量插入:https://www.cnblogs.com/drwong/p/4825752.html

redis集群客户端JedisCluster优化 - 管道(pipeline)模式支持:lib.csdn.net/article/redis/29350

最近做一个使用了大量redis查询的项目,发现查询速度并不是想象中那么快,在慢慢调试后发现,时间大多浪费在建立连接上去了,redis基于tcp连接的形式,每次查询数据都要经过三次握手,因而自己慢慢找原因。

终于在redis 的官方文档中看出,redis是一个基于tcp连接的的方式:

Redis is a TCP server using the client-server model and what is called a Request/Response protocol.

This means that usually a request is accomplished with the following steps:

1.The client sends a query to the server, and reads from the socket, usually in a blocking way, for the server response.
2.The server processes the command and sends the response back to the client.

因此在大量查询redis的过程中,时间大多花费在tcp的连接三次握手上;

例子如下:
在java中redis使用管道和不使用管道性能差:

long start=System.currentTimeMillis();  
      for (int i = 0; i <50000; i++) {  
          redis.set(String.valueOf(i),String.valueOf(i));  
      }  
      long end=System.currentTimeMillis();  
      logger.info("the total time is:"+(end-start));  

      Pipeline pipe=redis.pipelined();  
      long start_pipe=System.currentTimeMillis();  
      for (int i = 0; i <50000; i++) {  
          pipe.set(String.valueOf(i),String.valueOf(i));  
      }  
      pipe.sync();  
      long end_pipe=System.currentTimeMillis();  
      logger.info("the pipe total time is:"+(end_pipe-start_pipe));  


执行的结果是: 
8368

243

再看看官方一点的文档:

require 'rubygems'
require 'redis'

def bench(descr)
    start = Time.now
    yield
    puts "#{descr} #{Time.now-start} seconds"
end

def without_pipelining
    r = Redis.new
    10000.times {
        r.ping
    }
end

def with_pipelining
    r = Redis.new
    r.pipelined {
        10000.times {
            r.ping
        }
    }
end

bench("without pipelining") {
    without_pipelining
}
bench("with pipelining") {
    with_pipelining
}
long start=System.currentTimeMillis();  
      for (int i = 0; i <50000; i++) {  
          redis.set(String.valueOf(i),String.valueOf(i));  
      }  
      long end=System.currentTimeMillis();  
      logger.info("the total time is:"+(end-start));  

      Pipeline pipe=redis.pipelined();  
      long start_pipe=System.currentTimeMillis();  
      for (int i = 0; i <50000; i++) {  
          pipe.set(String.valueOf(i),String.valueOf(i));  
      }  
      pipe.sync();  
      long end_pipe=System.currentTimeMillis();  
      logger.info("the pipe total time is:"+(end_pipe-start_pipe));  
without pipelining 1.185238 seconds 
with pipelining 0.250783 seconds

redis作为存储,读取速度是mysql的10倍以上,但是再执行的时候发现性能不如mysql: 
究其原因,还是redis的连接花费的时间过多; 
用pipline 将redis查询请求一次发送给redis,一次性返回结果;

pipelining vs scripting
使用Redis scripting(在Redis版本2.6或更高版本中可用),可以使用在服务器端执行大量工作的脚本更有效地处理流水线的许多用例。脚本的一个很大的优点是它能够以最小的延迟读写数据,使得像读、计算、写操作这样的操作变得非常快(在这种情况下,pipeliniing无法起作用,因为客户端需要读取命令的应答才能调用write命令)。

备注:
Pipeline 的默认的同步的个数为53个,也就是说arges中累加到53条数据时会把数据提交
--------------------- 
作者:梧桐雨丶 
来源:CSDN 
原文:https://blog.csdn.net/u011181633/article/details/79889332?utm_source=copy 
版权声明:本文为博主原创文章,转载请附上博文链接!

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值