search
HomeDatabaseMysql TutorialRedis数据类型之HASH类型

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
    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
    Explain the InnoDB Buffer Pool and its importance for performance.Explain the InnoDB Buffer Pool and its importance for performance.Apr 19, 2025 am 12:24 AM

    InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

    MySQL vs. Other Programming Languages: A ComparisonMySQL vs. Other Programming Languages: A ComparisonApr 19, 2025 am 12:22 AM

    Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages ​​such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages ​​have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

    Learning MySQL: A Step-by-Step Guide for New UsersLearning MySQL: A Step-by-Step Guide for New UsersApr 19, 2025 am 12:19 AM

    MySQL is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.

    MySQL: Essential Skills for Beginners to MasterMySQL: Essential Skills for Beginners to MasterApr 18, 2025 am 12:24 AM

    MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

    MySQL: Structured Data and Relational DatabasesMySQL: Structured Data and Relational DatabasesApr 18, 2025 am 12:22 AM

    MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

    MySQL: Key Features and Capabilities ExplainedMySQL: Key Features and Capabilities ExplainedApr 18, 2025 am 12:17 AM

    MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

    The Purpose of SQL: Interacting with MySQL DatabasesThe Purpose of SQL: Interacting with MySQL DatabasesApr 18, 2025 am 12:12 AM

    SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

    MySQL for Beginners: Getting Started with Database ManagementMySQL for Beginners: Getting Started with Database ManagementApr 18, 2025 am 12:10 AM

    The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA

    See all articles

    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

    MantisBT

    MantisBT

    Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

    Dreamweaver Mac version

    Dreamweaver Mac version

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    PhpStorm Mac version

    PhpStorm Mac version

    The latest (2018.2.1) professional PHP integrated development tool

    WebStorm Mac version

    WebStorm Mac version

    Useful JavaScript development tools