Home Database Mysql Tutorial Redis数据类型之HASH类型

Redis数据类型之HASH类型

Jun 07, 2016 pm 03:53 PM

Redis hash 是一个 string 类型的 field 和 value 的映射表.它的添加、 删除操作都是 O(1) (平均) 。 hash 特别适合用于存储对

HASH类型-特点

Redis hash 是一个 string 类型的 field 和 value 的映射表.它的添加、 删除操作都是 O(1) (平均) 。
hash 特别适合用于存储对象。 相较于将对象的每个字段存成单个 string 类型。 将一个对象存储在 hash 类型中会占用更少的内存,并且可以更方便的存取整个对象。省内存的原因是新建一个 hash 对象时开始是用 zipmap(又称为 small hash)来存储的。这个 zipmap 其实并不是 hash table,但是 zipmap 相比正常的 hash 实现可以节省不少 hash 本身需要的一些元数据存储开销。尽管 zipmap 的添加,删除,查找都是 O(n),但是由于一般对象的 field 数量都不太多。所以使用 zipmap 也是很快的,也就是说添加删除平均还是 O(1)。如果 field 或者 value的大小超出一定限制后, Redis 会在内部自动将 zipmap 替换成正常的 hash 实现. 这个限制可以在配置文件中指定hash-max-zipmap-entries 64 #配置字段最多 64 个hash-max-zipmap-value 512 #配置 value 最大为 512 字节

