哈希索引(hashindex)基于哈希表实现,只有精确匹配索引的所有列的查询才有效,对于每一行数据,存储引擎都会对所有索引列计算一个哈希码,不同键值的行计算出
哈希索引(hash index)基于哈希表实现,只有精确匹配索引的所有列的查询才有效,对于每一行数据,存储引擎都会对所有索引列计算一个哈希码,不同键值的行计算出来的哈希码也不一样,哈希码保存在哈希索引中,同时哈希表中保存指向每个数据的指针。
1、Memory引擎支持哈希索引,也支持B-Tree索引,而且支持非唯一的哈希索引,如果多个列的哈希值相同,索引会以链表的方式存放多个记录指针到同一个哈希条目,这个是和特别的。
举例说明:
CREATE TABLE `testhash` ( `fname` varchar(50) NOT NULL, `lname` varchar(50) NOT NULL, KEY `fname` (`fname`) USING HASH ) ENGINE=MEMORY DEFAULT CHARSET=utf8 |假设索引使用f()生成哈希码如下
f('Arjen') = 2323
f('Baron') = 7437
f('Peter') = 8784
f('Vadim') = 2458
则哈希索引数据结构如下
槽
值
2323
指向第1行指针
2458
指向第4行指针
7437
指向第2行指针
8784
指向第3行指针
注意哈希码是有序的,但是数据行不是。
当执行查询的时候
select * from testhash where fname='Peter';先计算哈希码,然后找到第3行指针,最后比较第3行的值是否为‘Peter’,以确定就是要找的行。
2、哈希索引的限制:
a、哈希索引只包含哈希码和行指针,不存储字段值,所以无法用索引中的值来避免去读取行。
b、哈希索引数据并不是按照索引值顺序存储的,所以也就无法用于排序。
c、哈希索引也不支持部分索引列匹配查找,必须利用所有索引列,因为哈希值是通过所有索引列计算的。
d、哈希索引只支持等值比较查询,包括=、in()、(安全比较)比较包含null的时候用。哈希也不支持任何范围查询,比方说where price > 100
e、哈希索引非常快,除非有哈希冲突(不同的索引值会有相同的哈希值),这个时候引擎必须遍历链表中的所有行来匹配。
f、哈希冲突较多的时候,比方列上相同的值比较多的时候,索引维护代价就会比较高。
InnoDB引擎有一个特殊的功能叫做“自适应哈希索引”,由引擎内部实现,也可以关闭。
3、创建自定义哈希索引
如果存储引擎不支持哈希索引,可以在B-Tree基础上创建一个伪哈希索引。这个和真正的哈希索引不是一回事,还是用到B-Tree进行查找,只是利用键值的哈希值而不是键值来进行索引查找,只需要在where中手动指定哈希函数。
举例说明:
如果需要存储大量的URL,并且需要根据URL进行搜索,如果使用B-Tree来索引URL,存储内容会很大。比方说下面的查询
select * from url where url="http://www.baidu.com";若删除原来的URL列索引,而新增一个被索引的字段url_crc,使用crc32做哈希就可以使用下面的查询了
select * from url where url_crc=crc32("http://www.baidu.com") and url="http://www.baidu.com";这样性能就会很高。
这样的缺陷是需要维护哈希值。可以使用触发器来实现维护工作。
创建一张表
CREATE TABLE `pseudohash` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `url` varchar(255) NOT NULL, `url_crc` int(10) unsigned NOT NULL DEFAULT '0', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;创建触发器
//插入 delimiter $$ create trigger pseudohash_crc_ins before insert on pseudohash for each row begin set NEW.url_crc=crc32(NEW.url);end;$$ //更新 create trigger pseudohash_crc_upd before update on pseudohash for each row begin set NEW.url_crc=crc32(NEW.url);end;$$ delimiter ;尽量避免使用太长的哈希函数,会浪费很多空间。除非出现了大量冲突,,可以考虑自己实现一个简单的64位哈希函数,一个简单的方法是使用MD5()返回一部分值。
有一点值得注意:
当使用哈希索引进行查询的时候,必须在where中同时跟上rul的匹配,一旦出现了哈希冲突,这个真正要查询的值才会帮助匹配出真正的行。
本文出自 “phper-每天一点点~” 博客,请务必保留此出处

ACID attributes include atomicity, consistency, isolation and durability, and are the cornerstone of database design. 1. Atomicity ensures that the transaction is either completely successful or completely failed. 2. Consistency ensures that the database remains consistent before and after a transaction. 3. Isolation ensures that transactions do not interfere with each other. 4. Persistence ensures that data is permanently saved after transaction submission.

MySQL is not only a database management system (DBMS) but also closely related to programming languages. 1) As a DBMS, MySQL is used to store, organize and retrieve data, and optimizing indexes can improve query performance. 2) Combining SQL with programming languages, embedded in Python, using ORM tools such as SQLAlchemy can simplify operations. 3) Performance optimization includes indexing, querying, caching, library and table division and transaction management.

MySQL uses SQL commands to manage data. 1. Basic commands include SELECT, INSERT, UPDATE and DELETE. 2. Advanced usage involves JOIN, subquery and aggregate functions. 3. Common errors include syntax, logic and performance issues. 4. Optimization tips include using indexes, avoiding SELECT* and using LIMIT.

MySQL is an efficient relational database management system suitable for storing and managing data. Its advantages include high-performance queries, flexible transaction processing and rich data types. In practical applications, MySQL is often used in e-commerce platforms, social networks and content management systems, but attention should be paid to performance optimization, data security and scalability.

The relationship between SQL and MySQL is the relationship between standard languages and specific implementations. 1.SQL is a standard language used to manage and operate relational databases, allowing data addition, deletion, modification and query. 2.MySQL is a specific database management system that uses SQL as its operating language and provides efficient data storage and management.

InnoDB uses redologs and undologs to ensure data consistency and reliability. 1.redologs record data page modification to ensure crash recovery and transaction persistence. 2.undologs records the original data value and supports transaction rollback and MVCC.

Key metrics for EXPLAIN commands include type, key, rows, and Extra. 1) The type reflects the access type of the query. The higher the value, the higher the efficiency, such as const is better than ALL. 2) The key displays the index used, and NULL indicates no index. 3) rows estimates the number of scanned rows, affecting query performance. 4) Extra provides additional information, such as Usingfilesort prompts that it needs to be optimized.

Usingtemporary indicates that the need to create temporary tables in MySQL queries, which are commonly found in ORDERBY using DISTINCT, GROUPBY, or non-indexed columns. You can avoid the occurrence of indexes and rewrite queries and improve query performance. Specifically, when Usingtemporary appears in EXPLAIN output, it means that MySQL needs to create temporary tables to handle queries. This usually occurs when: 1) deduplication or grouping when using DISTINCT or GROUPBY; 2) sort when ORDERBY contains non-index columns; 3) use complex subquery or join operations. Optimization methods include: 1) ORDERBY and GROUPB


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 Linux new version
SublimeText3 Linux latest version

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.