redis - 常用高级命令keys,scan,Info

一、全量遍历键 keys

1)说明

用来列出所有满足特定正则字符串规则的key,当redis数据量比较大时,性能比较差,要避免使用。
通俗理解:若key值不多,直接使用keys获取所有数据可以;keys后可以使用通配符查询。但是keys是扫描全库的,若数据量很大,想想肯定性能就会慢

2)demo

127.0.0.1:6379> keys *
 1) "user:1:balance"
 2) "queue"
 3) "huohuo125"
 4) "test1"
 5) "user:2:balance"
 6) "huohuo123"
 7) "huocount"
 8) "user:2:name"
 9) "exists-key"
10) "test"
11) "not-test1-key"
12) "mykey"
13) "huo"
14) "page_rank"
15) "huohuo"
16) "huohuo124"
17) "user:1:name"
127.0.0.1:6379> keys huohuo*
1) "huohuo125"
2) "huohuo123"
3) "huohuo"
4) "huohuo124"
127.0.0.1:6379> keys huo*huo
1) "huo1huo"
2) "huohuo"
3) "huo2huo"

注:*可以放前中后位置,和mysql一样

二、渐进式遍历键 scan

1)说明

SCAN cursor [MATCH pattern] [COUNT count]
一共三个参数:

  • 第一个cursor 整数值(hash桶的索引值)
  • 第二个是 key 的正则模式
  • 第三个是一次遍历的key的数量(参考值,底层遍历的数量不一定),并不是符合条件的结果数量

2)对count的理解

对于count而言,获取的值是不准确的。因为在redis中是不存在索引的,所以达不到和mysql一样分页准确查询,通过scan也仅仅是实现渐进式遍历。就比如我要模糊查询huohuo* 想输出count3 ,那也得看获取的3条值里是否都符合我的查询条件,所以说这个count也就是一个参考值。
count3 有可能返回少于3或者大于3的结果集

3)对cursor的理解

取得是hash桶的索引值,这个就得看redis的底层数据结构了。简单点,其实redis底层就是一个hashmap,通过计算hash,放到对应的位置,cursor就是取得这个hash值。
第一次遍历时,cursor 值为 0,然后将返回结果中第一个整数值作为下一次遍历的 cursor。一直遍历到返回的 cursor 值为 0 时结束。

4)redis简单图解

后期会针对整理redis底层原理
在这里插入图片描述

4)demo

通过keys huohuo*,我们可以看到符合条件的结果集是4条

127.0.0.1:6379> keys huohuo*
1) "huohuo125"
2) "huohuo123"
3) "huohuo"
4) "huohuo124"
127.0.0.1:6379>

scan渐进式分页,查询结果如下:

127.0.0.1:6379> scan 0 match huohuo* count 2
1) "4"
2) (empty array)
127.0.0.1:6379> scan 4 match huohuo* count 2
1) "12"
2) (empty array)
127.0.0.1:6379> scan 12 match huohuo* count 2
1) "2"
2) 1) "huohuo"
127.0.0.1:6379> scan 2 match huohuo* count 2
1) "14"
2) (empty array)
127.0.0.1:6379> scan 14 match huohuo* count 2
1) "1"
2) (empty array)
127.0.0.1:6379> scan 1 match huohuo* count 2
1) "25"
2) 1) "huohuo125"
127.0.0.1:6379> scan 25 match huohuo* count 2
1) "3"
2) 1) "huohuo124"
127.0.0.1:6379> scan 3 match huohuo* count 2
1) "15"
2) (empty array)
127.0.0.1:6379> scan 15 match huohuo* count 2
1) "0"
2) 1) "huohuo123"

三、info

127.0.0.1:6379> info
# Server
redis_version:6.0.15
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:e95e6f5266fb21ce
redis_mode:standalone
os:Darwin 20.6.0 x86_64
arch_bits:64
multiplexing_api:kqueue
atomicvar_api:atomic-builtin
gcc_version:4.2.1
process_id:94976
run_id:222189a1374272334dca5d7e090669db6bbe6b1d
tcp_port:6379
uptime_in_seconds:1320753
uptime_in_days:15
hz:10
configured_hz:10
lru_clock:9709700
executable:/usr/local/redis-6.0.15/redis-server
config_file:/usr/local/redis-6.0.15/etc/redis.conf
io_threads_active:0

# Clients
connected_clients:1
client_recent_max_input_buffer:16
client_recent_max_output_buffer:0
blocked_clients:0
tracking_clients:0
clients_in_timeout_table:0