HASH常见命令
  • HSET
    HSET key field value
    将哈希表 key 中的域 field 的值设为 value 。
    如果 key 不存在,一个新的哈希表被创建并进行HSET 操作。
    如果域 field 已经存在于哈希表中,旧值将被覆盖。
    时间复杂度: O(1)
    返回值:
      如果 field 是哈希表中的一个新建域,并且值设置成功,返回 1 。
      如果哈希表中域 field 已经存在且旧值已被新值覆盖,返回 0 。

    127> "" (integer) 1 127> "" (integer) 0
  • HSETNX
    HSETNX key field value
    将哈希表 key 中的域 field 的值设置为 value ,当且仅当域 field 不存在。若域 field 已经存在,该操作无效。
    如果 key 不存在,一个新哈希表被创建并执行HSETNX 命令。
    时间复杂度: O(1)
    返回值:
      设置成功,返回 1 。
      如果给定域已经存在且没有操作被执行,返回 0 。

    127> (integer) 1 127> (integer) 0 127> "redis"
  • HMSET
    HMSET key field value [field value …]
    同时将多个 field-value (域 -值) 对设置到哈希表 key 中。此命令会覆盖哈希表中已存在的域。
    如果 key 不存在,一个空哈希表被创建并执行HMSET 操作。
    时间复杂度: O(N),N 为 field-value 对的数量。
    返回值:
      如果命令执行成功,返回 OK 。
      当 key 不是哈希表 (hash) 类型时,返回一个错误。

    127> 127> "" 127> ""
  • HGET
    HGET key field
    返回哈希表 key 中给定域 field 的值。
    时间复杂度: O(1)
    返回值:
      给定域的值。
      当给定域不存在或是给定 key 不存在时,返回 nil 。

    127> (integer) 1 127> "redis.com" 127> (nil)
  • HMGET
    返回哈希表 key 中,一个或多个给定域的值。
    如果给定的域不存在于哈希表,那么返回一个 nil 值。
    因为不存在的 key 被当作一个空哈希表来处理,所以对一个不存在的 key 进行HMGET 操作将返回一个只 带有 nil 值的表。
    时间复杂度: O(N),N 为给定域的数量。
    返回值: 一个包含多个给定域的关联值的表,表值的排列顺序和给定域参数的请求顺序一样。

    [.[15]> HMGET pet dog cat fake_pet ) (nil)
  • HEXISTS
    HEXISTS key field
    查看哈希表 key 中,给定域 field 是否存在。
    时间复杂度: O(1)
    返回值:
      如果哈希表含有给定域,返回 1 。
      如果哈希表不含有给定域,或 key 不存在,返回 0 。

    127> (integer) 0 127> (integer) 1 127> (integer) 1
  • HDEL
    HDEL key field [field …]
    删除哈希表 key 中的一个或多个指定域,不存在的域将被忽略。
    时间复杂度: O(N),N 为要删除的域的数量。
    返回值: 被成功移除的域的数量,不包括被忽略的域。

    :6379[15]> HMSET key f1 "v1" f2 "v2" f3 "v3" f4 "v4" OK :) ) ) ) :6379[15]> HDEL key f1 (:6379[15]> HDEL key not-field (:6379[15]> HDEL key f2 f3 (:6379[15]> HDEL key f4 f1 (integer) 1
  • HKEYS
    HKEYS key
    返回哈希表 key 中的所有域。
    时间复杂度: O(N),N 为哈希表的大小。
    返回值:
      一个包含哈希表中所有域的表。
      当 key 不存在时,返回一个空表。

    127> 127> HKEYS website 1) "google" 2) "yahoo" 127> EXISTS fake_key (integer) 0 127> HKEYS fake_key ()
  • HVALS
    HVALS key
    返回哈希表 key 中所有域的值。
    时间复杂度: O(N),N 为哈希表的大小。
    返回值:
      一个包含哈希表中所有值的表。
      当 key 不存在时,返回一个空表。

    127> 127> HVALS website 1) "" 2) "" 127> EXISTS not_exists (integer) 0 127> HVALS not_exists ()
  • HGETALL
    HGETALL key
    返回哈希表 key 中,所有的域和值。
    在返回值里,紧跟每个域名 (field name) 之后是域的值 (value),所以返回值的长度是哈希表大小的两倍。
    时间复杂度: O(N),N 为哈希表的大小。
    返回值:
      以列表形式返回哈希表的域和域的值。
      若 key 不存在,返回空列表。

    :6379[15]> HSET people jack "Jack Sparrow" (:6379[15]> HSET people gump "Forrest Gump" (:6379[15]> HGETALL people ) "gump" 4) "Forrest Gump"
  • Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    Video Face Swap

    Video Face Swap

    Swap faces in any video effortlessly with our completely free AI face swap tool!

    Hot Tools

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    Explain InnoDB Full-Text Search capabilities. Explain InnoDB Full-Text Search capabilities. Apr 02, 2025 pm 06:09 PM

    InnoDB's full-text search capabilities are very powerful, which can significantly improve database query efficiency and ability to process large amounts of text data. 1) InnoDB implements full-text search through inverted indexing, supporting basic and advanced search queries. 2) Use MATCH and AGAINST keywords to search, support Boolean mode and phrase search. 3) Optimization methods include using word segmentation technology, periodic rebuilding of indexes and adjusting cache size to improve performance and accuracy.

    When might a full table scan be faster than using an index in MySQL? When might a full table scan be faster than using an index in MySQL? Apr 09, 2025 am 12:05 AM

    Full table scanning may be faster in MySQL than using indexes. Specific cases include: 1) the data volume is small; 2) when the query returns a large amount of data; 3) when the index column is not highly selective; 4) when the complex query. By analyzing query plans, optimizing indexes, avoiding over-index and regularly maintaining tables, you can make the best choices in practical applications.

    Can I install mysql on Windows 7 Can I install mysql on Windows 7 Apr 08, 2025 pm 03:21 PM

    Yes, MySQL can be installed on Windows 7, and although Microsoft has stopped supporting Windows 7, MySQL is still compatible with it. However, the following points should be noted during the installation process: Download the MySQL installer for Windows. Select the appropriate version of MySQL (community or enterprise). Select the appropriate installation directory and character set during the installation process. Set the root user password and keep it properly. Connect to the database for testing. Note the compatibility and security issues on Windows 7, and it is recommended to upgrade to a supported operating system.

    Difference between clustered index and non-clustered index (secondary index) in InnoDB. Difference between clustered index and non-clustered index (secondary index) in InnoDB. Apr 02, 2025 pm 06:25 PM

    The difference between clustered index and non-clustered index is: 1. Clustered index stores data rows in the index structure, which is suitable for querying by primary key and range. 2. The non-clustered index stores index key values ​​and pointers to data rows, and is suitable for non-primary key column queries.

    What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)? What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)? Mar 21, 2025 pm 06:28 PM

    Article discusses popular MySQL GUI tools like MySQL Workbench and phpMyAdmin, comparing their features and suitability for beginners and advanced users.[159 characters]

    How do you handle large datasets in MySQL? How do you handle large datasets in MySQL? Mar 21, 2025 pm 12:15 PM

    Article discusses strategies for handling large datasets in MySQL, including partitioning, sharding, indexing, and query optimization.

    MySQL: Simple Concepts for Easy Learning MySQL: Simple Concepts for Easy Learning Apr 10, 2025 am 09:29 AM

    MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

    Explain different types of MySQL indexes (B-Tree, Hash, Full-text, Spatial). Explain different types of MySQL indexes (B-Tree, Hash, Full-text, Spatial). Apr 02, 2025 pm 07:05 PM

    MySQL supports four index types: B-Tree, Hash, Full-text, and Spatial. 1.B-Tree index is suitable for equal value search, range query and sorting. 2. Hash index is suitable for equal value searches, but does not support range query and sorting. 3. Full-text index is used for full-text search and is suitable for processing large amounts of text data. 4. Spatial index is used for geospatial data query and is suitable for GIS applications.

    See all articles