Schema的优化和索引 - 索引的基础 - 索引的类型 - Hash索引
2009-09-02 00:00:00 来源:WEB开发网这个方法的缺点就是需要维护hash值。你可以手动去做或者,在MySQL5.0以上版本使用触发器。下面的例子就是当你insert和update的时候通过触发器来维护crc_url.首先创建一个表
CREATE TABLE pseudohash (
id int unsigned NOT NULL auto_increment,
url varchar(255) NOT NULL,
url_crc int unsigned NOT NULL DEFAULT 0,
PRIMARY KEY(id)
);
现在我们创建触发器。我们先改变一下delimiter。这些写起来比较方便
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 ;
来让我们测试下触发器
mysql> INSERT INTO pseudohash (url) VALUES ('http://www.mysql.com');
mysql> SELECT * FROM pseudohash;
+----+----------------------+------------+
| id | url | url_crc |
+----+----------------------+------------+
| 1 | http://www.mysql.com | 1560514994 |
+----+----------------------+------------+
mysql> UPDATE pseudohash SET url='http://www.mysql.com/' WHERE id=1;
mysql> SELECT * FROM pseudohash;
+----+-----------------------+------------+
| id | url | url_crc |
+----+-----------------------+------------+
| 1 | http://www.mysql.com/ | 1558250469 |
+----+-----------------------+------------+
更多精彩
赞助商链接