# Memory
used_memory:1137712
used_memory_human:1.09M
used_memory_rss:806912
used_memory_rss_human:788.00K
used_memory_peak:3854976
used_memory_peak_human:3.68M
used_memory_peak_perc:29.51%
used_memory_overhead:1098184
used_memory_startup:1079488
used_memory_dataset:39528
used_memory_dataset_perc:67.89%
allocator_allocated:1099296
allocator_active:769024
allocator_resident:769024
total_system_memory:8589934592
total_system_memory_human:8.00G
used_memory_lua:37888
used_memory_lua_human:37.00K
used_memory_scripts:0
used_memory_scripts_human:0B
number_of_cached_scripts:0
maxmemory:0
maxmemory_human:0B
maxmemory_policy:noeviction
allocator_frag_ratio:0.70
allocator_frag_bytes:18446744073709221344
allocator_rss_ratio:1.00
allocator_rss_bytes:0
rss_overhead_ratio:1.05
rss_overhead_bytes:37888
mem_fragmentation_ratio:0.73
mem_fragmentation_bytes:-292384
mem_not_counted_for_evict:196
mem_replication_backlog:0
mem_clients_slaves:0
mem_clients_normal:17440
mem_aof_buffer:208
mem_allocator:libc
active_defrag_running:0
lazyfree_pending_objects:0

# Persistence
loading:0
rdb_changes_since_last_save:0
rdb_bgsave_in_progress:0
rdb_last_save_time:1637047146
rdb_last_bgsave_status:ok
rdb_last_bgsave_time_sec:0
rdb_current_bgsave_time_sec:-1
rdb_last_cow_size:0
aof_enabled:1
aof_rewrite_in_progress:0
aof_rewrite_scheduled:0
aof_last_rewrite_time_sec:-1
aof_current_rewrite_time_sec:-1
aof_last_bgrewrite_status:ok
aof_last_write_status:ok
aof_last_cow_size:0
module_fork_in_progress:0
module_fork_last_cow_size:0
aof_current_size:2131
aof_base_size:1713
aof_pending_rewrite:0
aof_buffer_length:0
aof_rewrite_buffer_length:0
aof_pending_bio_fsync:0
aof_delayed_fsync:0

# Stats
total_connections_received:103
total_commands_processed:45
instantaneous_ops_per_sec:0
total_net_input_bytes:2602076
total_net_output_bytes:10020714
instantaneous_input_kbps:0.00
instantaneous_output_kbps:0.00
rejected_connections:0
sync_full:0
sync_partial_ok:0
sync_partial_err:0
expired_keys:0
expired_stale_perc:0.00
expired_time_cap_reached_count:0
expire_cycle_cpu_milliseconds:558728
evicted_keys:0
keyspace_hits:5
keyspace_misses:2
pubsub_channels:0
pubsub_patterns:0
latest_fork_usec:687
migrate_cached_sockets:0
slave_expires_tracked_keys:0
active_defrag_hits:0
active_defrag_misses:0
active_defrag_key_hits:0
active_defrag_key_misses:0
tracking_total_keys:0
tracking_total_items:0
tracking_total_prefixes:0
unexpected_error_replies:0
total_reads_processed:200149
total_writes_processed:200046
io_threaded_reads_processed:0
io_threaded_writes_processed:0

# Replication
role:master
connected_slaves:0
master_replid:4154e2fdc9c35942aaf4d934564373871339d58a
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0

# CPU
used_cpu_sys:259.603210
used_cpu_user:64.060310
used_cpu_sys_children:0.015204
used_cpu_user_children:0.002908

# Modules

# Cluster
cluster_enabled:0

# Keyspace
db0:keys=19,expires=0,avg_ttl=0

一共是分为9大块

  • Server 服务器运行的环境参数
  • Clients 客户端相关信息
connected_clients:1             正在连接的客户端指令
  • Memory 服务器运行内存统计数据
used_memory:1137712             Redis分配的内存总量(byte),包含redis进程内部的开销和数据占用的内存
used_memory_human:1.09M         Redis分配的内存总量(Kb,human会展示出单位)
used_memory_rss_human:788.00K   向操作系统申请的内存大小(Mb)(这个值一般是大于used_memory的,因为Redis的内存分配策略会产生内存碎片)
used_memory_peak:3854976        redis的内存消耗峰值(byte)
used_memory_peak_human:3.68M    redis的内存消耗峰值(KB)

maxmemory:0                     配置中设置的最大可使用内存值(byte),默认0,不限制  
maxmemory_human:0B              配置中设置的最大可使用内存值
maxmemory_policy:noeviction     当达到maxmemory时的淘汰策略
  • Persistence 持久化信息
  • Stats 通用统计数据
instantaneous_ops_per_sec:0     每秒执行多少次指令
  • Replication 主从复制相关信息
  • CPU CPU 使用情况
  • Cluster 集群信息
  • KeySpace 键值对统计数量信息
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值