Redis2w大hash整取效率测试

由于需求,需要对容量2w的redis哈希进行整村整取,因此针对hash的常用命令进行效率测试;
测试通过golang语言的redis客户端库进行;

对redis的 HGETALL, HGET, HMGET,以及 pipeline下这几个命令的效率进行测试;测试结果:
效率: HGETALL > HMGET with pipe ≈ HMGET > HGET with pipe > HGET
个人理解:

  • hmgethgetall时间复杂度应该均是 O(n), 但 hmget基于入参field数量,而hgetall基于整个hash数量;对于整取的情况,两者区别不大;但 hmget 需要获取所有的fields,因此 hgetall在整取上表现更好;
  • 对于 hget, 其时间复杂度为 O(1), 但每一个key,每一个值均需要单独获取;通常批量操作在大规模查询或设置值时均优于单个命令的操作,通常会有更多的网络延时等的影响;
  • 对于pipeline, 所有语句一次提交到服务端,因此其消耗更少;

测试代码如下:

func BenchmarkRedisHash_hgetall(b *testing.B) {
	b.StopTimer()
	key := "{LicenseCenter}_GLOBAL_TotalAuth"
	client := getClient()
	b.StartTimer()
	for i:=1; i< b.N; i++ {
		_ = client.HGetAll(key).Val()
	}
}

func BenchmarkRedisHash_hget(b *testing.B) {
	b.StopTimer()
	key := "{LicenseCenter}_GLOBAL_TotalAuth"
	client := getClient()
	b.StartTimer()
	for i :=0; i

测试结果:

goos: windows
goarch: amd64
pkg: ******
BenchmarkRedisHash_hgetall-4          	     100	  84444830 ns/op
BenchmarkRedisHash_hget-4             	       1	2285130700 ns/op
BenchmarkRedisHash_hgetWithPipe-4     	      10	 178910230 ns/op
BenchmarkRedisHash_hmgetWitchPipe-4   	      10	 149308540 ns/op
BenchmarkRedisHash_hmget-4            	      10	 150108590 ns/op
PASS

如有错误,欢迎指正;同时期待告知更好的整取大hash的方式;

你可能感兴趣的:(数据库)