MySQL InnoDB哈希索引优化

时间:2014-10-10 07:30:15

标签: mysql optimization

我想知道我是否可以更好地优化它,也许有人在努力。

首先我有桌子:

CREATE TABLE `site_url` (
    `id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
    `url_hash` CHAR(32) NULL DEFAULT NULL,
    `url` VARCHAR(2048) NULL DEFAULT NULL,
    PRIMARY KEY (`id`),
    INDEX `url_hash` (`url_hash`)
)
ENGINE=InnoDB;

我在哪里存储网站URI(域名在不同的表中,但出于这个问题的目的,id并不重要 - 我希望)

url_hash是根据url

计算的MD5

似乎所有字段的长度都很长,索引应该是正确的,但其中有一些数据,我正在寻找更多优化。

标准查询如下所示:

select id from site_url where site_url.url_hash = MD5('something - often calculated in application rather than in mysql') and site_url.url = 'something - often calculated in application rather than in mysql'

描述给出:

+----+-------------+----------+------+---------------+----------+---------+-------+------+------------------------------------+
| id | select_type |  table   | type | possible_keys |   key    | key_len |  ref  | rows |               Extra                |
+----+-------------+----------+------+---------------+----------+---------+-------+------+------------------------------------+
|  1 | SIMPLE      | site_url | ref  | url_hash      | url_hash |      97 | const |    1 | Using index condition; Using where |
+----+-------------+----------+------+---------------+----------+---------+-------+------+------------------------------------+

但我想知道我是否可以帮助mysql进行搜索。必须通过InnoDB引擎,我才能将密钥添加到url,因为它的长度

我的朋友告诉我将哈希短路到16个字符,并将其写为数字。 BIGINT上的索引是否会快于char(32)上的索引?朋友还建议做MD5并采用它的16个第一个/最后一个字符,但我认为它会产生更多的冲突。

你对此有何看法?

2 个答案:

答案 0 :(得分:1)

这是您的查询:

select id
from site_url
where site_url.url_hash = MD5('something - often calculated in application rather than in mysql') and
      site_url.url = 'something - often calculated in application rather than in mysql';

此查询的最佳索引位于site_url(url_hash, url, id)。需要注意的是,您可能需要使用前缀,除非您设置了大前缀选项(请参阅innodb_large_prefix)。

答案 1 :(得分:0)

如果url_hash是url的md5,为什么用2个键选择?

select id from site_url where site_url.url_hash = MD5('something - often calculated in application rather than in mysql');

实际上你不需要检查site_url.url;

但如果您愿意,可以使用USE INDEX语法选择2个字段:

select id from site_url USE INDEX (url_hash) where site_url.url_hash = MD5('something - often calculated in application rather than in mysql') and site_url.url = 'something - often calculated in application rather than in mysql');