0

我目前正在使用 utf8 mysql 数据库。它检查翻译是否已经在数据库中,如果没有,它会进行翻译并将其存储在数据库中。

SELECT * FROM `translations` WHERE `input_text`=? AND `input_lang`=? AND `output_lang`=?;

(另一个字段是“output_text”。)对于基本数据库,它会首先逐字母比较输入文本与“input_text”“TEXT”字段。只要字符匹配,它就会继续比较它们。如果他们停止匹配,它将进入下一行。

我不知道数据库在低级别是如何工作的,但我假设对于基本数据库,它会在确定输入文本不在数据库中之前从数据库的每一行中搜索至少一个字符。

理想情况下,输入文本将被转换为哈希码(例如使用 sha1),并且每个“input_text”也将是一个哈希。然后,如果数据库被正确排序,它可以快速找到与哈希匹配的所有行,然后检查实际文本。如果没有匹配的哈希,那么即使没有手动检查每一行,它也不会返回任何结果。

是否有一种 mysql 存储引擎可以做这样的事情,或者是否有一些额外的 php 可以优化事情?应该将“input_text”设置为某种“索引”吗?(主要/唯一/索引/全文)

是否有另一种与 php 兼容且比 mysql 优越得多的数据库类型?

编辑:这讨论了 MySQL 的 B-Tree vs Hash 索引:

http://dev.mysql.com/doc/refman/5.5/en/index-btree-hash.html

哈希索引的任何限制对我来说都不是问题。它还说

它们仅用于使用 = 或 <=> 运算符的相等比较(但速度非常快)

[“非常”被他们用斜体表示]

新问题:

如何将“input_text”TEXT 设置为哈希索引?顺便说一句,多行包含相同的“input_text”......对于哈希索引可以吗?

http://dev.mysql.com/doc/refman/5.5/en/column-indexes.html

说“MEMORY 存储引擎默认使用 HASH 索引”——这是否意味着我必须更改存储引擎并将列索引设置为 INDEX?

4

2 回答 2

0

An index will speed up the lookups a lot.

By default indexes in InnoDB and MyISAM use search trees (B-trees). There is a limitation on the length of the row the index so you will have to index only the 1-st ~700 bytes of text.

CREATE INDEX txt_lookup ON translations (input_lang, output_lang, input_text(255));

This will create an index on input_lang, output_lang and the 1-st 255 characters of input_text.

When you select with your example query MySQL will use the index to find the rows with the appropriate languages and the same starting 255 characters quickly and then it will do the slow string compare with the full length of the column on the small set of rows which it got from the index.

于 2013-04-05T08:34:01.487 回答
0

一个普通的INDEX子句就足够了(确保索引所有字段,它在磁盘上会很大,但速度更快)。FULLTEXT使用LIKE子句时索引很好;-)

无论如何,对于这种查找,您应该使用 NoSQL 存储,例如Redis,它速度极快,并且具有内存存储,并且还通过快照进行数据持久性。

这里有一个 php 扩展:https ://github.com/nicolasff/phpredis

您将拥有以下形式的 redis 键:YOUR_PROJECT:INPUT_LANG:WORD:OUTPUT_LANG为了更好地管理数据,只需将每个值替换为您的值即可;)

于 2013-04-05T08:28:51.000 回答